Debian Codegraph @

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

raw
1 import { Server, Model, hasMany, belongsTo, JSONAPISerializer } from "miragejs";
2
3 describe("External | Shared | Serializers | JSON API Serializer | Associations | Links", () => {
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(`it supports links`, () => {
28 server.config({
29 serializers: {
30 application: JSONAPISerializer,
31 blogPost: JSONAPISerializer.extend({
32 links(model) {
33 return {
34 wordSmith: {
35 related: `/api/word_smiths/${model.wordSmith.id}`,
36 self: `/api/blog_posts/${model.id}/relationships/word_smith`,
37 },
38 fineComments: {
39 related: `/api/fine_comments?blog_post_id=${model.id}`,
40 self: `/api/blog_posts/${model.id}/relationships/fine_comments`,
41 },
42 };
43 },
44 }),
45 },
46 });
47
48 let link = server.schema.wordSmiths.create({ id: 3, name: "Link" }); // specify id to really test our links function
49 let blogPost = link.createBlogPost({ title: "Lorem ipsum" });
50
51 let result = server.serializerOrRegistry.serialize(blogPost);
52
53 expect(result).toEqual({
54 data: {
55 type: "blog-posts",
56 id: blogPost.id,
57 attributes: {
58 title: "Lorem ipsum",
59 },
60 relationships: {
61 "word-smith": {
62 links: {
63 related: `/api/word_smiths/${link.id}`,
64 self: `/api/blog_posts/${blogPost.id}/relationships/word_smith`,
65 },
66 },
67 "fine-comments": {
68 links: {
69 related: `/api/fine_comments?blog_post_id=${blogPost.id}`,
70 self: `/api/blog_posts/${blogPost.id}/relationships/fine_comments`,
71 },
72 },
73 },
74 },
75 });
76 });
77
78 test(`it supports links alongside data linkage`, () => {
79 let ApplicationSerializer = JSONAPISerializer.extend({
80 alwaysIncludeLinkageData: true,
81 });
82 server.config({
83 serializers: {
84 application: ApplicationSerializer,
85 blogPost: ApplicationSerializer.extend({
86 links(model) {
87 return {
88 wordSmith: {
89 related: `/api/word_smiths/${model.wordSmith.id}`,
90 self: `/api/blog_posts/${model.id}/relationships/word_smith`,
91 },
92 fineComments: {
93 related: `/api/fine_comments?blog_post_id=${model.id}`,
94 self: `/api/blog_posts/${model.id}/relationships/fine_comments`,
95 },
96 };
97 },
98 }),
99 },
100 });
101
102 let link = server.schema.wordSmiths.create({ id: 3, name: "Link" }); // specify id to really test our links function
103 let blogPost = link.createBlogPost({ title: "Lorem ipsum" });
104
105 let result = server.serializerOrRegistry.serialize(blogPost);
106
107 expect(result).toEqual({
108 data: {
109 type: "blog-posts",
110 id: blogPost.id,
111 attributes: {
112 title: "Lorem ipsum",
113 },
114 relationships: {
115 "word-smith": {
116 data: {
117 id: "3",
118 type: "word-smiths",
119 },
120 links: {
121 related: `/api/word_smiths/${link.id}`,
122 self: `/api/blog_posts/${blogPost.id}/relationships/word_smith`,
123 },
124 },
125 "fine-comments": {
126 data: [],
127 links: {
128 related: `/api/fine_comments?blog_post_id=${blogPost.id}`,
129 self: `/api/blog_posts/${blogPost.id}/relationships/fine_comments`,
130 },
131 },
132 },
133 },
134 });
135 });
136 });