| 1 |
import { |
| 2 |
Model, |
| 3 |
hasMany, |
| 4 |
belongsTo, |
| 5 |
JSONAPISerializer, |
| 6 |
RestSerializer, |
| 7 |
Response, |
| 8 |
Server, |
| 9 |
} from "miragejs"; |
| 10 |
import Collection from "@lib/orm/collection"; |
| 11 |
import GetShorthandRouteHandler from "@lib/route-handlers/shorthands/get"; |
| 12 |
|
| 13 |
describe("Integration | Route Handlers | GET shorthand", () => { |
| 14 |
let server, authors, posts, photos, projectOwners, schema, serializer; |
| 15 |
|
| 16 |
beforeEach(() => { |
| 17 |
server = new Server({ |
| 18 |
environment: "development", |
| 19 |
models: { |
| 20 |
author: Model.extend({ |
| 21 |
posts: hasMany(), |
| 22 |
}), |
| 23 |
post: Model.extend({ |
| 24 |
author: belongsTo(), |
| 25 |
comments: hasMany(), |
| 26 |
}), |
| 27 |
comment: Model.extend({ |
| 28 |
post: belongsTo(), |
| 29 |
}), |
| 30 |
photo: Model, |
| 31 |
"project-owner": Model, |
| 32 |
}, |
| 33 |
}); |
| 34 |
server.timing = 0; |
| 35 |
server.logging = false; |
| 36 |
|
| 37 |
authors = [ |
| 38 |
{ id: 1, name: "Link" }, |
| 39 |
{ id: 2, name: "Zelda" }, |
| 40 |
{ id: 3, name: "Epona" }, |
| 41 |
]; |
| 42 |
posts = [ |
| 43 |
{ id: 1, title: "Lorem", authorId: 1 }, |
| 44 |
{ id: 2, title: "Ipsum", authorId: 1 }, |
| 45 |
]; |
| 46 |
photos = [ |
| 47 |
{ id: 1, title: "Amazing", location: "Hyrule" }, |
| 48 |
{ id: 2, title: "Photo", location: "Goron City" }, |
| 49 |
]; |
| 50 |
projectOwners = [{ id: 1, name: "Nintendo" }]; |
| 51 |
server.db.loadData({ |
| 52 |
authors: authors, |
| 53 |
posts: posts, |
| 54 |
photos: photos, |
| 55 |
projectOwners: projectOwners, |
| 56 |
}); |
| 57 |
|
| 58 |
schema = server.schema; |
| 59 |
serializer = new JSONAPISerializer(); |
| 60 |
}); |
| 61 |
|
| 62 |
afterEach(() => { |
| 63 |
server.shutdown(); |
| 64 |
}); |
| 65 |
|
| 66 |
test("undefined shorthand returns the collection of models", () => { |
| 67 |
let request = { url: "/authors" }; |
| 68 |
let handler = new GetShorthandRouteHandler( |
| 69 |
schema, |
| 70 |
serializer, |
| 71 |
undefined, |
| 72 |
"/authors" |
| 73 |
); |
| 74 |
|
| 75 |
let authors = handler.handle(request); |
| 76 |
|
| 77 |
expect(authors.models).toHaveLength(3); |
| 78 |
expect(authors.models[0] instanceof Model).toBeTruthy(); |
| 79 |
expect(authors.models[0].modelName).toEqual("author"); |
| 80 |
}); |
| 81 |
|
| 82 |
test("undefined shorthand ignores query params", () => { |
| 83 |
let request = { url: "/authors?foo=bar" }; |
| 84 |
let handler = new GetShorthandRouteHandler( |
| 85 |
schema, |
| 86 |
serializer, |
| 87 |
undefined, |
| 88 |
"/authors" |
| 89 |
); |
| 90 |
|
| 91 |
let authors = handler.handle(request); |
| 92 |
|
| 93 |
expect(authors.models).toHaveLength(3); |
| 94 |
expect(authors.models[0] instanceof Model).toBeTruthy(); |
| 95 |
expect(authors.models[0].modelName).toEqual("author"); |
| 96 |
}); |
| 97 |
|
| 98 |
test("undefined shorthand can return a single model", () => { |
| 99 |
let request = { url: "/authors/2", params: { id: 2 } }; |
| 100 |
let handler = new GetShorthandRouteHandler( |
| 101 |
schema, |
| 102 |
serializer, |
| 103 |
undefined, |
| 104 |
"/authors/:id" |
| 105 |
); |
| 106 |
|
| 107 |
let author = handler.handle(request); |
| 108 |
|
| 109 |
expect(author instanceof Model).toBeTruthy(); |
| 110 |
expect(author.modelName).toEqual("author"); |
| 111 |
expect(author.name).toEqual("Zelda"); |
| 112 |
}); |
| 113 |
|
| 114 |
test("undefined shorthand returns a 404 if a singular resource does not exist", () => { |
| 115 |
let request = { url: "/authors/99", params: { id: 99 } }; |
| 116 |
let handler = new GetShorthandRouteHandler( |
| 117 |
schema, |
| 118 |
serializer, |
| 119 |
undefined, |
| 120 |
"/authors/:id" |
| 121 |
); |
| 122 |
|
| 123 |
let author = handler.handle(request); |
| 124 |
|
| 125 |
expect(author instanceof Response).toBeTruthy(); |
| 126 |
expect(author.code).toEqual(404); |
| 127 |
}); |
| 128 |
|
| 129 |
test("undefined shorthand ignores query params for a singular resource", () => { |
| 130 |
let request = { url: "/authors/2?foo=bar", params: { id: 2 } }; |
| 131 |
let handler = new GetShorthandRouteHandler( |
| 132 |
schema, |
| 133 |
serializer, |
| 134 |
undefined, |
| 135 |
"/authors/:id" |
| 136 |
); |
| 137 |
|
| 138 |
let author = handler.handle(request); |
| 139 |
|
| 140 |
expect(author instanceof Model).toBeTruthy(); |
| 141 |
expect(author.modelName).toEqual("author"); |
| 142 |
expect(author.name).toEqual("Zelda"); |
| 143 |
}); |
| 144 |
|
| 145 |
test("undefined shorthand with coalesce true returns the appropriate models [JSONAPI]", () => { |
| 146 |
let request = { |
| 147 |
url: "/authors?filter[id]=1,3", |
| 148 |
queryParams: { "filter[id]": "1,3" }, |
| 149 |
}; |
| 150 |
let options = { coalesce: true }; |
| 151 |
let handler = new GetShorthandRouteHandler( |
| 152 |
schema, |
| 153 |
serializer, |
| 154 |
undefined, |
| 155 |
"/authors", |
| 156 |
options |
| 157 |
); |
| 158 |
|
| 159 |
let authors = handler.handle(request); |
| 160 |
|
| 161 |
expect(authors.models).toHaveLength(2); |
| 162 |
expect(authors.models.map((author) => author.name)).toEqual([ |
| 163 |
"Link", |
| 164 |
"Epona", |
| 165 |
]); |
| 166 |
}); |
| 167 |
|
| 168 |
test("undefined shorthand with coalesce true returns the appropriate models [REST]", () => { |
| 169 |
let request = { |
| 170 |
url: "/authors?ids[]=1&ids[]=3", |
| 171 |
queryParams: { ids: [1, 3] }, |
| 172 |
}; |
| 173 |
let options = { coalesce: true }; |
| 174 |
let handler = new GetShorthandRouteHandler( |
| 175 |
schema, |
| 176 |
new RestSerializer(), |
| 177 |
undefined, |
| 178 |
"/authors", |
| 179 |
options |
| 180 |
); |
| 181 |
|
| 182 |
let authors = handler.handle(request); |
| 183 |
|
| 184 |
expect(authors.models).toHaveLength(2); |
| 185 |
expect(authors.models.map((author) => author.name)).toEqual([ |
| 186 |
"Link", |
| 187 |
"Epona", |
| 188 |
]); |
| 189 |
}); |
| 190 |
|
| 191 |
test("string shorthand returns the correct collection of models", () => { |
| 192 |
let request = { url: "/people" }; |
| 193 |
let handler = new GetShorthandRouteHandler(schema, serializer, "author"); |
| 194 |
|
| 195 |
let authors = handler.handle(request); |
| 196 |
|
| 197 |
expect(authors.models).toHaveLength(3); |
| 198 |
expect(authors.models[0] instanceof Model).toBeTruthy(); |
| 199 |
expect(authors.models[0].modelName).toEqual("author"); |
| 200 |
}); |
| 201 |
|
| 202 |
test("string shorthand with an id returns the correct model", () => { |
| 203 |
let request = { url: "/people/2", params: { id: 2 } }; |
| 204 |
let handler = new GetShorthandRouteHandler(schema, serializer, "author"); |
| 205 |
|
| 206 |
let author = handler.handle(request); |
| 207 |
|
| 208 |
expect(author instanceof Model).toBeTruthy(); |
| 209 |
expect(author.modelName).toEqual("author"); |
| 210 |
expect(author.name).toEqual("Zelda"); |
| 211 |
}); |
| 212 |
|
| 213 |
test("string shorthand with an id 404s if the model is not found", () => { |
| 214 |
let request = { url: "/people/99", params: { id: 99 } }; |
| 215 |
let handler = new GetShorthandRouteHandler(schema, serializer, "author"); |
| 216 |
|
| 217 |
let author = handler.handle(request); |
| 218 |
|
| 219 |
expect(author instanceof Response).toBeTruthy(); |
| 220 |
expect(author.code).toEqual(404); |
| 221 |
}); |
| 222 |
|
| 223 |
test("string shorthand with coalesce returns the correct models [JSONAPI]", () => { |
| 224 |
let request = { |
| 225 |
url: "/authors?filter[id]=1,3", |
| 226 |
queryParams: { "filter[id]": "1,3" }, |
| 227 |
}; |
| 228 |
let options = { coalesce: true }; |
| 229 |
let handler = new GetShorthandRouteHandler( |
| 230 |
schema, |
| 231 |
serializer, |
| 232 |
"author", |
| 233 |
"/people", |
| 234 |
options |
| 235 |
); |
| 236 |
|
| 237 |
let authors = handler.handle(request); |
| 238 |
|
| 239 |
expect(authors.models).toHaveLength(2); |
| 240 |
expect(authors.models.map((author) => author.name)).toEqual([ |
| 241 |
"Link", |
| 242 |
"Epona", |
| 243 |
]); |
| 244 |
}); |
| 245 |
|
| 246 |
test("string shorthand with coalesce returns the correct models [REST]", () => { |
| 247 |
let request = { |
| 248 |
url: "/people?ids[]=1&ids[]=3", |
| 249 |
queryParams: { ids: [1, 3] }, |
| 250 |
}; |
| 251 |
let options = { coalesce: true }; |
| 252 |
let handler = new GetShorthandRouteHandler( |
| 253 |
schema, |
| 254 |
new RestSerializer(), |
| 255 |
"author", |
| 256 |
"/people", |
| 257 |
options |
| 258 |
); |
| 259 |
|
| 260 |
let authors = handler.handle(request); |
| 261 |
|
| 262 |
expect(authors.models).toHaveLength(2); |
| 263 |
expect(authors.models.map((author) => author.name)).toEqual([ |
| 264 |
"Link", |
| 265 |
"Epona", |
| 266 |
]); |
| 267 |
}); |
| 268 |
|
| 269 |
test("array shorthand returns the correct models", () => { |
| 270 |
let url = "/home"; |
| 271 |
let request = { url }; |
| 272 |
let handler = new GetShorthandRouteHandler( |
| 273 |
schema, |
| 274 |
serializer, |
| 275 |
["authors", "photos"], |
| 276 |
url |
| 277 |
); |
| 278 |
|
| 279 |
let models = handler.handle(request); |
| 280 |
|
| 281 |
expect(models[0] instanceof Collection).toBeTruthy(); |
| 282 |
expect(models[0].modelName).toEqual("author"); |
| 283 |
expect(models[0].models).toHaveLength(authors.length); |
| 284 |
|
| 285 |
expect(models[1] instanceof Collection).toBeTruthy(); |
| 286 |
expect(models[1].modelName).toEqual("photo"); |
| 287 |
expect(models[1].models).toHaveLength(photos.length); |
| 288 |
}); |
| 289 |
|
| 290 |
test("array shorthand for a singular resource errors", () => { |
| 291 |
let url = "/authors/1"; |
| 292 |
let request = { url, params: { id: 1 } }; |
| 293 |
let handler = new GetShorthandRouteHandler( |
| 294 |
schema, |
| 295 |
serializer, |
| 296 |
["author", "posts"], |
| 297 |
url |
| 298 |
); |
| 299 |
|
| 300 |
expect(function () { |
| 301 |
handler.handle(request); |
| 302 |
}).toThrow(); |
| 303 |
}); |
| 304 |
|
| 305 |
test("shorthand for list of models with a dash in their name", () => { |
| 306 |
let url = "/project-owners"; |
| 307 |
let request = { url }; |
| 308 |
let handler = new GetShorthandRouteHandler( |
| 309 |
schema, |
| 310 |
serializer, |
| 311 |
undefined, |
| 312 |
url |
| 313 |
); |
| 314 |
let models = handler.handle(request); |
| 315 |
|
| 316 |
expect(models.models).toHaveLength(1); |
| 317 |
expect(models.models[0] instanceof Model).toBeTruthy(); |
| 318 |
expect(models.models[0].modelName).toEqual("project-owner"); |
| 319 |
}); |
| 320 |
|
| 321 |
test("if a shorthand tries to access an unknown type it throws an error", () => { |
| 322 |
let url = "/foobars"; |
| 323 |
let request = { url }; |
| 324 |
let handler = new GetShorthandRouteHandler( |
| 325 |
schema, |
| 326 |
serializer, |
| 327 |
undefined, |
| 328 |
url |
| 329 |
); |
| 330 |
|
| 331 |
expect(function () { |
| 332 |
handler.handle(request); |
| 333 |
}).toThrow(); |
| 334 |
}); |
| 335 |
}); |