Debian Codegraph @

__tests__/internal/move-after-handle-request/serializers/includes-test.js

raw
1 import { Server, Model, hasMany, belongsTo, Serializer } from "miragejs";
2
3 describe("Internal | MoveAfterHandleRequest | Serializers | includes", function () {
4 let server;
5
6 beforeEach(function () {
7 server = new Server({
8 environment: "test",
9 models: {
10 wordSmith: Model.extend({
11 posts: hasMany("blog-post"),
12 }),
13 blogPost: Model.extend({
14 author: belongsTo("word-smith"),
15 comments: hasMany("fine-comment"),
16 }),
17 fineComment: Model.extend({
18 post: belongsTo("blog-post"),
19 }),
20 },
21 routes() {
22 this.resource("blog-posts");
23 },
24 });
25
26 let wordSmith = server.schema.wordSmiths.create({ name: "Link" });
27 let blogPost = wordSmith.createPost({ title: "Lorem" });
28 blogPost.createComment({ text: "pwned" });
29 });
30
31 afterEach(function () {
32 server.shutdown();
33 });
34
35 test("include can be an array of relationship names", async () => {
36 server.config({
37 serializers: {
38 blogPost: Serializer.extend({
39 include: ["author", "comments"],
40 }),
41 },
42 });
43
44 let json = await fetch("/blog-posts/1").then((res) => res.json());
45
46 expect(json).toEqual({
47 blogPost: {
48 authorId: "1",
49 commentIds: ["1"],
50 id: "1",
51 title: "Lorem",
52 },
53 fineComments: [
54 {
55 id: "1",
56 text: "pwned",
57 },
58 ],
59 wordSmiths: [
60 {
61 id: "1",
62 name: "Link",
63 },
64 ],
65 });
66 });
67
68 test("include can be a function", async () => {
69 server.config({
70 serializers: {
71 blogPost: Serializer.extend({
72 include() {
73 return ["comments"];
74 },
75 }),
76 },
77 });
78
79 let json = await fetch("/blog-posts/1").then((res) => res.json());
80
81 expect(json).toEqual({
82 blogPost: {
83 commentIds: ["1"],
84 id: "1",
85 title: "Lorem",
86 },
87 fineComments: [
88 {
89 id: "1",
90 text: "pwned",
91 },
92 ],
93 });
94 });
95
96 test("include gets the request as the first param", async () => {
97 server.config({
98 serializers: {
99 blogPost: Serializer.extend({
100 include(request) {
101 return request.queryParams.include ? ["comments"] : [];
102 },
103 }),
104 },
105 });
106
107 let json = await fetch("/blog-posts/1?include").then((res) => res.json());
108
109 expect(json).toEqual({
110 blogPost: {
111 commentIds: ["1"],
112 id: "1",
113 title: "Lorem",
114 },
115 fineComments: [
116 {
117 id: "1",
118 text: "pwned",
119 },
120 ],
121 });
122 });
123
124 test("include gets the model as the second param", async () => {
125 server.config({
126 serializers: {
127 blogPost: Serializer.extend({
128 include(request, model) {
129 return Object.keys(model.associations);
130 },
131 }),
132 },
133 });
134
135 let json = await fetch("/blog-posts/1?include").then((res) => res.json());
136
137 expect(json).toEqual({
138 blogPost: {
139 authorId: "1",
140 commentIds: ["1"],
141 id: "1",
142 title: "Lorem",
143 },
144 fineComments: [
145 {
146 id: "1",
147 text: "pwned",
148 },
149 ],
150 wordSmiths: [
151 {
152 id: "1",
153 name: "Link",
154 },
155 ],
156 });
157 });
158
159 test("serializer.schema is exposed", async () => {
160 server.config({
161 serializers: {
162 blogPost: Serializer.extend({
163 include(request, resource) {
164 return Object.keys(this.schema.associationsFor(resource.modelName));
165 },
166 }),
167 },
168 });
169
170 let json = await fetch("/blog-posts/1?include").then((res) => res.json());
171
172 expect(json).toEqual({
173 blogPost: {
174 authorId: "1",
175 commentIds: ["1"],
176 id: "1",
177 title: "Lorem",
178 },
179 fineComments: [
180 {
181 id: "1",
182 text: "pwned",
183 },
184 ],
185 wordSmiths: [
186 {
187 id: "1",
188 name: "Link",
189 },
190 ],
191 });
192 });
193 });