Debian Codegraph @

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

raw
1 import {
2 _ormSchema as Schema,
3 _Db as Db,
4 Model,
5 hasMany,
6 belongsTo,
7 JSONAPISerializer,
8 } from "@lib";
9
10 /*
11 This test is heavily coupled to the implementation and can be deleted
12 during a future refactoring.
13 */
14 describe("Unit | Serializers | JSON API Serializer | #_createIncludesGraphSnapshot mixed", function () {
15 let serializer = null;
16 let registry = null;
17 let type = null;
18 let request = {};
19
20 beforeEach(function () {
21 registry = {
22 serializerFor() {
23 return serializer;
24 },
25 };
26 type = "foo";
27 request = {};
28
29 serializer = new JSONAPISerializer(registry, type, request);
30 });
31
32 test("it works on models and collections with dot-path includes", () => {
33 let schema = new Schema(new Db(), {
34 wordSmith: Model.extend({
35 blogPosts: hasMany(),
36 }),
37 blogPost: Model.extend({
38 happyTag: belongsTo(),
39 }),
40 happyTag: Model.extend({
41 happyColor: belongsTo(),
42 }),
43 happyColor: Model.extend(),
44 });
45 let wordSmith = schema.wordSmiths.create();
46 let blogPost1 = wordSmith.createBlogPost();
47 let happyTag = blogPost1.createHappyTag();
48 happyTag.createHappyColor();
49
50 let blogPost2 = wordSmith.createBlogPost();
51 let happyTag2 = blogPost2.createHappyTag();
52 happyTag2.createHappyColor();
53
54 serializer.request = {
55 queryParams: { include: "blog-posts.happy-tag.happy-color" },
56 };
57
58 serializer._createRequestedIncludesGraph(wordSmith);
59
60 expect(serializer.request._includesGraph).toEqual({
61 data: {
62 "word-smith:1": {
63 relationships: {
64 "blog-posts": ["blog-post:1", "blog-post:2"],
65 },
66 },
67 },
68 included: {
69 "blog-posts": {
70 "blog-post:1": {
71 relationships: {
72 "happy-tag": "happy-tag:1",
73 },
74 },
75 "blog-post:2": {
76 relationships: {
77 "happy-tag": "happy-tag:2",
78 },
79 },
80 },
81 "happy-tags": {
82 "happy-tag:1": {
83 relationships: {
84 "happy-color": "happy-color:1",
85 },
86 },
87 "happy-tag:2": {
88 relationships: {
89 "happy-color": "happy-color:2",
90 },
91 },
92 },
93 "happy-colors": {
94 "happy-color:1": {},
95 "happy-color:2": {},
96 },
97 },
98 });
99 });
100 });