Debian Codegraph @

__tests__/external/shared/serializers/base/associations/embedded-collection-test.js

raw
1 import { Server, Model, hasMany, belongsTo, Serializer } from "miragejs";
2
3 describe("External | Shared | Serializers | Base | Associations | Embedded Collections", function () {
4 let server, BaseSerializer;
5
6 beforeEach(function () {
7 server = new Server({
8 models: {
9 wordSmith: Model.extend({
10 posts: hasMany("blogPost", { inverse: "author" }),
11 }),
12 blogPost: Model.extend({
13 author: belongsTo("wordSmith", { inverse: "posts" }),
14 comments: hasMany("fineComment", { inverse: "post" }),
15 }),
16 fineComment: Model.extend({
17 post: belongsTo("blogPost"),
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: true,
32 });
33 });
34
35 afterEach(function () {
36 server.shutdown();
37 });
38
39 test(`it can embed a collection with a has-many relationship`, () => {
40 server.config({
41 serializers: {
42 application: BaseSerializer,
43 wordSmith: BaseSerializer.extend({
44 include: ["posts"],
45 }),
46 },
47 });
48
49 let wordSmiths = server.schema.wordSmiths.all();
50 let result = server.serializerOrRegistry.serialize(wordSmiths);
51
52 expect(result).toEqual({
53 wordSmiths: [
54 {
55 id: "1",
56 name: "Link",
57 posts: [
58 { id: "1", title: "Lorem" },
59 { id: "2", title: "Ipsum" },
60 ],
61 },
62 {
63 id: "2",
64 name: "Zelda",
65 posts: [],
66 },
67 ],
68 });
69 });
70
71 test(`it can embed a collection with a chain of has-many relationships`, () => {
72 server.config({
73 serializers: {
74 application: BaseSerializer,
75 wordSmith: BaseSerializer.extend({
76 include: ["posts"],
77 }),
78 blogPost: BaseSerializer.extend({
79 include: ["comments"],
80 }),
81 },
82 });
83
84 let wordSmiths = server.schema.wordSmiths.all();
85 let result = server.serializerOrRegistry.serialize(wordSmiths);
86
87 expect(result).toEqual({
88 wordSmiths: [
89 {
90 id: "1",
91 name: "Link",
92 posts: [
93 {
94 id: "1",
95 title: "Lorem",
96 comments: [{ id: "1", text: "pwned" }],
97 },
98 {
99 id: "2",
100 title: "Ipsum",
101 comments: [],
102 },
103 ],
104 },
105 {
106 id: "2",
107 name: "Zelda",
108 posts: [],
109 },
110 ],
111 });
112 });
113
114 test(`it can embed a collection with a belongs-to relationship`, () => {
115 server.config({
116 serializers: {
117 application: BaseSerializer,
118 blogPost: BaseSerializer.extend({
119 include: ["author"],
120 }),
121 },
122 });
123
124 let blogPosts = server.schema.blogPosts.all();
125 let result = server.serializerOrRegistry.serialize(blogPosts);
126
127 expect(result).toEqual({
128 blogPosts: [
129 {
130 id: "1",
131 title: "Lorem",
132 author: { id: "1", name: "Link" },
133 },
134 {
135 id: "2",
136 title: "Ipsum",
137 author: { id: "1", name: "Link" },
138 },
139 ],
140 });
141 });
142
143 test(`it can embed a collection with a chain of belongs-to relationships`, () => {
144 server.config({
145 serializers: {
146 application: BaseSerializer,
147 fineComment: BaseSerializer.extend({
148 include: ["post"],
149 }),
150 blogPost: BaseSerializer.extend({
151 include: ["author"],
152 }),
153 },
154 });
155
156 let fineComments = server.schema.fineComments.all();
157 let result = server.serializerOrRegistry.serialize(fineComments);
158
159 expect(result).toEqual({
160 fineComments: [
161 {
162 id: "1",
163 text: "pwned",
164 post: {
165 id: "1",
166 title: "Lorem",
167 author: { id: "1", name: "Link" },
168 },
169 },
170 ],
171 });
172 });
173 });