Debian Codegraph @

__tests__/external/shared/orm/belongs-to/1-basic/instantiating-test.js

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