Debian Codegraph @

__tests__/external/shared/orm/has-many/10-many-to-many-polymorphic/association-new-test.js

raw
1 import Helper, { states } from "./_helper";
2
3 describe("External | Shared | ORM | Has Many | Many-to-many Polymorphic | association #new", () => {
4 let helper;
5 beforeEach(() => {
6 helper = new Helper();
7 });
8 afterEach(() => {
9 helper.shutdown();
10 });
11
12 /*
13 The model can make a new unsaved belongs-to association, for all states
14 */
15
16 states.forEach((state) => {
17 test(`a ${state} can build a new associated parent`, () => {
18 let [user] = helper[state]();
19 let initialCount = user.commentables.models.length;
20
21 let post = user.newCommentable("post", { title: "Lorem ipsum" });
22
23 expect(!post.id).toBeTruthy();
24 expect(user.commentables.models).toHaveLength(initialCount + 1);
25 expect(post.users.models).toHaveLength(1);
26
27 post.save();
28
29 expect(post.attrs).toEqual({
30 id: post.id,
31 title: "Lorem ipsum",
32 userIds: [user.id],
33 });
34 expect(user.commentables.models).toHaveLength(initialCount + 1);
35 expect(user.commentables.includes(post)).toBeTruthy();
36 expect(
37 user.commentableIds.find((obj) => {
38 return obj.id === post.id && obj.type === "post";
39 })
40 ).toBeTruthy();
41 expect(post.users.includes(user)).toBeTruthy();
42 });
43 });
44 });