Debian Codegraph @

__tests__/external/shared/orm/belongs-to/6-one-way-reflexive/instantiating-test.js

raw
1 import Helper from "./_helper";
2
3 describe("External | Shared | ORM | Belongs To | One-Way Reflexive | instantiating", () => {
4 let helper, schema;
5
6 beforeEach(() => {
7 helper = new Helper();
8 schema = helper.schema;
9 });
10 afterEach(() => {
11 helper.shutdown();
12 });
13
14 test("the child accepts a saved parent id", () => {
15 let parent = helper.savedParent();
16 let child = schema.users.new({ userId: parent.id });
17
18 expect(child.userId).toEqual(parent.id);
19 expect(child.user.attrs).toEqual(parent.attrs);
20 expect(child.attrs).toEqual({ userId: parent.id });
21 });
22
23 test("the child errors if the parent id doesnt exist", () => {
24 expect(function () {
25 schema.users.new({ userId: 2 });
26 }).toThrow();
27 });
28
29 test("the child accepts a null parent id", () => {
30 let child = schema.users.new({ userId: null });
31
32 expect(child.userId).toBeNil();
33 expect(child.user).toBeNil();
34 expect(child.attrs).toEqual({ userId: null });
35 });
36
37 test("the child accepts a saved parent model", () => {
38 let parent = helper.savedParent();
39 let child = schema.users.new({ user: parent });
40
41 expect(child.userId).toEqual("1");
42 expect(child.user.attrs).toEqual(parent.attrs);
43 });
44
45 test("the child accepts a new parent model", () => {
46 let zelda = schema.users.new({ name: "Zelda" });
47 let child = schema.users.new({ user: zelda });
48
49 expect(child.userId).toBeNil();
50 expect(child.user).toEqual(zelda);
51 expect(child.attrs).toEqual({ userId: null });
52 });
53
54 test("the child accepts a null parent model", () => {
55 let child = schema.users.new({ user: null });
56
57 expect(child.userId).toBeNil();
58 expect(child.user).toBeNil();
59 expect(child.attrs).toEqual({ userId: null });
60 });
61
62 test("the child accepts a parent model and id", () => {
63 let parent = helper.savedParent();
64 let child = schema.users.new({ user: parent, userId: parent.id });
65
66 expect(child.userId).toEqual("1");
67 expect(child.user.attrs).toEqual(parent.attrs);
68 expect(child.attrs).toEqual({ userId: parent.id });
69 });
70
71 test("the child accepts no reference to a parent id or model as empty obj", () => {
72 let child = schema.users.new({});
73
74 expect(child.userId).toBeNil();
75 expect(child.user).toBeNil();
76 expect(child.attrs).toEqual({ userId: null });
77 });
78
79 test("the child accepts no reference to a parent id or model", () => {
80 let child = schema.users.new();
81
82 expect(child.userId).toBeNil();
83 expect(child.user).toBeNil();
84 expect(child.attrs).toEqual({ userId: null });
85 });
86 });