Debian Codegraph @

__tests__/external/shared/orm/has-many/7-named-one-way-reflexive/create-test.js

raw
1 import Helper from "./_helper";
2 import { Model } from "miragejs";
3
4 describe("External | Shared | ORM | Has Many | Named One-Way 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(tagB.labelIds).toEqual([tagA.id]);
24 expect(tagA.labelIds).toBeEmpty();
25 expect(tagB.attrs.labelIds).toEqual([tagA.id]);
26 expect(tagB.labels.models[0].attrs).toEqual(tagA.attrs);
27 expect(helper.db.tags).toHaveLength(2);
28 expect(helper.db.tags[0]).toEqual({ id: "1", labelIds: null });
29 expect(helper.db.tags[1]).toEqual({ id: "2", labelIds: ["1"] });
30 });
31
32 test("it sets up associations correctly when passing in an array of models", () => {
33 let { schema } = helper;
34 let tagA = schema.tags.create();
35 let tagB = schema.tags.create({
36 labels: [tagA],
37 });
38
39 tagA.reload();
40
41 expect(tagB.labelIds).toEqual([tagA.id]);
42 expect(tagA.labelIds).toBeEmpty();
43 expect(tagB.attrs.labelIds).toEqual([tagA.id]);
44 expect(tagA.attrs.labelIds).toBeNull();
45 expect(helper.db.tags).toHaveLength(2);
46 });
47
48 test("it sets up associations correctly when passing in a collection", () => {
49 let { schema } = helper;
50 let tagA = schema.tags.create();
51 let tagB = schema.tags.create({
52 labels: schema.tags.all(),
53 });
54
55 tagA.reload();
56
57 expect(tagB.labelIds).toEqual([tagA.id]);
58 expect(tagA.labelIds).toBeEmpty();
59 expect(tagB.attrs.labelIds).toEqual([tagA.id]);
60 expect(tagA.attrs.labelIds).toBeNull();
61 expect(helper.db.tags).toHaveLength(2);
62 });
63
64 test("it throws an error if a model is passed in without a defined relationship", () => {
65 let { schema } = helper;
66
67 expect(function () {
68 schema.tags.create({
69 foo: schema.create("foo"),
70 });
71 }).toThrow();
72 });
73
74 test("it throws an error if an array of models is passed in without a defined relationship", () => {
75 let { schema } = helper;
76
77 expect(function () {
78 schema.tags.create({
79 foos: [schema.create("foo")],
80 });
81 }).toThrow();
82 });
83
84 test("it throws an error if a collection is passed in without a defined relationship", () => {
85 let { schema } = helper;
86 schema.foos.create();
87 schema.foos.create();
88
89 expect(function () {
90 schema.tags.create({
91 foos: schema.foos.all(),
92 });
93 }).toThrow();
94 });
95 });