Debian Codegraph @

__tests__/internal/move-after-handle-request/shorthands/post-shorthand-with-relationships-test.js

raw
1 import { Server, JSONAPISerializer, Model, belongsTo, hasMany } from "miragejs";
2
3 describe("Integration | Server | Shorthands | Post with relationships", function () {
4 let newServerWithSchema, server;
5
6 beforeEach(function () {
7 newServerWithSchema = function (schema) {
8 server = new Server({
9 environment: "development",
10 models: schema,
11 serializers: {
12 appliction: JSONAPISerializer,
13 },
14 });
15 server.timing = 0;
16 server.logging = false;
17
18 return server;
19 };
20 });
21
22 afterEach(function () {
23 server.shutdown();
24 });
25
26 test("it works for belongs to", async () => {
27 let server = newServerWithSchema({
28 author: Model.extend({
29 posts: hasMany(),
30 }),
31 post: Model.extend({
32 author: belongsTo(),
33 }),
34 });
35 server.post("/posts");
36
37 expect(server.db.posts).toHaveLength(0);
38
39 let author = server.create("author");
40
41 let res = await fetch("/posts", {
42 method: "POST",
43 body: JSON.stringify({
44 data: {
45 attributes: {
46 title: "Post 1",
47 },
48 relationships: {
49 author: {
50 data: {
51 type: "authors",
52 id: author.id,
53 },
54 },
55 },
56 },
57 }),
58 });
59 let data = await res.json();
60
61 let postId = data.post.id;
62 let post = server.schema.posts.find(postId);
63
64 expect(post).toBeTruthy();
65 expect(post.author.id).toEqual(author.id);
66 });
67
68 test("it works for belongs to polymorphic", async () => {
69 let server = newServerWithSchema({
70 video: Model.extend(),
71 post: Model.extend(),
72 comment: Model.extend({
73 commentable: belongsTo({ polymorphic: true }),
74 }),
75 });
76 server.post("/comments");
77
78 expect(server.db.comments).toHaveLength(0);
79
80 let video = server.create("video");
81
82 let res = await fetch("/comments", {
83 method: "POST",
84 body: JSON.stringify({
85 data: {
86 type: "comments",
87 attributes: {
88 text: "Comment 1",
89 },
90 relationships: {
91 commentable: {
92 data: {
93 type: "videos",
94 id: video.id,
95 },
96 },
97 },
98 },
99 }),
100 });
101 let data = await res.json();
102
103 let commentId = data.comment.id;
104 let comment = server.schema.comments.find(commentId);
105
106 expect(comment).toBeTruthy();
107 expect(comment.commentable.equals(video)).toBeTruthy();
108 });
109
110 test("it works for has many polymorphic", async () => {
111 let server = newServerWithSchema({
112 car: Model.extend(),
113 watch: Model.extend(),
114 user: Model.extend({
115 collectibles: hasMany({ polymorphic: true }),
116 }),
117 });
118
119 server.post("/users");
120
121 expect(server.db.users).toHaveLength(0);
122
123 let car = server.create("car");
124 let watch = server.create("watch");
125
126 let res = await fetch("/users", {
127 method: "POST",
128 body: JSON.stringify({
129 data: {
130 type: "users",
131 attributes: {
132 name: "Elon Musk",
133 },
134 relationships: {
135 collectibles: {
136 data: [
137 {
138 type: "cars",
139 id: car.id,
140 },
141 {
142 type: "watches",
143 id: watch.id,
144 },
145 ],
146 },
147 },
148 },
149 }),
150 });
151
152 let data = await res.json();
153
154 let userId = data.user.id;
155 let user = server.schema.users.find(userId);
156
157 expect(user).toBeTruthy();
158 expect(user.collectibles.includes(car)).toBeTruthy();
159 expect(user.collectibles.includes(watch)).toBeTruthy();
160 });
161 });