Debian Codegraph @

__tests__/internal/unit/serializers/json-api-serializer/create-includes-graph-snapshot-collections-test.js

raw
1 import {
2 _ormSchema as Schema,
3 _Db as Db,
4 Model,
5 hasMany,
6 JSONAPISerializer,
7 } from "@lib";
8
9 /*
10 This test is heavily coupled to the implementation and can be deleted
11 during a future refactoring.
12 */
13 describe("Unit | Serializers | JSON API Serializer | #_createIncludesGraphSnapshot collections", function () {
14 let serializer = null;
15 let registry = null;
16 let type = null;
17 let request = {};
18
19 beforeEach(function () {
20 registry = {
21 serializerFor() {
22 return serializer;
23 },
24 };
25 type = "foo";
26 request = {};
27
28 serializer = new JSONAPISerializer(registry, type, request);
29 });
30
31 test("it works on collections with no includes", () => {
32 let schema = new Schema(new Db(), {
33 wordSmith: Model.extend({}),
34 });
35 schema.wordSmiths.create();
36 schema.wordSmiths.create();
37
38 serializer._createRequestedIncludesGraph(schema.wordSmiths.all());
39
40 expect(serializer.request._includesGraph).toEqual({
41 data: {
42 "word-smith:1": {},
43 "word-smith:2": {},
44 },
45 });
46 });
47
48 test("it works on collections with hasMany relationships and dot-path includes", () => {
49 let schema = new Schema(new Db(), {
50 wordSmith: Model.extend({
51 redTags: hasMany(),
52 bluePosts: hasMany(),
53 }),
54 bluePost: Model.extend({
55 redTags: hasMany(),
56 }),
57 redTag: Model.extend({
58 someColors: hasMany(),
59 }),
60 someColor: Model.extend(),
61 });
62 let wordSmith1 = schema.wordSmiths.create();
63 wordSmith1.createRedTag();
64 wordSmith1.createRedTag();
65
66 let bluePost = wordSmith1.createBluePost();
67 let redTag = bluePost.createRedTag();
68 redTag.createSomeColor();
69
70 let wordSmith2 = schema.wordSmiths.create();
71 wordSmith2.createRedTag();
72
73 let bluePost2 = wordSmith2.createBluePost();
74 let redTag2 = bluePost2.createRedTag();
75 redTag2.createSomeColor();
76
77 serializer.request = {
78 queryParams: { include: "red-tags,blue-posts.red-tags.some-colors" },
79 };
80
81 serializer._createRequestedIncludesGraph(schema.wordSmiths.all());
82
83 expect(serializer.request._includesGraph).toEqual({
84 data: {
85 "word-smith:1": {
86 relationships: {
87 "red-tags": ["red-tag:1", "red-tag:2"],
88 "blue-posts": ["blue-post:1"],
89 },
90 },
91 "word-smith:2": {
92 relationships: {
93 "red-tags": ["red-tag:4"],
94 "blue-posts": ["blue-post:2"],
95 },
96 },
97 },
98 included: {
99 "red-tags": {
100 "red-tag:1": {},
101 "red-tag:2": {},
102 "red-tag:3": {
103 relationships: {
104 "some-colors": ["some-color:1"],
105 },
106 },
107 "red-tag:4": {},
108 "red-tag:5": {
109 relationships: {
110 "some-colors": ["some-color:2"],
111 },
112 },
113 },
114 "blue-posts": {
115 "blue-post:1": {
116 relationships: {
117 "red-tags": ["red-tag:3"],
118 },
119 },
120 "blue-post:2": {
121 relationships: {
122 "red-tags": ["red-tag:5"],
123 },
124 },
125 },
126 "some-colors": {
127 "some-color:1": {},
128 "some-color:2": {},
129 },
130 },
131 });
132 });
133 });