| 1 |
import Helper, { states } from "./_helper"; |
| 2 |
|
| 3 |
describe("External | Shared | ORM | Has Many | One-way 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] = helper[state](); |
| 18 |
let savedPost = helper.savedChild(); |
| 19 |
|
| 20 |
user.things = [savedPost]; |
| 21 |
|
| 22 |
expect(user.things.models.includes(savedPost)).toBeTruthy(); |
| 23 |
expect( |
| 24 |
user.thingIds.find( |
| 25 |
({ id, type }) => id === savedPost.id && type === "post" |
| 26 |
) |
| 27 |
).toBeTruthy(); |
| 28 |
}); |
| 29 |
|
| 30 |
test(`a ${state} can update its association to a new parent`, () => { |
| 31 |
let [user] = helper[state](); |
| 32 |
let newPost = helper.newChild(); |
| 33 |
|
| 34 |
user.things = [newPost]; |
| 35 |
|
| 36 |
expect(user.thingIds).toEqual([{ type: "post", id: undefined }]); |
| 37 |
expect(user.things.models[0]).toEqual(newPost); |
| 38 |
}); |
| 39 |
|
| 40 |
test(`a ${state} can clear its association via an empty list`, () => { |
| 41 |
let [user] = helper[state](); |
| 42 |
|
| 43 |
user.things = []; |
| 44 |
|
| 45 |
expect(user.thingIds).toBeEmpty(); |
| 46 |
expect(user.things.models).toHaveLength(0); |
| 47 |
}); |
| 48 |
|
| 49 |
test(`a ${state} can clear its association via null`, () => { |
| 50 |
let [user] = helper[state](); |
| 51 |
|
| 52 |
user.things = null; |
| 53 |
|
| 54 |
expect(user.thingIds).toBeEmpty(); |
| 55 |
expect(user.things.models).toHaveLength(0); |
| 56 |
}); |
| 57 |
}); |
| 58 |
}); |