| 1 |
import Helper from "./_helper"; |
| 2 |
|
| 3 |
describe("External | Shared | ORM | Has Many | Named Reflexive | new", () => { |
| 4 |
let helper, schema; |
| 5 |
beforeEach(() => { |
| 6 |
helper = new Helper(); |
| 7 |
schema = helper.schema; |
| 8 |
}); |
| 9 |
afterEach(() => { |
| 10 |
helper.shutdown(); |
| 11 |
}); |
| 12 |
|
| 13 |
test("the parent accepts a saved child id", () => { |
| 14 |
let tagA = helper.savedChild(); |
| 15 |
let tagB = schema.tags.new({ |
| 16 |
labelIds: [tagA.id], |
| 17 |
}); |
| 18 |
|
| 19 |
expect(tagB.labelIds).toEqual([tagA.id]); |
| 20 |
expect(tagB.labels.includes(tagA)).toBeTruthy(); |
| 21 |
}); |
| 22 |
|
| 23 |
test("the parent errors if the children ids don't exist", () => { |
| 24 |
expect(function () { |
| 25 |
schema.tags.new({ labelIds: [2] }); |
| 26 |
}).toThrow(); |
| 27 |
}); |
| 28 |
|
| 29 |
test("the parent accepts null children foreign key", () => { |
| 30 |
let tag = schema.tags.new({ labelIds: null }); |
| 31 |
|
| 32 |
expect(tag.labels.models).toHaveLength(0); |
| 33 |
expect(tag.labelIds).toBeEmpty(); |
| 34 |
expect(tag.attrs).toEqual({ labelIds: null }); |
| 35 |
}); |
| 36 |
|
| 37 |
test("the parent accepts saved children", () => { |
| 38 |
let tagA = helper.savedChild(); |
| 39 |
let tagB = schema.tags.new({ labels: [tagA] }); |
| 40 |
|
| 41 |
expect(tagB.labelIds).toEqual([tagA.id]); |
| 42 |
expect(tagB.labels.models[0]).toEqual(tagA); |
| 43 |
}); |
| 44 |
|
| 45 |
test("the parent accepts new children", () => { |
| 46 |
let tagA = schema.tags.new({ color: "Red" }); |
| 47 |
let tagB = schema.tags.new({ labels: [tagA] }); |
| 48 |
|
| 49 |
expect(tagB.labelIds).toEqual([undefined]); |
| 50 |
expect(tagB.labels.models[0]).toEqual(tagA); |
| 51 |
}); |
| 52 |
|
| 53 |
test("the parent accepts null children", () => { |
| 54 |
let tag = schema.tags.new({ labels: null }); |
| 55 |
|
| 56 |
expect(tag.labels.models).toHaveLength(0); |
| 57 |
expect(tag.labelIds).toBeEmpty(); |
| 58 |
expect(tag.attrs).toEqual({ labelIds: null }); |
| 59 |
}); |
| 60 |
|
| 61 |
test("the parent accepts children and child ids", () => { |
| 62 |
let tagA = helper.savedChild(); |
| 63 |
let tagB = schema.tags.new({ labels: [tagA], labelIds: [tagA.id] }); |
| 64 |
|
| 65 |
expect(tagB.labelIds).toEqual([tagA.id]); |
| 66 |
expect(tagB.labels.models[0]).toEqual(tagA); |
| 67 |
}); |
| 68 |
|
| 69 |
test("the parent accepts no reference to children or child ids as empty obj", () => { |
| 70 |
let tag = schema.tags.new({}); |
| 71 |
|
| 72 |
expect(tag.labelIds).toBeEmpty(); |
| 73 |
expect(tag.labels.models).toBeEmpty(); |
| 74 |
expect(tag.attrs).toEqual({ labelIds: null }); |
| 75 |
}); |
| 76 |
|
| 77 |
test("the parent accepts no reference to children or child ids", () => { |
| 78 |
let tag = schema.tags.new(); |
| 79 |
|
| 80 |
expect(tag.labelIds).toBeEmpty(); |
| 81 |
expect(tag.labels.models).toBeEmpty(); |
| 82 |
expect(tag.attrs).toEqual({ labelIds: null }); |
| 83 |
}); |
| 84 |
}); |