Debian Codegraph @

__tests__/internal/move-after-handle-request/customized-normalize-method-test.js

raw
1 import { Server, Model, ActiveModelSerializer } from "miragejs";
2 import { camelize } from "@lib/utils/inflector";
3
4 describe("Integration | Server | Customized normalize method", function () {
5 let server;
6
7 beforeEach(function () {
8 server = new Server({
9 environment: "test",
10 models: {
11 contact: Model,
12 },
13 serializers: {
14 application: ActiveModelSerializer,
15 contact: ActiveModelSerializer.extend({
16 normalize(payload) {
17 let attrs = payload.some.random[1].attrs;
18 Object.keys(attrs).forEach(camelize);
19
20 let jsonApiDoc = {
21 data: {
22 type: "contacts",
23 attributes: attrs,
24 },
25 };
26 return jsonApiDoc;
27 },
28 }),
29 },
30 });
31 server.timing = 0;
32 server.logging = false;
33 });
34
35 afterEach(function () {
36 server.shutdown();
37 });
38
39 test("custom model-specific normalize functions are used", async () => {
40 expect.assertions(3);
41
42 server.post("/contacts");
43
44 let res = await fetch("/contacts", {
45 method: "POST",
46 body: JSON.stringify({
47 some: {
48 random: [
49 {
50 format: true,
51 },
52 {
53 attrs: {
54 first_name: "Zelda",
55 },
56 },
57 ],
58 },
59 }),
60 });
61
62 expect(res.status).toEqual(201);
63 expect(server.db.contacts).toHaveLength(1);
64 expect(server.db.contacts[0].firstName).toEqual("Zelda");
65 });
66
67 test("custom model-specific normalize functions are used with custom function handlers", async () => {
68 server.put("/contacts/:id", function (schema, request) {
69 let attrs = this.normalizedRequestAttrs();
70
71 expect(attrs).toEqual({
72 id: "1",
73 firstName: "Zelda",
74 });
75
76 return {};
77 });
78
79 await fetch("/contacts/1", {
80 method: "PUT",
81 headers: {
82 "Content-Type": "application/json",
83 },
84 body: JSON.stringify({
85 some: {
86 random: [
87 {
88 format: true,
89 },
90 {
91 attrs: {
92 first_name: "Zelda",
93 },
94 },
95 ],
96 },
97 }),
98 });
99 });
100 });