Debian Codegraph @

__tests__/external/shared/serializers/base/associations/sideloading-model-test.js

raw
1 import { Server, Model, hasMany, belongsTo, Serializer } from "miragejs";
2
3 describe("External | Shared | Serializers | Base | Associations | Sideloading Models", function () {
4 let server, BaseSerializer;
5
6 beforeEach(function () {
7 server = new Server({
8 models: {
9 wordSmith: Model.extend({
10 posts: hasMany("blog-post"),
11 }),
12 blogPost: Model.extend({
13 author: belongsTo("word-smith"),
14 comments: hasMany("fine-comment"),
15 }),
16 fineComment: Model.extend({
17 post: belongsTo("blog-post"),
18 }),
19 },
20 });
21
22 let wordSmith = server.schema.wordSmiths.create({ name: "Link" });
23 let blogPost = wordSmith.createPost({ title: "Lorem" });
24 blogPost.createComment({ text: "pwned" });
25
26 wordSmith.createPost({ title: "Ipsum" });
27
28 server.schema.wordSmiths.create({ name: "Zelda" });
29
30 BaseSerializer = Serializer.extend({
31 embed: false,
32 });
33 });
34
35 afterEach(function () {
36 server.shutdown();
37 });
38
39 test(`it throws an error if embed is false and root is false`, () => {
40 server.config({
41 serializers: {
42 wordSmith: BaseSerializer.extend({
43 root: false,
44 include: ["posts"],
45 }),
46 },
47 });
48
49 let link = server.schema.wordSmiths.find(1);
50 expect(function () {
51 server.serializerOrRegistry.serialize(link);
52 }).toThrow();
53 });
54
55 test(`it can sideload a model with a has-many relationship`, () => {
56 server.config({
57 serializers: {
58 application: BaseSerializer,
59 wordSmith: BaseSerializer.extend({
60 include: ["posts"],
61 }),
62 },
63 });
64
65 let link = server.schema.wordSmiths.find(1);
66 let result = server.serializerOrRegistry.serialize(link);
67
68 expect(result).toEqual({
69 wordSmith: {
70 id: "1",
71 name: "Link",
72 postIds: ["1", "2"],
73 },
74 blogPosts: [
75 { id: "1", title: "Lorem" },
76 { id: "2", title: "Ipsum" },
77 ],
78 });
79 });
80
81 test(`it can sideload a model with a chain of has-many relationships`, () => {
82 server.config({
83 serializers: {
84 application: BaseSerializer,
85 wordSmith: BaseSerializer.extend({
86 include: ["posts"],
87 }),
88 blogPost: BaseSerializer.extend({
89 include: ["comments"],
90 }),
91 },
92 });
93
94 let link = server.schema.wordSmiths.find(1);
95 let result = server.serializerOrRegistry.serialize(link);
96
97 expect(result).toEqual({
98 wordSmith: {
99 id: "1",
100 name: "Link",
101 postIds: ["1", "2"],
102 },
103 blogPosts: [
104 { id: "1", title: "Lorem", commentIds: ["1"] },
105 { id: "2", title: "Ipsum", commentIds: [] },
106 ],
107 fineComments: [{ id: "1", text: "pwned" }],
108 });
109 });
110
111 test(`it avoids circularity when serializing a model`, () => {
112 server.config({
113 serializers: {
114 application: BaseSerializer,
115 wordSmith: BaseSerializer.extend({
116 include: ["posts"],
117 }),
118 blogPost: BaseSerializer.extend({
119 include: ["author"],
120 }),
121 },
122 });
123
124 let link = server.schema.wordSmiths.find(1);
125 let result = server.serializerOrRegistry.serialize(link);
126
127 expect(result).toEqual({
128 wordSmith: {
129 id: "1",
130 name: "Link",
131 postIds: ["1", "2"],
132 },
133 blogPosts: [
134 { id: "1", title: "Lorem", authorId: "1" },
135 { id: "2", title: "Ipsum", authorId: "1" },
136 ],
137 });
138 });
139
140 test(`it can sideload a model with a belongs-to relationship`, () => {
141 server.config({
142 serializers: {
143 application: BaseSerializer,
144 blogPost: BaseSerializer.extend({
145 include: ["author"],
146 }),
147 },
148 });
149
150 let blogPost = server.schema.blogPosts.find(1);
151 let result = server.serializerOrRegistry.serialize(blogPost);
152
153 expect(result).toEqual({
154 blogPost: {
155 id: "1",
156 title: "Lorem",
157 authorId: "1",
158 },
159 wordSmiths: [{ id: "1", name: "Link" }],
160 });
161 });
162
163 test(`it can sideload a model with a chain of belongs-to relationships`, () => {
164 server.config({
165 serializers: {
166 application: BaseSerializer,
167 fineComment: BaseSerializer.extend({
168 include: ["post"],
169 }),
170 blogPost: BaseSerializer.extend({
171 include: ["author"],
172 }),
173 },
174 });
175
176 let fineComment = server.schema.fineComments.find(1);
177 let result = server.serializerOrRegistry.serialize(fineComment);
178
179 expect(result).toEqual({
180 fineComment: {
181 id: "1",
182 text: "pwned",
183 postId: "1",
184 },
185 blogPosts: [{ id: "1", title: "Lorem", authorId: "1" }],
186 wordSmiths: [{ id: "1", name: "Link" }],
187 });
188 });
189 });