| 1 |
import { Server, Model, hasMany, belongsTo } from "miragejs"; |
| 2 |
|
| 3 |
describe("External | Shared | ORM | assertions", () => { |
| 4 |
let server; |
| 5 |
|
| 6 |
beforeEach(() => { |
| 7 |
server = new Server({ |
| 8 |
models: { |
| 9 |
user: Model.extend({ |
| 10 |
posts: hasMany(), |
| 11 |
}), |
| 12 |
post: Model.extend({ |
| 13 |
author: belongsTo("user"), |
| 14 |
}), |
| 15 |
}, |
| 16 |
}); |
| 17 |
}); |
| 18 |
|
| 19 |
afterEach(() => { |
| 20 |
server.shutdown(); |
| 21 |
}); |
| 22 |
|
| 23 |
test("it errors when passing in the wrong type for a HasMany association", () => { |
| 24 |
expect(() => { |
| 25 |
server.schema.users.create({ |
| 26 |
name: "Sam", |
| 27 |
posts: [1], |
| 28 |
}); |
| 29 |
}).toThrow(); |
| 30 |
}); |
| 31 |
|
| 32 |
test(`it doesn't error when passing in an empty array`, () => { |
| 33 |
server.schema.users.create({ |
| 34 |
name: "Sam", |
| 35 |
posts: [], |
| 36 |
}); |
| 37 |
expect(true).toBeTruthy(); |
| 38 |
}); |
| 39 |
|
| 40 |
test("it errors when passing in the wrong type for a HasMany association foreign key", () => { |
| 41 |
expect(() => { |
| 42 |
server.schema.users.create({ |
| 43 |
name: "Sam", |
| 44 |
postIds: "foo", |
| 45 |
}); |
| 46 |
}).toThrow(); |
| 47 |
}); |
| 48 |
|
| 49 |
test("it errors when passing in a missing foreign key for a HasMany association foreign key", () => { |
| 50 |
expect(() => { |
| 51 |
server.schema.users.create({ |
| 52 |
name: "Sam", |
| 53 |
postIds: [2], |
| 54 |
}); |
| 55 |
}).toThrow(); |
| 56 |
}); |
| 57 |
|
| 58 |
test("it errors when passing in the wrong type for a BelongsTo association", () => { |
| 59 |
expect(() => { |
| 60 |
server.schema.posts.create({ |
| 61 |
title: "Post 1", |
| 62 |
author: "sam", |
| 63 |
}); |
| 64 |
}).toThrow(); |
| 65 |
}); |
| 66 |
|
| 67 |
test("it errors when passing in a missing foreign key for a BelongsTo association foreign key", () => { |
| 68 |
expect(() => { |
| 69 |
server.schema.posts.create({ |
| 70 |
title: "Post 1", |
| 71 |
authorId: 1, |
| 72 |
}); |
| 73 |
}).toThrow(); |
| 74 |
}); |
| 75 |
}); |