| 1 |
import Helper, { states } from "./_helper"; |
| 2 |
|
| 3 |
describe("External | Shared | ORM | Has Many | Reflexive | 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 [tag, originalTags] = helper[state](); |
| 18 |
let savedTag = helper.savedChild(); |
| 19 |
|
| 20 |
tag.tags = [savedTag]; |
| 21 |
|
| 22 |
expect(tag.tags.includes(savedTag)).toBeTruthy(); |
| 23 |
expect(tag.tagIds[0]).toEqual(savedTag.id); |
| 24 |
expect(savedTag.tags.includes(tag)).toBeTruthy(); |
| 25 |
|
| 26 |
tag.save(); |
| 27 |
|
| 28 |
originalTags.forEach((originalTag) => { |
| 29 |
originalTag.reload(); |
| 30 |
expect(originalTag.tags.includes(tag)).toBeFalsy(); |
| 31 |
}); |
| 32 |
}); |
| 33 |
|
| 34 |
test(`a ${state} can update its association to a new parent`, () => { |
| 35 |
let [tag, originalTags] = helper[state](); |
| 36 |
let newTag = helper.newChild(); |
| 37 |
|
| 38 |
tag.tags = [newTag]; |
| 39 |
|
| 40 |
expect(tag.tags.includes(newTag)).toBeTruthy(); |
| 41 |
expect(tag.tagIds[0]).toBeUndefined(); |
| 42 |
expect(newTag.tags.includes(tag)).toBeTruthy(); |
| 43 |
|
| 44 |
tag.save(); |
| 45 |
|
| 46 |
originalTags.forEach((originalTag) => { |
| 47 |
originalTag.reload(); |
| 48 |
expect(originalTag.tags.includes(tag)).toBeFalsy(); |
| 49 |
}); |
| 50 |
}); |
| 51 |
|
| 52 |
test(`a ${state} can clear its association via an empty list`, () => { |
| 53 |
let [tag, originalTags] = helper[state](); |
| 54 |
|
| 55 |
tag.tags = []; |
| 56 |
|
| 57 |
expect(tag.tagIds).toBeEmpty(); |
| 58 |
expect(tag.tags.models).toHaveLength(0); |
| 59 |
|
| 60 |
tag.save(); |
| 61 |
originalTags.forEach((originalTag) => { |
| 62 |
originalTag.reload(); |
| 63 |
expect(originalTag.tags.includes(tag)).toBeFalsy(); |
| 64 |
}); |
| 65 |
}); |
| 66 |
|
| 67 |
test(`a ${state} can clear its association via an empty list`, () => { |
| 68 |
let [tag, originalTags] = helper[state](); |
| 69 |
|
| 70 |
tag.tags = null; |
| 71 |
|
| 72 |
expect(tag.tagIds).toBeEmpty(); |
| 73 |
expect(tag.tags.models).toHaveLength(0); |
| 74 |
|
| 75 |
tag.save(); |
| 76 |
originalTags.forEach((originalTag) => { |
| 77 |
originalTag.reload(); |
| 78 |
expect(originalTag.tags.includes(tag)).toBeFalsy(); |
| 79 |
}); |
| 80 |
}); |
| 81 |
}); |
| 82 |
}); |