| 1 |
import { Server, Model } from "miragejs"; |
| 2 |
|
| 3 |
describe("External | Shared | ORM | #findOrCreateBy", () => { |
| 4 |
let User; |
| 5 |
let server; |
| 6 |
|
| 7 |
beforeEach(() => { |
| 8 |
User = Model; |
| 9 |
server = new Server({ |
| 10 |
environment: "test", |
| 11 |
models: { |
| 12 |
user: User, |
| 13 |
}, |
| 14 |
}); |
| 15 |
|
| 16 |
server.db.loadData({ |
| 17 |
users: [ |
| 18 |
{ id: 1, name: "Link", good: true }, |
| 19 |
{ id: 2, name: "Zelda", good: true }, |
| 20 |
{ id: 3, name: "Ganon", good: false }, |
| 21 |
], |
| 22 |
}); |
| 23 |
}); |
| 24 |
|
| 25 |
afterEach(() => { |
| 26 |
server.shutdown(); |
| 27 |
}); |
| 28 |
|
| 29 |
test("it returns the first model that matches the attrs", () => { |
| 30 |
let user = server.schema.users.findOrCreateBy({ good: true }); |
| 31 |
|
| 32 |
expect(user instanceof User).toBeTruthy(); |
| 33 |
expect(user.attrs).toEqual({ id: "1", name: "Link", good: true }); |
| 34 |
}); |
| 35 |
|
| 36 |
test("it creates a model if no existing model with the attrs is found", () => { |
| 37 |
expect(server.schema.db.users).toHaveLength(3); |
| 38 |
|
| 39 |
let newUser = server.schema.users.findOrCreateBy({ |
| 40 |
name: "Link", |
| 41 |
good: false, |
| 42 |
}); |
| 43 |
|
| 44 |
expect(server.schema.db.users).toHaveLength(4); |
| 45 |
expect(newUser instanceof User).toBeTruthy(); |
| 46 |
expect(newUser.attrs).toEqual({ id: "4", name: "Link", good: false }); |
| 47 |
}); |
| 48 |
}); |