Debian Codegraph @

__tests__/external/shared/serializers/active-model-serializer-test.js

raw
1 import {
2 Server,
3 ActiveModelSerializer,
4 Model,
5 hasMany,
6 belongsTo,
7 } from "miragejs";
8
9 describe("External | Shared | Serializers | ActiveModelSerializer", () => {
10 let server;
11
12 afterEach(function () {
13 server.shutdown();
14 });
15
16 describe("with serializeIds as included", () => {
17 beforeEach(function () {
18 server = new Server({
19 models: {
20 wordSmith: Model.extend({
21 blogPosts: hasMany(),
22 }),
23 blogPost: Model.extend({
24 wordSmith: belongsTo(),
25 comments: hasMany(),
26 }),
27 user: Model.extend({
28 contactInfos: hasMany(),
29 }),
30 contactInfo: Model.extend({
31 user: belongsTo(),
32 }),
33 comment: Model.extend({
34 commentable: belongsTo({ polymorphic: true }),
35 }),
36 team: Model.extend({
37 users: hasMany(),
38 comments: hasMany(),
39 }),
40 },
41 serializers: {
42 application: ActiveModelSerializer,
43 wordSmith: ActiveModelSerializer.extend({
44 serializeIds: "included",
45 attrs: ["id", "name"],
46 include: ["blogPosts"],
47 }),
48 blogPost: ActiveModelSerializer.extend({
49 serializeIds: "included",
50 include: ["wordSmith", "comments"],
51 }),
52 comment: ActiveModelSerializer.extend({
53 serializeIds: "included",
54 include: ["commentable"],
55 }),
56 contactInfo: ActiveModelSerializer.extend({
57 serializeIds: "included",
58 include: ["user"],
59 }),
60 user: ActiveModelSerializer.extend({
61 serializeIds: "included",
62 attrs: ["id", "name"],
63 include: ["contactInfos"],
64 embed: true,
65 }),
66 team: ActiveModelSerializer.extend({
67 serializeIds: "included",
68 attrs: ["id", "name"],
69 include: ["users", "comments"],
70 embed: (key) => key === "comments",
71 }),
72 },
73 });
74
75 let link = server.schema.wordSmiths.create({ name: "Link", age: 123 });
76 let post1 = link.createBlogPost({ title: "Lorem" });
77 link.createBlogPost({ title: "Ipsum" });
78
79 server.schema.wordSmiths.create({ name: "Zelda", age: 230 });
80
81 let user = server.schema.users.create({ name: "John Peach", age: 123 });
82 user.createContactInfo({ email: "[email protected]" });
83 user.createContactInfo({ email: "[email protected]" });
84
85 server.schema.users.create({ name: "Pine Apple", age: 230 });
86 server.schema.comments.create({ text: "Hi there", commentable: post1 });
87
88 let team = server.schema.teams.create({
89 name: "Blue Team",
90 users: [user],
91 });
92 server.schema.comments.create({
93 text: "Blue team, go!",
94 commentable: team,
95 });
96 });
97
98 test("it sideloads associations and snake-cases relationships and attributes correctly for a model", () => {
99 let link = server.schema.wordSmiths.find(1);
100 let result = server.serializerOrRegistry.serialize(link);
101
102 expect(result).toEqual({
103 word_smith: {
104 id: "1",
105 name: "Link",
106 blog_post_ids: ["1", "2"],
107 },
108 blog_posts: [
109 {
110 id: "1",
111 title: "Lorem",
112 word_smith_id: "1",
113 comment_ids: ["1"],
114 },
115 {
116 id: "2",
117 title: "Ipsum",
118 word_smith_id: "1",
119 comment_ids: [],
120 },
121 ],
122 comments: [
123 {
124 id: "1",
125 text: "Hi there",
126 commentable_id: "1",
127 commentable_type: "blog-post",
128 },
129 ],
130 });
131 });
132
133 test("it sideloads associations and snake-cases relationships and attributes correctly for a collection", () => {
134 let wordSmiths = server.schema.wordSmiths.all();
135 let result = server.serializerOrRegistry.serialize(wordSmiths);
136
137 expect(result).toEqual({
138 word_smiths: [
139 {
140 id: "1",
141 name: "Link",
142 blog_post_ids: ["1", "2"],
143 },
144 {
145 id: "2",
146 name: "Zelda",
147 blog_post_ids: [],
148 },
149 ],
150 blog_posts: [
151 {
152 id: "1",
153 title: "Lorem",
154 word_smith_id: "1",
155 comment_ids: ["1"],
156 },
157 {
158 id: "2",
159 title: "Ipsum",
160 word_smith_id: "1",
161 comment_ids: [],
162 },
163 ],
164 comments: [
165 {
166 id: "1",
167 text: "Hi there",
168 commentable_id: "1",
169 commentable_type: "blog-post",
170 },
171 ],
172 });
173 });
174
175 test("it embeds all associations and snake-cases relationships and attributes correctly for a collection", () => {
176 let users = server.schema.users.all();
177 let result = server.serializerOrRegistry.serialize(users);
178
179 expect(result).toEqual({
180 users: [
181 {
182 id: "1",
183 name: "John Peach",
184 contact_infos: [
185 {
186 id: "1",
187 email: "[email protected]",
188 user_id: "1",
189 },
190 {
191 id: "2",
192 email: "[email protected]",
193 user_id: "1",
194 },
195 ],
196 },
197 {
198 id: "2",
199 name: "Pine Apple",
200 contact_infos: [],
201 },
202 ],
203 });
204 });
205
206 test("it embeds some associations and snake-cases relationships and attributes correctly for a collection", () => {
207 let teams = server.schema.teams.all();
208 let result = server.serializerOrRegistry.serialize(teams);
209
210 expect(result).toEqual({
211 teams: [
212 {
213 id: "1",
214 name: "Blue Team",
215 comments: [
216 {
217 text: "Blue team, go!",
218 id: "2",
219 commentable_type: "team",
220 commentable_id: "1",
221 },
222 ],
223 user_ids: ["1"],
224 },
225 ],
226 users: [
227 {
228 id: "1",
229 name: "John Peach",
230 contact_infos: [
231 { email: "[email protected]", id: "1", user_id: "1" },
232 { email: "[email protected]", id: "2", user_id: "1" },
233 ],
234 },
235 ],
236 });
237 });
238 });
239
240 describe("with serializeIds as always", () => {
241 test("it correctly serializes a Collection for a model that has a polymorphic relationship", () => {
242 server = new Server({
243 models: {
244 post: Model,
245 comment: Model.extend({
246 commentable: belongsTo({ polymorphic: true }),
247 }),
248 },
249
250 serializers: {
251 application: ActiveModelSerializer.extend({
252 serializeIds: "always",
253 }),
254 },
255 });
256
257 server.schema.comments.create({
258 commentable: server.schema.posts.create(),
259 });
260
261 let serializedComments = server.serializerOrRegistry.serialize(
262 server.schema.comments.all()
263 );
264
265 expect(serializedComments).toEqual({
266 comments: [
267 {
268 id: "1",
269 commentable_id: "1",
270 commentable_type: "post",
271 },
272 ],
273 });
274 });
275 });
276 });