Debian Codegraph @

__tests__/external/shared/serializers/json-api-serializer/key-formatting-test.js

raw
1 import { Server, Model, JSONAPISerializer } from "miragejs";
2 import snakeCase from "lodash.snakecase";
3
4 describe("External | Shared | Serializers | JSON API Serializer | Key Formatting", () => {
5 let server;
6
7 beforeEach(() => {
8 server = new Server({
9 models: {
10 wordSmith: Model,
11 photograph: Model,
12 },
13 });
14 });
15
16 afterEach(() => {
17 server.shutdown();
18 });
19
20 test(`keyForAttribute formats the attributes of a model`, () => {
21 server.config({
22 serializers: {
23 application: JSONAPISerializer.extend({
24 keyForAttribute(key) {
25 return snakeCase(key);
26 },
27 }),
28 },
29 });
30 let wordSmith = server.schema.wordSmiths.create({
31 id: 1,
32 firstName: "Link",
33 lastName: "Jackson",
34 age: 323,
35 });
36
37 let result = server.serializerOrRegistry.serialize(wordSmith);
38
39 expect(result).toEqual({
40 data: {
41 type: "word-smiths",
42 id: "1",
43 attributes: {
44 age: 323,
45 first_name: "Link",
46 last_name: "Jackson",
47 },
48 },
49 });
50 });
51
52 test(`keyForAttribute also formats the models in a collections`, () => {
53 server.config({
54 serializers: {
55 application: JSONAPISerializer.extend({
56 keyForAttribute(key) {
57 return snakeCase(key);
58 },
59 }),
60 },
61 });
62
63 server.schema.wordSmiths.create({
64 id: 1,
65 firstName: "Link",
66 lastName: "Jackson",
67 });
68 server.schema.wordSmiths.create({
69 id: 2,
70 firstName: "Zelda",
71 lastName: "Brown",
72 });
73 let wordSmiths = server.schema.wordSmiths.all();
74
75 let result = server.serializerOrRegistry.serialize(wordSmiths);
76
77 expect(result).toEqual({
78 data: [
79 {
80 type: "word-smiths",
81 id: "1",
82 attributes: {
83 first_name: "Link",
84 last_name: "Jackson",
85 },
86 },
87 {
88 type: "word-smiths",
89 id: "2",
90 attributes: {
91 first_name: "Zelda",
92 last_name: "Brown",
93 },
94 },
95 ],
96 });
97 });
98 });