| 1 |
import { Server, Model, JSONAPISerializer } from "miragejs"; |
| 2 |
import PostShorthandRouteHandler from "@lib/route-handlers/shorthands/post"; |
| 3 |
|
| 4 |
describe("Integration | Route Handlers | POST shorthand", () => { |
| 5 |
let server, schema, serializer, body; |
| 6 |
|
| 7 |
beforeEach(() => { |
| 8 |
server = new Server({ |
| 9 |
environment: "development", |
| 10 |
models: { |
| 11 |
author: Model.extend({}), |
| 12 |
}, |
| 13 |
}); |
| 14 |
server.timing = 0; |
| 15 |
server.logging = false; |
| 16 |
|
| 17 |
schema = server.schema; |
| 18 |
serializer = new JSONAPISerializer(); |
| 19 |
|
| 20 |
body = { |
| 21 |
data: { |
| 22 |
type: "authors", |
| 23 |
attributes: { |
| 24 |
"first-name": "Ganon", |
| 25 |
"last-name": "Dorf", |
| 26 |
}, |
| 27 |
}, |
| 28 |
}; |
| 29 |
}); |
| 30 |
|
| 31 |
afterEach(() => { |
| 32 |
server.shutdown(); |
| 33 |
}); |
| 34 |
|
| 35 |
test("string shorthand creates a record of the specified type and returns the new model", () => { |
| 36 |
let request = { requestBody: JSON.stringify(body), url: "/people" }; |
| 37 |
let handler = new PostShorthandRouteHandler(schema, serializer, "author"); |
| 38 |
|
| 39 |
let model = handler.handle(request); |
| 40 |
|
| 41 |
expect(schema.db.authors).toHaveLength(1); |
| 42 |
expect(model instanceof Model).toBeTruthy(); |
| 43 |
expect(model.modelName).toEqual("author"); |
| 44 |
expect(model.firstName).toEqual("Ganon"); |
| 45 |
}); |
| 46 |
|
| 47 |
test("query params are ignored", () => { |
| 48 |
let request = { |
| 49 |
requestBody: JSON.stringify(body), |
| 50 |
url: "/authors?foo=bar", |
| 51 |
queryParams: { foo: "bar" }, |
| 52 |
}; |
| 53 |
let handler = new PostShorthandRouteHandler(schema, serializer, "author"); |
| 54 |
|
| 55 |
let model = handler.handle(request); |
| 56 |
|
| 57 |
expect(schema.db.authors).toHaveLength(1); |
| 58 |
expect(model instanceof Model).toBeTruthy(); |
| 59 |
expect(model.modelName).toEqual("author"); |
| 60 |
expect(model.firstName).toEqual("Ganon"); |
| 61 |
}); |
| 62 |
|
| 63 |
test("undefined shorthand creates a record and returns the new model", () => { |
| 64 |
let request = { requestBody: JSON.stringify(body), url: "/authors" }; |
| 65 |
let handler = new PostShorthandRouteHandler( |
| 66 |
schema, |
| 67 |
serializer, |
| 68 |
null, |
| 69 |
"/authors" |
| 70 |
); |
| 71 |
|
| 72 |
let model = handler.handle(request); |
| 73 |
|
| 74 |
expect(schema.db.authors).toHaveLength(1); |
| 75 |
expect(model instanceof Model).toBeTruthy(); |
| 76 |
expect(model.modelName).toEqual("author"); |
| 77 |
expect(model.firstName).toEqual("Ganon"); |
| 78 |
}); |
| 79 |
|
| 80 |
test("if a shorthand tries to access an unknown type it throws an error", () => { |
| 81 |
let request = { requestBody: JSON.stringify(body), url: "/foobars" }; |
| 82 |
let handler = new PostShorthandRouteHandler(schema, serializer, "foobar"); |
| 83 |
|
| 84 |
expect(function () { |
| 85 |
handler.handle(request); |
| 86 |
}).toThrow(); |
| 87 |
expect(true).toBeTruthy(); |
| 88 |
}); |
| 89 |
}); |