| 1 |
import { Server, Model, hasMany } from "miragejs"; |
| 2 |
import DeleteShorthandRouteHandler from "@lib/route-handlers/shorthands/delete"; |
| 3 |
import JSONAPISerializer from "@lib/serializers/json-api-serializer"; |
| 4 |
|
| 5 |
describe("Integration | Route Handlers | DELETE shorthand", () => { |
| 6 |
let server, schema, serializer; |
| 7 |
|
| 8 |
beforeEach(() => { |
| 9 |
server = new Server({ |
| 10 |
environment: "development", |
| 11 |
models: { |
| 12 |
wordSmith: Model.extend({ |
| 13 |
blogPosts: hasMany(), |
| 14 |
}), |
| 15 |
blogPost: Model, |
| 16 |
}, |
| 17 |
}); |
| 18 |
server.timing = 0; |
| 19 |
server.logging = false; |
| 20 |
|
| 21 |
let wordSmiths = [{ id: 1, name: "Ganon", blogPostIds: [1] }]; |
| 22 |
let blogPosts = [ |
| 23 |
{ id: 1, title: "Lorem", wordSmithId: "1" }, |
| 24 |
{ id: 2, title: "Another", wordSmithId: "2" }, |
| 25 |
]; |
| 26 |
server.db.loadData({ wordSmiths, blogPosts }); |
| 27 |
|
| 28 |
schema = server.schema; |
| 29 |
serializer = new JSONAPISerializer(); |
| 30 |
}); |
| 31 |
|
| 32 |
afterEach(() => { |
| 33 |
server.shutdown(); |
| 34 |
}); |
| 35 |
|
| 36 |
test("undefined shorthand deletes the record and returns null", () => { |
| 37 |
let request = { url: "/word-smiths/1", params: { id: "1" } }; |
| 38 |
let handler = new DeleteShorthandRouteHandler( |
| 39 |
schema, |
| 40 |
serializer, |
| 41 |
undefined, |
| 42 |
"/word-smiths/:id" |
| 43 |
); |
| 44 |
|
| 45 |
let response = handler.handle(request); |
| 46 |
|
| 47 |
expect(schema.db.wordSmiths).toHaveLength(0); |
| 48 |
expect(response).toBeNil(); |
| 49 |
}); |
| 50 |
|
| 51 |
test("query params are ignored", () => { |
| 52 |
let request = { |
| 53 |
url: "/word-smiths/1?foo=bar", |
| 54 |
params: { id: "1" }, |
| 55 |
queryParams: { foo: "bar" }, |
| 56 |
}; |
| 57 |
let handler = new DeleteShorthandRouteHandler( |
| 58 |
schema, |
| 59 |
serializer, |
| 60 |
undefined, |
| 61 |
"/word-smiths/:id" |
| 62 |
); |
| 63 |
|
| 64 |
let response = handler.handle(request); |
| 65 |
|
| 66 |
expect(schema.db.wordSmiths).toHaveLength(0); |
| 67 |
expect(response).toBeNil(); |
| 68 |
}); |
| 69 |
|
| 70 |
test("string shorthand deletes the record of the specified type", () => { |
| 71 |
let request = { |
| 72 |
url: "/word-smiths/1?foo=bar", |
| 73 |
params: { id: "1" }, |
| 74 |
queryParams: { foo: "bar" }, |
| 75 |
}; |
| 76 |
let handler = new DeleteShorthandRouteHandler( |
| 77 |
schema, |
| 78 |
serializer, |
| 79 |
undefined, |
| 80 |
"/word-smiths/:id" |
| 81 |
); |
| 82 |
|
| 83 |
let response = handler.handle(request); |
| 84 |
|
| 85 |
expect(schema.db.wordSmiths).toHaveLength(0); |
| 86 |
expect(response).toBeNil(); |
| 87 |
}); |
| 88 |
|
| 89 |
test("array shorthand deletes the record and all related records", () => { |
| 90 |
let request = { url: "/word-smiths/1", params: { id: "1" } }; |
| 91 |
let handler = new DeleteShorthandRouteHandler(schema, serializer, [ |
| 92 |
"word-smith", |
| 93 |
"blog-posts", |
| 94 |
]); |
| 95 |
|
| 96 |
let response = handler.handle(request); |
| 97 |
|
| 98 |
expect(schema.db.wordSmiths).toHaveLength(0); |
| 99 |
expect(schema.db.blogPosts).toHaveLength(1); |
| 100 |
expect(response).toBeNil(); |
| 101 |
}); |
| 102 |
|
| 103 |
test("if a shorthand tries to access an unknown type it throws an error", () => { |
| 104 |
let request = { url: "/foobars/1", params: { id: "1" } }; |
| 105 |
let handler = new DeleteShorthandRouteHandler( |
| 106 |
schema, |
| 107 |
serializer, |
| 108 |
undefined, |
| 109 |
"/foobars/:id" |
| 110 |
); |
| 111 |
|
| 112 |
expect(function () { |
| 113 |
handler.handle(request); |
| 114 |
}).toThrow(); |
| 115 |
expect(true).toBeTruthy(); |
| 116 |
}); |
| 117 |
}); |