| 1 |
import { Server, Model, JSONAPISerializer, Response } from "miragejs"; |
| 2 |
import HeadShorthandRouteHandler from "@lib/route-handlers/shorthands/head"; |
| 3 |
|
| 4 |
describe("Integration | Route Handlers | HEAD shorthand", () => { |
| 5 |
let server, authors, photos, schema, serializer; |
| 6 |
|
| 7 |
beforeEach(() => { |
| 8 |
server = new Server({ |
| 9 |
environment: "development", |
| 10 |
models: { |
| 11 |
author: Model, |
| 12 |
photo: Model, |
| 13 |
}, |
| 14 |
}); |
| 15 |
server.timing = 0; |
| 16 |
server.logging = false; |
| 17 |
|
| 18 |
authors = [ |
| 19 |
{ id: 1, name: "Link" }, |
| 20 |
{ id: 2, name: "Zelda" }, |
| 21 |
{ id: 3, name: "Epona" }, |
| 22 |
]; |
| 23 |
photos = [ |
| 24 |
{ id: 1, title: "Amazing", location: "Hyrule" }, |
| 25 |
{ id: 2, title: "Photo", location: "Goron City" }, |
| 26 |
]; |
| 27 |
server.db.loadData({ |
| 28 |
authors: authors, |
| 29 |
photos: photos, |
| 30 |
}); |
| 31 |
|
| 32 |
schema = server.schema; |
| 33 |
serializer = new JSONAPISerializer(); |
| 34 |
}); |
| 35 |
|
| 36 |
afterEach(() => { |
| 37 |
server.shutdown(); |
| 38 |
}); |
| 39 |
|
| 40 |
test("undefined shorthand with an ID that is not in the DB will return a 404 Response", () => { |
| 41 |
let request = { url: "/authors", params: { id: 101 } }; |
| 42 |
let handler = new HeadShorthandRouteHandler( |
| 43 |
schema, |
| 44 |
serializer, |
| 45 |
undefined, |
| 46 |
"/authors" |
| 47 |
); |
| 48 |
|
| 49 |
let response = handler.handle(request); |
| 50 |
|
| 51 |
expect(response instanceof Response).toBeTruthy(); |
| 52 |
expect(response.code).toEqual(404); |
| 53 |
}); |
| 54 |
|
| 55 |
test("undefined shorthand with an ID that is in the DB will return a 204 Response", () => { |
| 56 |
let request = { url: "/authors", params: { id: 1 } }; |
| 57 |
let handler = new HeadShorthandRouteHandler( |
| 58 |
schema, |
| 59 |
serializer, |
| 60 |
undefined, |
| 61 |
"/authors" |
| 62 |
); |
| 63 |
|
| 64 |
let response = handler.handle(request); |
| 65 |
|
| 66 |
expect(response instanceof Response).toBeTruthy(); |
| 67 |
expect(response.code).toEqual(204); |
| 68 |
}); |
| 69 |
|
| 70 |
test("undefined shorthand with coalesce true will return a 204 response if one of the IDs are found", () => { |
| 71 |
let request = { |
| 72 |
url: "/authors?ids[]=1&ids[]=3", |
| 73 |
queryParams: { ids: [1, 3] }, |
| 74 |
}; |
| 75 |
let options = { coalesce: true }; |
| 76 |
let handler = new HeadShorthandRouteHandler( |
| 77 |
schema, |
| 78 |
serializer, |
| 79 |
undefined, |
| 80 |
"/authors", |
| 81 |
options |
| 82 |
); |
| 83 |
|
| 84 |
let response = handler.handle(request); |
| 85 |
|
| 86 |
expect(response instanceof Response).toBeTruthy(); |
| 87 |
expect(response.code).toEqual(204); |
| 88 |
}); |
| 89 |
|
| 90 |
test("undefined shorthand string (no id) shorthand returns a 204 (regardless of the length of the collection)", () => { |
| 91 |
let request = { url: "/authors" }; |
| 92 |
let handler = new HeadShorthandRouteHandler( |
| 93 |
schema, |
| 94 |
serializer, |
| 95 |
undefined, |
| 96 |
"/authors" |
| 97 |
); |
| 98 |
|
| 99 |
let response = handler.handle(request); |
| 100 |
|
| 101 |
expect(response instanceof Response).toBeTruthy(); |
| 102 |
expect(response.code).toEqual(204); |
| 103 |
}); |
| 104 |
|
| 105 |
test("string shorthand with an ID that is not in the DB will return a 404 Response", () => { |
| 106 |
let request = { url: "/authors", params: { id: 101 } }; |
| 107 |
let handler = new HeadShorthandRouteHandler(schema, serializer, "author"); |
| 108 |
|
| 109 |
let response = handler.handle(request); |
| 110 |
|
| 111 |
expect(response instanceof Response).toBeTruthy(); |
| 112 |
expect(response.code).toEqual(404); |
| 113 |
}); |
| 114 |
|
| 115 |
test("string shorthand with an ID that is in the DB will return a 204 Response", () => { |
| 116 |
let request = { url: "/authors", params: { id: 1 } }; |
| 117 |
let handler = new HeadShorthandRouteHandler(schema, serializer, "author"); |
| 118 |
|
| 119 |
let response = handler.handle(request); |
| 120 |
|
| 121 |
expect(response instanceof Response).toBeTruthy(); |
| 122 |
expect(response.code).toEqual(204); |
| 123 |
}); |
| 124 |
|
| 125 |
test("string shorthand with coalesce true will return a 204 response if one of the IDs are found", () => { |
| 126 |
let request = { |
| 127 |
url: "/authors?ids[]=1&ids[]=3", |
| 128 |
queryParams: { ids: [1, 3] }, |
| 129 |
}; |
| 130 |
let options = { coalesce: true }; |
| 131 |
let handler = new HeadShorthandRouteHandler( |
| 132 |
schema, |
| 133 |
serializer, |
| 134 |
"author", |
| 135 |
"/people", |
| 136 |
options |
| 137 |
); |
| 138 |
|
| 139 |
let response = handler.handle(request); |
| 140 |
|
| 141 |
expect(response instanceof Response).toBeTruthy(); |
| 142 |
expect(response.code).toEqual(204); |
| 143 |
}); |
| 144 |
|
| 145 |
test("string shorthand string (no id) shorthand returns a 204 (regardless of the length of the collection)", () => { |
| 146 |
let request = { url: "/authors" }; |
| 147 |
let handler = new HeadShorthandRouteHandler(schema, serializer, "author"); |
| 148 |
|
| 149 |
let response = handler.handle(request); |
| 150 |
|
| 151 |
expect(response instanceof Response).toBeTruthy(); |
| 152 |
expect(response.code).toEqual(204); |
| 153 |
}); |
| 154 |
}); |