Debian Codegraph @

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

raw
1 import Helper, { states } from "./_helper";
2
3 describe("External | Shared | ORM | Has Many | Many-to-many Polymorphic | association #set", () => {
4 let helper;
5 beforeEach(() => {
6 helper = new Helper();
7 });
8 afterEach(() => {
9 helper.shutdown();
10 });
11
12 /*
13 The model can update its association via parent, for all states
14 */
15 states.forEach((state) => {
16 test(`a ${state} can update its association to a list of saved children`, () => {
17 let [user, originalPosts] = helper[state]();
18 let savedPost = helper.savedChild();
19
20 user.commentables = [savedPost];
21
22 expect(user.commentables.models.includes(savedPost)).toBeTruthy();
23 expect(
24 user.commentableIds.find(
25 ({ id, type }) => id === savedPost.id && type === "post"
26 )
27 ).toBeTruthy();
28 expect(savedPost.users.includes(user)).toBeTruthy();
29
30 user.save();
31
32 originalPosts.forEach((post) => {
33 post.reload();
34 expect(post.users.includes(user)).toBeFalsy();
35 });
36 });
37
38 test(`a ${state} can update its association to a new parent`, () => {
39 let [user, originalPosts] = helper[state]();
40 let newPost = helper.newChild();
41
42 user.commentables = [newPost];
43
44 expect(user.commentableIds).toEqual([{ type: "post", id: undefined }]);
45 expect(user.commentables.models[0]).toEqual(newPost);
46 expect(newPost.users.includes(user)).toBeTruthy();
47
48 user.save();
49
50 originalPosts.forEach((post) => {
51 post.reload();
52 expect(post.users.includes(user)).toBeFalsy();
53 });
54 });
55
56 test(`a ${state} can clear its association via an empty list`, () => {
57 let [user, originalPosts] = helper[state]();
58
59 user.commentables = [];
60
61 expect(user.commentableIds).toBeEmpty();
62 expect(user.commentables.models).toHaveLength(0);
63
64 user.save();
65
66 originalPosts.forEach((post) => {
67 post.reload();
68 expect(post.users.includes(user)).toBeFalsy();
69 });
70 });
71
72 test(`a ${state} can clear its association via null`, () => {
73 let [user, originalPosts] = helper[state]();
74
75 user.commentables = null;
76
77 expect(user.commentableIds).toBeEmpty();
78 expect(user.commentables.models).toHaveLength(0);
79
80 user.save();
81
82 originalPosts.forEach((post) => {
83 post.reload();
84 expect(post.users.includes(user)).toBeFalsy();
85 });
86 });
87 });
88 });