Debian Codegraph @

__tests__/internal/move-after-handle-request/shorthands/rest-serializer-sanity-test.js

raw
1 import { Server, Model, hasMany, belongsTo, RestSerializer } from "miragejs";
2
3 describe("Integration | Server | Shorthands | REST Serializer Sanity check", function () {
4 let server;
5
6 beforeEach(function () {
7 server = new Server({
8 environment: "test",
9 models: {
10 contact: Model.extend({
11 addresses: hasMany(),
12 }),
13 address: Model.extend({
14 contact: belongsTo(),
15 }),
16 },
17 serializers: {
18 application: RestSerializer.extend({
19 normalizeIds: true,
20 }),
21 },
22 });
23 server.timing = 0;
24 server.logging = false;
25 });
26
27 afterEach(function () {
28 server.shutdown();
29 });
30
31 test("a get shorthand works", async () => {
32 expect.assertions(2);
33
34 server.db.loadData({
35 contacts: [{ id: 1, name: "Link" }],
36 });
37
38 server.get("/contacts");
39
40 let res = await fetch("/contacts");
41 let data = await res.json();
42
43 expect(res.status).toEqual(200);
44 expect(data).toEqual({
45 contacts: [{ id: "1", name: "Link", addresses: [] }],
46 });
47 });
48
49 test("a post shorthand works", async () => {
50 expect.assertions(3);
51
52 server.db.loadData({
53 contacts: [{ id: 1, name: "Link" }],
54 });
55
56 server.post("/addresses");
57
58 let res = await fetch("/addresses", {
59 method: "POST",
60 body: JSON.stringify({
61 address: {
62 street: "5th ave",
63 contact: 1,
64 },
65 }),
66 });
67
68 expect(res.status).toEqual(201);
69 expect(server.db.addresses).toHaveLength(1);
70 expect(server.db.addresses[0].contactId).toEqual(1);
71 });
72
73 test("a put shorthand works", async () => {
74 expect.assertions(2);
75
76 server.db.loadData({
77 contacts: [{ id: 1, name: "Link" }],
78 });
79
80 server.put("/contacts/:id");
81
82 let res = await fetch("/contacts/1", {
83 method: "PUT",
84 body: JSON.stringify({
85 contact: {
86 name: "Zelda",
87 },
88 }),
89 });
90
91 expect(res.status).toEqual(200);
92 expect(server.db.contacts[0].name).toEqual("Zelda");
93 });
94
95 test("a patch shorthand works", async () => {
96 expect.assertions(2);
97
98 server.db.loadData({
99 contacts: [{ id: 1, name: "Link" }],
100 });
101
102 server.patch("/contacts/:id");
103
104 let res = await fetch("/contacts/1", {
105 method: "PATCH",
106 body: JSON.stringify({
107 contact: {
108 name: "Zelda",
109 },
110 }),
111 });
112
113 expect(res.status).toEqual(200);
114 expect(server.db.contacts[0].name).toEqual("Zelda");
115 });
116
117 test("a delete shorthand works", async () => {
118 expect.assertions(2);
119
120 server.db.loadData({
121 contacts: [{ id: 1, name: "Link" }],
122 });
123
124 server.del("/contacts/:id");
125
126 let res = await fetch("/contacts/1", {
127 method: "DELETE",
128 });
129
130 expect(res.status).toEqual(204);
131 expect(server.db.contacts).toHaveLength(0);
132 });
133 });