Debian Codegraph @

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

raw
1 import Helper, { states } from "./_helper";
2
3 describe("External | Shared | ORM | Has Many | Many-to-many Polymorphic | association #setIds", () => {
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 parentId, for all states
14 */
15 states.forEach((state) => {
16 test(`a ${state} can update its association to a saved parent via parentId`, () => {
17 let [user, originalPosts] = helper[state]();
18 let savedPost = helper.savedChild();
19
20 user.commentableIds = [{ type: "post", id: savedPost.id }];
21
22 expect(user.commentables.includes(savedPost)).toBeTruthy();
23 expect(
24 user.commentableIds.find(
25 ({ id, type }) => id === savedPost.id && type === "post"
26 )
27 ).toBeTruthy();
28
29 user.save();
30 savedPost.reload();
31
32 expect(savedPost.users.includes(user)).toBeTruthy();
33 originalPosts.forEach((post) => {
34 if (post.isSaved()) {
35 post.reload();
36 expect(post.users.includes(user)).toBeFalsy();
37 }
38 });
39 });
40
41 test(`a ${state} can clear its association via a null ids`, () => {
42 let [user, originalPosts] = helper[state]();
43
44 user.commentableIds = null;
45
46 expect(user.commentables.models).toBeEmpty();
47 expect(user.commentableIds).toBeEmpty();
48
49 user.save();
50
51 originalPosts.forEach((post) => {
52 post.reload();
53 expect(post.users.includes(user)).toBeFalsy();
54 });
55 });
56 });
57 });