| 1 |
import Helper, { states } from "./_helper"; |
| 2 |
|
| 3 |
describe("External | Shared | ORM | Has Many | One-Way 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] = 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)).toBeFalsy(); |
| 25 |
|
| 26 |
tag.save(); |
| 27 |
}); |
| 28 |
|
| 29 |
test(`a ${state} can update its association to a new parent`, () => { |
| 30 |
let [tag] = helper[state](); |
| 31 |
let newTag = helper.newChild(); |
| 32 |
|
| 33 |
tag.tags = [newTag]; |
| 34 |
|
| 35 |
expect(tag.tags.includes(newTag)).toBeTruthy(); |
| 36 |
expect(tag.tagIds[0]).toBeUndefined(); |
| 37 |
expect(newTag.tags.includes(tag)).toBeFalsy(); |
| 38 |
|
| 39 |
tag.save(); |
| 40 |
}); |
| 41 |
|
| 42 |
test(`a ${state} can clear its association via an empty list`, () => { |
| 43 |
let [tag] = helper[state](); |
| 44 |
|
| 45 |
tag.tags = []; |
| 46 |
|
| 47 |
expect(tag.tagIds).toBeEmpty(); |
| 48 |
expect(tag.tags.models).toHaveLength(0); |
| 49 |
|
| 50 |
tag.save(); |
| 51 |
}); |
| 52 |
|
| 53 |
test(`a ${state} can clear its association via an empty list`, () => { |
| 54 |
let [tag] = helper[state](); |
| 55 |
|
| 56 |
tag.tags = null; |
| 57 |
|
| 58 |
expect(tag.tagIds).toBeEmpty(); |
| 59 |
expect(tag.tags.models).toHaveLength(0); |
| 60 |
|
| 61 |
tag.save(); |
| 62 |
}); |
| 63 |
}); |
| 64 |
}); |