| 1 |
import Helper from "./_helper"; |
| 2 |
import { Model } from "miragejs"; |
| 3 |
|
| 4 |
describe("External | Shared | ORM | Has Many | Many to Many | create", () => { |
| 5 |
let helper; |
| 6 |
beforeEach(() => { |
| 7 |
helper = new Helper(); |
| 8 |
helper.schema.registerModel("foo", Model); |
| 9 |
}); |
| 10 |
afterEach(() => { |
| 11 |
helper.shutdown(); |
| 12 |
}); |
| 13 |
|
| 14 |
test("it sets up associations correctly when passing in the foreign key", () => { |
| 15 |
let { schema } = helper; |
| 16 |
let product = schema.products.create(); |
| 17 |
let order = schema.orders.create({ |
| 18 |
productIds: [product.id], |
| 19 |
}); |
| 20 |
|
| 21 |
product.reload(); |
| 22 |
|
| 23 |
expect(order.productIds).toEqual([product.id]); |
| 24 |
expect(product.orderIds).toEqual([order.id]); |
| 25 |
expect(order.attrs.productIds).toEqual([product.id]); |
| 26 |
expect(product.attrs.orderIds).toEqual([order.id]); |
| 27 |
expect(order.products.models[0].attrs).toEqual(product.attrs); |
| 28 |
expect(product.orders.models[0].attrs).toEqual(order.attrs); |
| 29 |
expect(helper.db.orders).toHaveLength(1); |
| 30 |
expect(helper.db.products).toHaveLength(1); |
| 31 |
expect(helper.db.orders[0]).toEqual({ id: "1", productIds: ["1"] }); |
| 32 |
expect(helper.db.products[0]).toEqual({ id: "1", orderIds: ["1"] }); |
| 33 |
}); |
| 34 |
|
| 35 |
test("it sets up associations correctly when passing in an array of models", () => { |
| 36 |
let { schema } = helper; |
| 37 |
let product = schema.products.create(); |
| 38 |
let order = schema.orders.create({ |
| 39 |
products: [product], |
| 40 |
}); |
| 41 |
|
| 42 |
product.reload(); |
| 43 |
|
| 44 |
expect(order.productIds).toEqual([product.id]); |
| 45 |
expect(product.orderIds).toEqual([order.id]); |
| 46 |
expect(order.attrs.productIds).toEqual([product.id]); |
| 47 |
expect(product.attrs.orderIds).toEqual([order.id]); |
| 48 |
expect(helper.db.orders).toHaveLength(1); |
| 49 |
expect(helper.db.products).toHaveLength(1); |
| 50 |
}); |
| 51 |
|
| 52 |
test("it sets up associations correctly when passing in a collection", () => { |
| 53 |
let { schema } = helper; |
| 54 |
let product = schema.products.create(); |
| 55 |
let order = schema.orders.create({ |
| 56 |
products: schema.products.all(), |
| 57 |
}); |
| 58 |
|
| 59 |
product.reload(); |
| 60 |
|
| 61 |
expect(order.productIds).toEqual([product.id]); |
| 62 |
expect(product.orderIds).toEqual([order.id]); |
| 63 |
expect(order.attrs.productIds).toEqual([product.id]); |
| 64 |
expect(product.attrs.orderIds).toEqual([order.id]); |
| 65 |
expect(helper.db.orders).toHaveLength(1); |
| 66 |
expect(helper.db.products).toHaveLength(1); |
| 67 |
}); |
| 68 |
|
| 69 |
test("it throws an error if a model is passed in without a defined relationship", () => { |
| 70 |
let { schema } = helper; |
| 71 |
|
| 72 |
expect(function () { |
| 73 |
schema.orders.create({ |
| 74 |
foo: schema.create("foo"), |
| 75 |
}); |
| 76 |
}).toThrow(); |
| 77 |
}); |
| 78 |
|
| 79 |
test("it throws an error if an array of models is passed in without a defined relationship", () => { |
| 80 |
let { schema } = helper; |
| 81 |
|
| 82 |
expect(function () { |
| 83 |
schema.orders.create({ |
| 84 |
foos: [schema.create("foo")], |
| 85 |
}); |
| 86 |
}).toThrow(); |
| 87 |
}); |
| 88 |
|
| 89 |
test("it throws an error if a collection is passed in without a defined relationship", () => { |
| 90 |
let { schema } = helper; |
| 91 |
schema.foos.create(); |
| 92 |
schema.foos.create(); |
| 93 |
|
| 94 |
expect(function () { |
| 95 |
schema.orders.create({ |
| 96 |
foos: schema.foos.all(), |
| 97 |
}); |
| 98 |
}).toThrow(); |
| 99 |
}); |
| 100 |
}); |