Debian Codegraph @

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

raw
1 import { Server, Model, JSONAPISerializer } from "miragejs";
2
3 describe("External | Shared | Serializers | JSON API Serializer | Base", () => {
4 let server;
5
6 beforeEach(() => {
7 server = new Server({
8 models: {
9 wordSmith: Model,
10 },
11 serializers: {
12 application: JSONAPISerializer,
13 },
14 });
15 });
16
17 afterEach(() => {
18 server.shutdown();
19 });
20
21 test(`it includes all attributes for a model`, () => {
22 let link = server.schema.wordSmiths.create({ firstName: "Link", age: 123 });
23 let result = server.serializerOrRegistry.serialize(link);
24
25 expect(result).toEqual({
26 data: {
27 type: "word-smiths",
28 id: "1",
29 attributes: {
30 "first-name": "Link",
31 age: 123,
32 },
33 },
34 });
35 });
36
37 test(`it includes all attributes for each model in a collection`, () => {
38 server.schema.wordSmiths.create({ firstName: "Link", age: 123 });
39 server.schema.wordSmiths.create({ id: 1, firstName: "Link", age: 123 });
40 server.schema.wordSmiths.create({ id: 2, firstName: "Zelda", age: 456 });
41
42 let collection = server.schema.wordSmiths.all();
43 let result = server.serializerOrRegistry.serialize(collection);
44
45 expect(result).toEqual({
46 data: [
47 {
48 type: "word-smiths",
49 id: "1",
50 attributes: {
51 "first-name": "Link",
52 age: 123,
53 },
54 },
55 {
56 type: "word-smiths",
57 id: "2",
58 attributes: {
59 "first-name": "Zelda",
60 age: 456,
61 },
62 },
63 ],
64 });
65 });
66
67 test(`it can serialize an empty collection`, () => {
68 let wordSmiths = server.schema.wordSmiths.all();
69 let result = server.serializerOrRegistry.serialize(wordSmiths);
70
71 expect(result).toEqual({
72 data: [],
73 });
74 });
75 });