Debian Codegraph @

__tests__/internal/move-after-handle-request/route-handlers/put-shorthand-test.js

raw
1 import { Server, Model, JSONAPISerializer } from "miragejs";
2 import PutShorthandRouteHandler from "@lib/route-handlers/shorthands/put";
3
4 describe("Integration | Route Handlers | PUT shorthand", () => {
5 let server, authors, schema, serializer, body;
6
7 beforeEach(() => {
8 server = new Server({
9 environment: "development",
10 models: {
11 author: Model.extend(),
12 },
13 });
14 server.timing = 0;
15 server.logging = false;
16
17 authors = [{ id: 1, firstName: "Ganon" }];
18 server.db.loadData({
19 authors: authors,
20 });
21
22 schema = server.schema;
23 serializer = new JSONAPISerializer();
24
25 body = {
26 data: {
27 type: "authors",
28 id: "1",
29 attributes: {
30 "first-name": "Ganondorf",
31 },
32 },
33 };
34 });
35
36 afterEach(() => {
37 server.shutdown();
38 });
39
40 test("undefined shorthand updates the record and returns the model", () => {
41 let handler = new PutShorthandRouteHandler(
42 schema,
43 serializer,
44 undefined,
45 "/authors/:id"
46 );
47 let request = {
48 requestBody: JSON.stringify(body),
49 url: "/authors/1",
50 params: { id: "1" },
51 };
52
53 let model = handler.handle(request);
54
55 expect(schema.db.authors).toHaveLength(1);
56 expect(model instanceof Model).toBeTruthy();
57 expect(model.modelName).toEqual("author");
58 expect(model.firstName).toEqual("Ganondorf");
59 });
60
61 test("query params are ignored", () => {
62 let handler = new PutShorthandRouteHandler(schema, serializer, "author");
63 let request = {
64 requestBody: JSON.stringify(body),
65 url: "/authors/1?foo=bar",
66 params: { id: "1" },
67 queryParams: { foo: "bar" },
68 };
69
70 let model = handler.handle(request);
71
72 expect(schema.db.authors).toHaveLength(1);
73 expect(model instanceof Model).toBeTruthy();
74 expect(model.modelName).toEqual("author");
75 expect(model.firstName).toEqual("Ganondorf");
76 });
77
78 test("string shorthand updates the record of the specified type and returns the model", () => {
79 let handler = new PutShorthandRouteHandler(
80 schema,
81 serializer,
82 undefined,
83 "/authors/:id"
84 );
85 let request = {
86 requestBody: JSON.stringify(body),
87 url: "/authors/1",
88 params: { id: "1" },
89 };
90
91 let model = handler.handle(request);
92
93 expect(schema.db.authors).toHaveLength(1);
94 expect(model instanceof Model).toBeTruthy();
95 expect(model.modelName).toEqual("author");
96 expect(model.firstName).toEqual("Ganondorf");
97 });
98
99 test("if a shorthand tries to access an unknown type it throws an error", () => {
100 let handler = new PutShorthandRouteHandler(
101 schema,
102 serializer,
103 undefined,
104 "/foobars/:id"
105 );
106 let request = {
107 requestBody: JSON.stringify(body),
108 url: "/foobars/1",
109 params: { id: "1" },
110 };
111
112 expect(function () {
113 handler.handle(request);
114 }).toThrow();
115 expect(true).toBeTruthy();
116 });
117 });