Debian Codegraph @

__tests__/external/shared/serializers/json-api-serializer/associations/model-test.js

raw
1 import { Server, Model, hasMany, belongsTo, JSONAPISerializer } from "miragejs";
2
3 describe("External | Shared | Serializers | JSON API Serializer | Associations | Model", () => {
4 let server;
5
6 beforeEach(() => {
7 server = new Server({
8 models: {
9 wordSmith: Model.extend({
10 blogPosts: hasMany(),
11 }),
12 blogPost: Model.extend({
13 wordSmith: belongsTo(),
14 fineComments: hasMany(),
15 }),
16 fineComment: Model.extend({
17 blogPost: belongsTo(),
18 }),
19 },
20 });
21 });
22
23 afterEach(() => {
24 server.shutdown();
25 });
26
27 test(`by default, it doesn't include a model's relationships if those relationships are not included in the document and no links are defined`, () => {
28 server.config({
29 serializers: {
30 application: JSONAPISerializer,
31 },
32 });
33 let link = server.schema.wordSmiths.create({
34 firstName: "Link",
35 age: 123,
36 });
37 let post = link.createBlogPost({ title: "Lorem ipsum" });
38
39 let result = server.serializerOrRegistry.serialize(post);
40 expect(result).toEqual({
41 data: {
42 type: "blog-posts",
43 id: "1",
44 attributes: {
45 title: "Lorem ipsum",
46 },
47 },
48 });
49 });
50
51 test(`when alwaysIncludeLinkageData is true, it contains linkage data for all a model's relationships, regardless of includes`, () => {
52 server.config({
53 serializers: {
54 application: JSONAPISerializer.extend({
55 alwaysIncludeLinkageData: true,
56 }),
57 },
58 });
59 let link = server.schema.wordSmiths.create({
60 firstName: "Link",
61 age: 123,
62 });
63 let post = link.createBlogPost({ title: "Lorem ipsum" });
64
65 let result = server.serializerOrRegistry.serialize(post);
66 expect(result).toEqual({
67 data: {
68 type: "blog-posts",
69 id: "1",
70 attributes: {
71 title: "Lorem ipsum",
72 },
73 relationships: {
74 "word-smith": {
75 data: {
76 type: "word-smiths",
77 id: "1",
78 },
79 },
80 "fine-comments": {
81 data: [],
82 },
83 },
84 },
85 });
86 });
87
88 test(`when shouldIncludeLinkageData returns true for a certain belongsTo relationship, it contains linkage data for that relationship, regardless of includes`, () => {
89 server.config({
90 serializers: {
91 application: JSONAPISerializer.extend({
92 shouldIncludeLinkageData(relationshipName, model) {
93 if (relationshipName === "wordSmith") {
94 return true;
95 }
96 },
97 }),
98 },
99 });
100 let link = server.schema.wordSmiths.create({
101 firstName: "Link",
102 age: 123,
103 });
104 let post = link.createBlogPost({ title: "Lorem ipsum" });
105
106 let result = server.serializerOrRegistry.serialize(post);
107 expect(result).toEqual({
108 data: {
109 type: "blog-posts",
110 id: "1",
111 attributes: {
112 title: "Lorem ipsum",
113 },
114 relationships: {
115 "word-smith": {
116 data: {
117 type: "word-smiths",
118 id: "1",
119 },
120 },
121 },
122 },
123 });
124 });
125
126 test(`when shouldIncludeLinkageData returns true for a certain hasMany relationship, it contains linkage data for that relationship, regardless of includes`, () => {
127 server.config({
128 serializers: {
129 application: JSONAPISerializer,
130 wordSmith: JSONAPISerializer.extend({
131 shouldIncludeLinkageData(relationshipName, model) {
132 if (relationshipName === "blogPosts") {
133 return true;
134 }
135 },
136 }),
137 },
138 });
139
140 let link = server.schema.wordSmiths.create({ firstName: "Link" });
141 link.createBlogPost({ title: "Lorem" });
142 link.createBlogPost({ title: "Ipsum" });
143
144 let result = server.serializerOrRegistry.serialize(link);
145
146 expect(result).toEqual({
147 data: {
148 type: "word-smiths",
149 id: "1",
150 attributes: {
151 "first-name": "Link",
152 },
153 relationships: {
154 "blog-posts": {
155 data: [
156 { type: "blog-posts", id: "1" },
157 { type: "blog-posts", id: "2" },
158 ],
159 },
160 },
161 },
162 });
163 });
164
165 test(`it includes linkage data for a has-many relationship that's being included`, () => {
166 server.config({
167 serializers: {
168 application: JSONAPISerializer,
169 wordSmith: JSONAPISerializer.extend({
170 include: ["blogPosts"],
171 }),
172 },
173 });
174
175 let link = server.schema.wordSmiths.create({ firstName: "Link" });
176 link.createBlogPost({ title: "Lorem" });
177 link.createBlogPost({ title: "Ipsum" });
178
179 let result = server.serializerOrRegistry.serialize(link);
180
181 expect(result).toEqual({
182 data: {
183 type: "word-smiths",
184 id: "1",
185 attributes: {
186 "first-name": "Link",
187 },
188 relationships: {
189 "blog-posts": {
190 data: [
191 { type: "blog-posts", id: "1" },
192 { type: "blog-posts", id: "2" },
193 ],
194 },
195 },
196 },
197 included: [
198 {
199 type: "blog-posts",
200 id: "1",
201 attributes: {
202 title: "Lorem",
203 },
204 },
205 {
206 type: "blog-posts",
207 id: "2",
208 attributes: {
209 title: "Ipsum",
210 },
211 },
212 ],
213 });
214 });
215
216 test(`it can include a chain of has-many relationships`, () => {
217 server.config({
218 serializers: {
219 application: JSONAPISerializer,
220 wordSmith: JSONAPISerializer.extend({
221 include: ["blogPosts"],
222 }),
223 blogPost: JSONAPISerializer.extend({
224 include: ["fineComments"],
225 }),
226 },
227 });
228
229 let link = server.schema.wordSmiths.create({ firstName: "Link" });
230 let post1 = link.createBlogPost({ title: "Lorem" });
231 post1.createFineComment({ text: "pwned" });
232 link.createBlogPost({ title: "Ipsum" });
233
234 let result = server.serializerOrRegistry.serialize(link);
235
236 expect(result).toEqual({
237 data: {
238 type: "word-smiths",
239 id: "1",
240 attributes: {
241 "first-name": "Link",
242 },
243 relationships: {
244 "blog-posts": {
245 data: [
246 { type: "blog-posts", id: "1" },
247 { type: "blog-posts", id: "2" },
248 ],
249 },
250 },
251 },
252 included: [
253 {
254 type: "blog-posts",
255 id: "1",
256 attributes: {
257 title: "Lorem",
258 },
259 relationships: {
260 "fine-comments": {
261 data: [{ type: "fine-comments", id: "1" }],
262 },
263 },
264 },
265 {
266 type: "fine-comments",
267 id: "1",
268 attributes: {
269 text: "pwned",
270 },
271 },
272 {
273 type: "blog-posts",
274 id: "2",
275 attributes: {
276 title: "Ipsum",
277 },
278 relationships: {
279 "fine-comments": {
280 data: [],
281 },
282 },
283 },
284 ],
285 });
286 });
287
288 test(`it can include a belongs-to relationship`, () => {
289 server.config({
290 serializers: {
291 application: JSONAPISerializer,
292 blogPost: JSONAPISerializer.extend({
293 include: ["wordSmith"],
294 }),
295 },
296 });
297
298 let link = server.schema.wordSmiths.create({ firstName: "Link" });
299 let blogPost = link.createBlogPost({ title: "Lorem" });
300 blogPost.createFineComment();
301
302 let result = server.serializerOrRegistry.serialize(blogPost);
303
304 expect(result).toEqual({
305 data: {
306 type: "blog-posts",
307 id: "1",
308 attributes: {
309 title: "Lorem",
310 },
311 relationships: {
312 "word-smith": {
313 data: {
314 id: "1",
315 type: "word-smiths",
316 },
317 },
318 },
319 },
320 included: [
321 {
322 attributes: {
323 "first-name": "Link",
324 },
325 id: "1",
326 type: "word-smiths",
327 },
328 ],
329 });
330 });
331
332 test(`it gracefully handles null belongs-to relationship`, () => {
333 server.config({
334 serializers: {
335 application: JSONAPISerializer,
336 blogPost: JSONAPISerializer.extend({
337 include: ["wordSmith"],
338 }),
339 },
340 });
341
342 let blogPost = server.schema.blogPosts.create({ title: "Lorem" });
343 let result = server.serializerOrRegistry.serialize(blogPost);
344
345 expect(result).toEqual({
346 data: {
347 type: "blog-posts",
348 id: "1",
349 attributes: {
350 title: "Lorem",
351 },
352 relationships: {
353 "word-smith": {
354 data: null,
355 },
356 },
357 },
358 });
359 });
360
361 test(`it can include a chain of belongs-to relationships`, () => {
362 server.config({
363 serializers: {
364 application: JSONAPISerializer,
365 blogPost: JSONAPISerializer.extend({
366 include: ["wordSmith"],
367 }),
368 fineComment: JSONAPISerializer.extend({
369 include: ["blogPost"],
370 }),
371 },
372 });
373
374 let wordSmith = server.schema.wordSmiths.create({ firstName: "Link" });
375 let post = wordSmith.createBlogPost({ title: "Lorem" });
376 let comment = post.createFineComment({ text: "pwned" });
377
378 let result = server.serializerOrRegistry.serialize(comment);
379
380 expect(result).toEqual({
381 data: {
382 type: "fine-comments",
383 id: "1",
384 attributes: {
385 text: "pwned",
386 },
387 relationships: {
388 "blog-post": {
389 data: {
390 id: "1",
391 type: "blog-posts",
392 },
393 },
394 },
395 },
396 included: [
397 {
398 type: "blog-posts",
399 id: "1",
400 attributes: {
401 title: "Lorem",
402 },
403 relationships: {
404 "word-smith": {
405 data: {
406 type: "word-smiths",
407 id: "1",
408 },
409 },
410 },
411 },
412 {
413 type: "word-smiths",
414 id: "1",
415 attributes: {
416 "first-name": "Link",
417 },
418 },
419 ],
420 });
421 });
422
423 test(`it properly serializes complex relationships`, () => {
424 server.config({
425 serializers: {
426 application: JSONAPISerializer,
427 wordSmith: JSONAPISerializer.extend({
428 include: ["blogPosts"],
429 }),
430 blogPost: JSONAPISerializer.extend({
431 include: ["wordSmith", "fineComments"],
432 }),
433 fineComment: JSONAPISerializer.extend({
434 include: ["blogPost"],
435 }),
436 },
437 });
438
439 let wordSmith = server.schema.wordSmiths.create({ firstName: "Link" });
440 let post = wordSmith.createBlogPost({ title: "Lorem" });
441 wordSmith.createBlogPost({ title: "Ipsum" });
442 post.createFineComment({ text: "pwned" });
443
444 let result = server.serializerOrRegistry.serialize(wordSmith);
445
446 expect(result).toEqual({
447 data: {
448 attributes: {
449 "first-name": "Link",
450 },
451 id: "1",
452 relationships: {
453 "blog-posts": {
454 data: [
455 { type: "blog-posts", id: "1" },
456 { type: "blog-posts", id: "2" },
457 ],
458 },
459 },
460 type: "word-smiths",
461 },
462 included: [
463 {
464 type: "blog-posts",
465 id: "1",
466 attributes: {
467 title: "Lorem",
468 },
469 relationships: {
470 "word-smith": {
471 data: { type: "word-smiths", id: "1" },
472 },
473 "fine-comments": {
474 data: [{ type: "fine-comments", id: "1" }],
475 },
476 },
477 },
478 {
479 type: "fine-comments",
480 id: "1",
481 attributes: {
482 text: "pwned",
483 },
484 relationships: {
485 "blog-post": {
486 data: { type: "blog-posts", id: "1" },
487 },
488 },
489 },
490 {
491 type: "blog-posts",
492 id: "2",
493 attributes: {
494 title: "Ipsum",
495 },
496 relationships: {
497 "word-smith": {
498 data: { type: "word-smiths", id: "1" },
499 },
500 "fine-comments": {
501 data: [],
502 },
503 },
504 },
505 ],
506 });
507 });
508 });