Debian Codegraph @

__tests__/internal/move-after-handle-request/regressions/1322-relationship-path-normalization-test.js

raw
1 import { Server, Model, hasMany, belongsTo, JSONAPISerializer } from "miragejs";
2 import { underscore } from "@lib/utils/inflector";
3
4 describe("Integration | Server | Regressions | 1322 Relationship Path Normalization Test", function () {
5 let server;
6
7 beforeEach(function () {
8 server = new Server({
9 environment: "test",
10 models: {
11 happyUser: Model.extend({
12 happyLicenses: hasMany(),
13 happyAvatar: belongsTo(),
14 }),
15 happyLicense: Model.extend({
16 happyUser: belongsTo(),
17 }),
18 happyAvatar: Model.extend({
19 happyUser: belongsTo(),
20 }),
21 },
22 serializers: {
23 application: JSONAPISerializer.extend({
24 keyForRelationship(relationshipName) {
25 return underscore(relationshipName);
26 },
27 }),
28 },
29 routes() {
30 this.resource("happy-licenses");
31 },
32 });
33 });
34
35 afterEach(function () {
36 server.shutdown();
37 });
38
39 test("it works", async () => {
40 let avatar1 = server.create("happy-avatar");
41 let user1 = server.create("happy-user", { happyAvatar: avatar1 });
42 server.create("happy-license", { happyUser: user1 });
43
44 expect.assertions(2);
45
46 let res = await fetch("/happy-licenses/1?include=happy_user.happy_avatar");
47 let json = await res.json();
48
49 expect(json.data).toEqual({
50 attributes: {},
51 id: "1",
52 relationships: {
53 happy_user: {
54 data: {
55 id: user1.id,
56 type: "happy-users",
57 },
58 },
59 },
60 type: "happy-licenses",
61 });
62 expect(json.included).toEqual([
63 {
64 id: user1.id,
65 type: "happy-users",
66 attributes: {},
67 relationships: {
68 happy_avatar: {
69 data: {
70 id: avatar1.id,
71 type: "happy-avatars",
72 },
73 },
74 },
75 },
76 {
77 id: avatar1.id,
78 type: "happy-avatars",
79 attributes: {},
80 },
81 ]);
82 });
83 });