Debian Codegraph @

__tests__/external/shared/orm/belongs-to/3-reflexive/association-set-id-test.js

raw
1 import Helper, { states } from "./_helper";
2
3 describe("External | Shared | ORM | Belongs To | Reflexive | association #setId", () => {
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, originalUser] = helper[state]();
18 let friend = helper.savedParent();
19
20 user.userId = friend.id;
21
22 expect(user.userId).toEqual(friend.id);
23 expect(user.user.attrs).toEqual(friend.attrs);
24
25 user.save();
26 if (originalUser) {
27 originalUser.reload();
28 expect(originalUser.userId).toBeNil();
29 }
30 });
31 });
32
33 ["savedChildSavedParent", "newChildSavedParent"].forEach((state) => {
34 test(`a ${state} can clear its association via a null parentId`, () => {
35 let [user, originalUser] = helper[state]();
36
37 user.userId = null;
38
39 expect(user.userId).toBeNil();
40 expect(user.user).toBeNil();
41
42 user.save();
43 if (originalUser) {
44 originalUser.reload();
45 expect(originalUser.userId).toBeNil();
46 }
47 });
48 });
49 });