Debian Codegraph @

__tests__/internal/move-after-handle-request/route-handlers/function-handler/normalize-request-attrs-test.js

raw
1 import { Server, Model, ActiveModelSerializer } from "miragejs";
2
3 describe("Integration | Route handlers | Function handler | #normalizedRequestAttrs", () => {
4 let server;
5 beforeEach(() => {
6 server = new Server({
7 environment: "development",
8 models: {
9 user: Model.extend({}),
10 fineComment: Model.extend({}),
11 },
12 serializers: {
13 application: ActiveModelSerializer,
14 },
15 });
16 server.timing = 0;
17 server.logging = false;
18 });
19
20 afterEach(() => {
21 server.shutdown();
22 });
23
24 test(`it returns an object with the primary resource's attrs and belongsTo keys camelized`, async () => {
25 expect.assertions(1);
26
27 server.post("/users", function () {
28 let attrs = this.normalizedRequestAttrs();
29
30 expect(attrs).toEqual({
31 firstName: "Sam",
32 lastName: "Selikoff",
33 teamId: 1,
34 });
35
36 return {};
37 });
38
39 await fetch("/users", {
40 method: "POST",
41 headers: {
42 "Content-Type": "application/json",
43 },
44 body: JSON.stringify({
45 user: {
46 first_name: "Sam",
47 last_name: "Selikoff",
48 team_id: 1,
49 },
50 }),
51 });
52 });
53
54 test(`it works for compound names`, async () => {
55 expect.assertions(1);
56
57 server.post("/fine-comments", function () {
58 let attrs = this.normalizedRequestAttrs();
59
60 expect(attrs).toEqual({
61 shortText: "lorem ipsum",
62 });
63
64 return {};
65 });
66
67 await fetch("/fine-comments", {
68 method: "POST",
69 headers: {
70 "Content-Type": "application/json",
71 },
72 body: JSON.stringify({
73 fine_comment: {
74 short_text: "lorem ipsum",
75 },
76 }),
77 });
78 });
79
80 test(`it shows a meaningful error message if it cannot infer the modelname from the URL`, async () => {
81 expect.assertions(2);
82
83 server.post("/users/create", function () {
84 this.normalizedRequestAttrs();
85 });
86
87 let res = await fetch("/users/create", { method: "POST" });
88 let data = await res.json();
89
90 expect(res.status).toEqual(500);
91 expect(data.message).toMatch(
92 "the detected model of 'create' does not exist"
93 );
94 });
95
96 test(`it accepts an optional modelName if it cannot be inferred from the path`, async () => {
97 expect.assertions(1);
98
99 server.post("/users/create", function () {
100 let attrs = this.normalizedRequestAttrs("user");
101
102 expect(attrs).toEqual({
103 firstName: "Sam",
104 lastName: "Selikoff",
105 teamId: 1,
106 });
107
108 return {};
109 });
110
111 await fetch("/users/create", {
112 method: "POST",
113 headers: {
114 "Content-Type": "application/json",
115 },
116 body: JSON.stringify({
117 user: {
118 first_name: "Sam",
119 last_name: "Selikoff",
120 team_id: 1,
121 },
122 }),
123 });
124 });
125
126 test(`it errors if the optional parameter is camelized for a model with a compount name`, async () => {
127 expect.assertions(2);
128
129 server.post("/fine-comments/create", function () {
130 this.normalizedRequestAttrs("fineComment");
131 });
132
133 let res = await fetch("/fine-comments/create", { method: "POST" });
134 let data = await res.json();
135
136 expect(res.status).toEqual(500);
137 expect(data.message).toMatch(
138 "You called normalizedRequestAttrs('fineComment'), but normalizedRequestAttrs was intended to be used with the dasherized version of the model type. Please change this to normalizedRequestAttrs('fine-comment')"
139 );
140 });
141
142 test(`it works with a form encoded request that has a lower-case content-type (issue 1398)`, async () => {
143 expect.assertions(1);
144
145 server.post("/form-test", function () {
146 let attrs = this.normalizedRequestAttrs("user");
147
148 expect(attrs).toEqual({
149 name: "Sam Selikoff",
150 company: "TED",
151 email: "[email protected]",
152 });
153
154 return {};
155 });
156
157 await fetch("/form-test", {
158 method: "POST",
159 headers: {
160 "content-type": "x-www-form-urlencoded",
161 },
162 body: "name=Sam+Selikoff&company=TED&[email protected]",
163 });
164 });
165 });