Debian Codegraph @

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

raw
1 import Helper from "./_helper";
2
3 describe("External | Shared | ORM | Mixed | One To Many Polymorphic | instantiating", () => {
4 let helper, schema;
5 beforeEach(() => {
6 helper = new Helper();
7 schema = helper.schema;
8 });
9 afterEach(() => {
10 helper.shutdown();
11 });
12
13 test("the parent accepts a saved child id", () => {
14 let post = helper.savedChild();
15 let user = schema.users.new({
16 thingIds: [{ type: "post", id: post.id }],
17 });
18
19 expect(user.thingIds).toEqual([{ type: "post", id: post.id }]);
20 expect(user.things.includes(post)).toBeTruthy();
21 });
22
23 test("the parent errors if the children ids don't exist", () => {
24 expect(function () {
25 schema.users.new({ thingIds: [{ type: "post", id: 2 }] });
26 }).toThrow();
27 });
28
29 test("the parent accepts null children foreign key", () => {
30 let user = schema.users.new({ thingIds: null });
31
32 expect(user.things.models).toHaveLength(0);
33 expect(user.thingIds).toBeEmpty();
34 expect(user.attrs).toEqual({ thingIds: null });
35 });
36
37 test("the parent accepts saved children", () => {
38 let post = helper.savedChild();
39 let user = schema.users.new({ things: [post] });
40
41 expect(user.thingIds).toEqual([{ type: "post", id: post.id }]);
42 expect(user.things.models[0]).toEqual(post);
43 });
44
45 test("the parent accepts new children", () => {
46 let post = schema.posts.new({ title: "Lorem" });
47 let user = schema.users.new({ things: [post] });
48
49 expect(user.thingIds).toEqual([{ type: "post", id: undefined }]);
50 expect(user.things.models[0]).toEqual(post);
51 });
52
53 test("the parent accepts null children", () => {
54 let user = schema.users.new({ things: null });
55
56 expect(user.things.models).toHaveLength(0);
57 expect(user.thingIds).toBeEmpty();
58 expect(user.attrs).toEqual({ thingIds: null });
59 });
60
61 test("the parent accepts children and child ids", () => {
62 let post = helper.savedChild();
63 let user = schema.users.new({
64 things: [post],
65 thingIds: [{ type: "post", id: post.id }],
66 });
67
68 expect(user.thingIds).toEqual([{ type: "post", id: post.id }]);
69 expect(user.things.models[0]).toEqual(post);
70 });
71
72 test("the parent accepts no reference to children or child ids as empty obj", () => {
73 let user = schema.users.new({});
74
75 expect(user.thingIds).toBeEmpty();
76 expect(user.things.models).toBeEmpty();
77 expect(user.attrs).toEqual({ thingIds: null });
78 });
79
80 test("the parent accepts no reference to children or child ids", () => {
81 let user = schema.users.new();
82
83 expect(user.thingIds).toBeEmpty();
84 expect(user.things.models).toBeEmpty();
85 expect(user.attrs).toEqual({ thingIds: null });
86 });
87 });