Debian Codegraph @

__tests__/external/shared/orm/mixed/3-one-to-many-polymorphic/create-test.js

raw
1 import Helper from "./_helper";
2 import { Model } from "miragejs";
3
4 describe("External | Shared | ORM | Mixed | One To Many 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 post.reload();
20
21 expect(user.thingIds).toEqual([{ type: "post", id: post.id }]);
22 expect(user.attrs.thingIds).toEqual([{ type: "post", id: post.id }]);
23 expect(user.things.includes(post)).toBeTruthy();
24 expect(post.user.attrs).toEqual(user.attrs);
25
26 let { db } = helper;
27 expect(db.posts).toHaveLength(1);
28 expect(db.posts[0]).toEqual({ id: "1", userId: "1" });
29 expect(db.users).toHaveLength(1);
30 expect(db.users[0]).toEqual({
31 id: "1",
32 thingIds: [{ type: "post", id: "1" }],
33 });
34 });
35
36 test("it sets up associations correctly when passing in an array of models", () => {
37 let post = helper.schema.create("post");
38 let user = helper.schema.create("user", {
39 things: [post],
40 });
41
42 expect(user.thingIds).toEqual([{ type: "post", id: post.id }]);
43 expect(user.attrs.thingIds).toEqual([{ type: "post", id: post.id }]);
44 expect(user.things.includes(post)).toBeTruthy();
45 expect(post.user.attrs).toEqual(user.attrs);
46
47 let { db } = helper;
48 expect(db.posts).toHaveLength(1);
49 expect(db.posts[0]).toEqual({ id: "1", userId: "1" });
50 expect(db.users).toHaveLength(1);
51 expect(db.users[0]).toEqual({
52 id: "1",
53 thingIds: [{ type: "post", id: "1" }],
54 });
55 });
56
57 test("it sets up associations correctly when passing in a collection", () => {
58 let post = helper.schema.create("post");
59 let user = helper.schema.create("user", {
60 things: helper.schema.posts.all(),
61 });
62 post.reload();
63
64 expect(user.thingIds).toEqual([{ type: "post", id: post.id }]);
65 expect(user.attrs.thingIds).toEqual([{ type: "post", id: post.id }]);
66 expect(user.things.includes(post)).toBeTruthy();
67
68 let { db } = helper;
69 expect(db.posts).toHaveLength(1);
70 expect(db.posts[0]).toEqual({ id: "1", userId: "1" });
71 expect(db.users).toHaveLength(1);
72 expect(db.users[0]).toEqual({
73 id: "1",
74 thingIds: [{ type: "post", id: "1" }],
75 });
76 });
77 });