| 1 |
import Helper, { states } from "./_helper"; |
| 2 |
|
| 3 |
describe("External | Shared | ORM | Mixed | Many To One | 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 [post, originalUser] = helper[state](); |
| 18 |
let user = helper.savedParent(); |
| 19 |
|
| 20 |
post.userId = user.id; |
| 21 |
|
| 22 |
expect(post.userId).toEqual(user.id); |
| 23 |
expect(post.user.attrs).toEqual(user.attrs); |
| 24 |
|
| 25 |
expect(post.user.posts.includes(post)).toBeTruthy(); |
| 26 |
|
| 27 |
post.save(); |
| 28 |
user.reload(); |
| 29 |
|
| 30 |
expect(user.posts.includes(post)).toBeTruthy(); |
| 31 |
|
| 32 |
// Old inverses were cleared |
| 33 |
if (originalUser && originalUser.isSaved()) { |
| 34 |
originalUser.reload(); |
| 35 |
expect(originalUser.posts.includes(post)).toBeFalsy(); |
| 36 |
} |
| 37 |
}); |
| 38 |
|
| 39 |
test(`a ${state} can clear its association via a null parentId`, () => { |
| 40 |
let [post, originalUser] = helper[state](); |
| 41 |
|
| 42 |
post.userId = null; |
| 43 |
|
| 44 |
expect(post.user).toBeNull(); |
| 45 |
expect(post.userId).toBeNull(); |
| 46 |
|
| 47 |
post.save(); |
| 48 |
|
| 49 |
if (originalUser && originalUser.isSaved()) { |
| 50 |
originalUser.reload(); |
| 51 |
expect(originalUser.posts.includes(post)).toBeFalsy(); |
| 52 |
} |
| 53 |
}); |
| 54 |
}); |
| 55 |
}); |