Debian Codegraph @

__tests__/external/shared/orm/has-many/9-one-way-polymorphic/create-test.js

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