| 1 |
import Helper, { states } from "./_helper"; |
| 2 |
|
| 3 |
describe("External | Shared | ORM | Mixed | One 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.things = [savedPost]; |
| 21 |
|
| 22 |
expect(user.things.includes(savedPost)).toBeTruthy(); |
| 23 |
expect( |
| 24 |
user.thingIds.find( |
| 25 |
({ id, type }) => id === savedPost.id && type === "post" |
| 26 |
) |
| 27 |
).toBeTruthy(); |
| 28 |
|
| 29 |
user.save(); |
| 30 |
|
| 31 |
originalPosts.forEach((post) => { |
| 32 |
if (post.isSaved()) { |
| 33 |
post.reload(); |
| 34 |
expect(post.user).toBeNull(); |
| 35 |
} |
| 36 |
}); |
| 37 |
}); |
| 38 |
|
| 39 |
test(`a ${state} can update its association to a new parent`, () => { |
| 40 |
let [user, originalPosts] = helper[state](); |
| 41 |
let newPost = helper.newChild(); |
| 42 |
|
| 43 |
user.things = [newPost]; |
| 44 |
|
| 45 |
expect(user.thingIds).toEqual([{ type: "post", id: undefined }]); |
| 46 |
expect(user.things.includes(newPost)).toBeTruthy(); |
| 47 |
|
| 48 |
user.save(); |
| 49 |
|
| 50 |
originalPosts.forEach((post) => { |
| 51 |
if (post.isSaved()) { |
| 52 |
post.reload(); |
| 53 |
expect(post.user).toBeNull(); |
| 54 |
} |
| 55 |
}); |
| 56 |
}); |
| 57 |
|
| 58 |
test(`a ${state} can clear its association via an empty list`, () => { |
| 59 |
let [user, originalPosts] = helper[state](); |
| 60 |
|
| 61 |
user.things = []; |
| 62 |
|
| 63 |
expect(user.thingIds).toBeEmpty(); |
| 64 |
expect(user.things.models).toHaveLength(0); |
| 65 |
|
| 66 |
user.save(); |
| 67 |
|
| 68 |
originalPosts.forEach((post) => { |
| 69 |
if (post.isSaved()) { |
| 70 |
post.reload(); |
| 71 |
expect(post.user).toBeNull(); |
| 72 |
} |
| 73 |
}); |
| 74 |
}); |
| 75 |
|
| 76 |
test(`a ${state} can clear its association via an empty list`, () => { |
| 77 |
let [user, originalPosts] = helper[state](); |
| 78 |
|
| 79 |
user.things = null; |
| 80 |
|
| 81 |
expect(user.thingIds).toBeEmpty(); |
| 82 |
expect(user.things.models).toHaveLength(0); |
| 83 |
|
| 84 |
user.save(); |
| 85 |
|
| 86 |
originalPosts.forEach((post) => { |
| 87 |
if (post.isSaved()) { |
| 88 |
post.reload(); |
| 89 |
expect(post.user).toBeNull(); |
| 90 |
} |
| 91 |
}); |
| 92 |
}); |
| 93 |
}); |
| 94 |
}); |