Debian Codegraph @

__tests__/external/shared/orm/belongs-to/9-one-way-polymorphic/instantiating-test.js

raw
1 import Helper from "./_helper";
2
3 describe("External | Shared | ORM | Belongs To | One-way Polymorphic | 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 post = helper.savedParent();
16 let comment = schema.comments.new({
17 commentableId: { id: post.id, type: "post" },
18 });
19
20 expect(comment.commentableId).toEqual({ id: post.id, type: "post" });
21 expect(comment.commentable).toEqual(post);
22 expect(comment.attrs).toEqual({
23 commentableId: { id: post.id, type: "post" },
24 });
25 });
26
27 test("the child errors if the parent id doesnt exist", () => {
28 expect(function () {
29 schema.comments.new({ commentableId: { type: "post", id: 2 } });
30 }).toThrow();
31 });
32
33 test("the child accepts a null parent id", () => {
34 let comment = schema.comments.new({ commentableId: null });
35
36 expect(comment.commentableId).toBeNil();
37 expect(comment.commentable).toBeNil();
38 expect(comment.attrs).toEqual({ commentableId: null });
39 });
40
41 test("the child accepts a saved parent model", () => {
42 let post = helper.savedParent();
43 let comment = schema.comments.new({ commentable: post });
44
45 expect(comment.commentableId).toEqual({ type: "post", id: post.id });
46 expect(comment.commentable).toEqual(post);
47 });
48
49 test("the child accepts a new parent model", () => {
50 let post = schema.posts.new({ text: "foo" });
51 let comment = schema.comments.new({ commentable: post });
52
53 expect(comment.commentableId).toEqual({ type: "post", id: undefined });
54 expect(comment.commentable).toEqual(post);
55 expect(comment.attrs).toEqual({ commentableId: null });
56 });
57
58 test("the child accepts a null parent model", () => {
59 let comment = schema.comments.new({ commentable: null });
60
61 expect(comment.commentableId).toBeNil();
62 expect(comment.commentable).toBeNil();
63 expect(comment.attrs).toEqual({ commentableId: null });
64 });
65
66 test("the child accepts a parent model and id", () => {
67 let post = helper.savedParent();
68 let comment = schema.comments.new({
69 commentable: post,
70 commentableId: { type: "post", id: post.id },
71 });
72
73 expect(comment.commentableId).toEqual({ type: "post", id: "1" });
74 expect(comment.commentable).toEqual(post);
75 expect(comment.attrs).toEqual({
76 commentableId: { type: "post", id: post.id },
77 });
78 });
79
80 test("the child accepts no reference to a parent id or model as empty obj", () => {
81 let comment = schema.comments.new({});
82
83 expect(comment.commentableId).toBeNil();
84 expect(comment.commentable).toBeNil();
85 expect(comment.attrs).toEqual({ commentableId: null });
86 });
87
88 test("the child accepts no reference to a parent id or model", () => {
89 let comment = schema.comments.new();
90
91 expect(comment.commentableId).toBeNil();
92 expect(comment.commentable).toBeNil();
93 expect(comment.attrs).toEqual({ commentableId: null });
94 });
95 });