Debian Codegraph @

__tests__/external/shared/orm/mixed/2-many-to-one/association-new-test.js

raw
1 import Helper, { states } from "./_helper";
2
3 describe("External | Shared | ORM | Mixed | Many To One | 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 [post, originalUser] = helper[state]();
19
20 let user = post.newUser({ name: "Zelda" });
21
22 expect(!user.id).toBeTruthy();
23 expect(post.user).toEqual(user);
24 expect(user.posts.includes(post)).toBeTruthy();
25
26 user.save();
27 post.reload();
28
29 expect(user.id).toBeTruthy();
30 expect(post.user.attrs).toEqual(user.attrs);
31 expect(post.userId).toEqual(user.id);
32
33 // Check the inverse
34 expect(user.posts.includes(post)).toBeTruthy();
35
36 // Ensure old inverse was cleared
37 if (originalUser && originalUser.isSaved()) {
38 originalUser.reload();
39 expect(originalUser.posts.includes(post)).toBeFalsy();
40 }
41 });
42 });
43 });