Debian Codegraph @

__tests__/external/shared/orm/has-many/4-named-reflexive/create-test.js

raw
1 import Helper from "./_helper";
2 import { Model } from "miragejs";
3
4 describe("External | Shared | ORM | Has Many | Named Reflexive | create", () => {
5 let helper;
6 beforeEach(() => {
7 helper = new Helper();
8 helper.schema.registerModel("foo", Model);
9 });
10 afterEach(() => {
11 helper.shutdown();
12 });
13
14 test("it sets up associations correctly when passing in the foreign key", () => {
15 let { schema } = helper;
16 let tagA = schema.tags.create();
17 let tagB = schema.tags.create({
18 labelIds: [tagA.id],
19 });
20
21 tagA.reload();
22
23 expect(tagA.labelIds).toEqual([tagB.id]);
24 expect(tagB.labelIds).toEqual([tagA.id]);
25 expect(tagA.attrs.labelIds).toEqual([tagB.id]);
26 expect(tagB.attrs.labelIds).toEqual([tagA.id]);
27 expect(tagA.labels.models[0].attrs).toEqual(tagB.attrs);
28 expect(tagB.labels.models[0].attrs).toEqual(tagA.attrs);
29 expect(helper.db.tags).toHaveLength(2);
30 expect(helper.db.tags[0]).toEqual({ id: "1", labelIds: ["2"] });
31 expect(helper.db.tags[1]).toEqual({ id: "2", labelIds: ["1"] });
32 });
33
34 test("it sets up associations correctly when passing in an array of models", () => {
35 let { schema } = helper;
36 let tagA = schema.tags.create();
37 let tagB = schema.tags.create({
38 labels: [tagA],
39 });
40
41 tagA.reload();
42
43 expect(tagB.labelIds).toEqual([tagA.id]);
44 expect(tagA.labelIds).toEqual([tagB.id]);
45 expect(tagA.attrs.labelIds).toEqual([tagB.id]);
46 expect(tagB.attrs.labelIds).toEqual([tagA.id]);
47 expect(helper.db.tags).toHaveLength(2);
48 });
49
50 test("it sets up associations correctly when passing in a collection", () => {
51 let { schema } = helper;
52 let tagA = schema.tags.create();
53 let tagB = schema.tags.create({
54 labels: schema.tags.all(),
55 });
56
57 tagA.reload();
58
59 expect(tagB.labelIds).toEqual([tagA.id]);
60 expect(tagA.labelIds).toEqual([tagB.id]);
61 expect(tagB.attrs.labelIds).toEqual([tagA.id]);
62 expect(tagA.attrs.labelIds).toEqual([tagB.id]);
63 expect(helper.db.tags).toHaveLength(2);
64 });
65
66 test("it throws an error if a model is passed in without a defined relationship", () => {
67 let { schema } = helper;
68
69 expect(function () {
70 schema.tags.create({
71 foo: schema.create("foo"),
72 });
73 }).toThrow();
74 });
75
76 test("it throws an error if an array of models is passed in without a defined relationship", () => {
77 let { schema } = helper;
78
79 expect(function () {
80 schema.tags.create({
81 foos: [schema.create("foo")],
82 });
83 }).toThrow();
84 });
85
86 test("it throws an error if a collection is passed in without a defined relationship", () => {
87 let { schema } = helper;
88 schema.foos.create();
89 schema.foos.create();
90
91 expect(function () {
92 schema.tags.create({
93 foos: schema.foos.all(),
94 });
95 }).toThrow();
96 });
97 });