| 1 |
import { Server, Model } from "miragejs"; |
| 2 |
|
| 3 |
describe("External | Shared | ORM | create", () => { |
| 4 |
let User; |
| 5 |
let server; |
| 6 |
|
| 7 |
beforeEach(() => { |
| 8 |
User = Model.extend(); |
| 9 |
server = new Server({ |
| 10 |
environment: "test", |
| 11 |
models: { |
| 12 |
user: User, |
| 13 |
}, |
| 14 |
}); |
| 15 |
}); |
| 16 |
|
| 17 |
afterEach(() => { |
| 18 |
server.shutdown(); |
| 19 |
}); |
| 20 |
|
| 21 |
test("it cannot make new models that havent been registered", () => { |
| 22 |
expect(function () { |
| 23 |
server.schema.authors.new({ name: "Link" }); |
| 24 |
}).toThrow(); |
| 25 |
}); |
| 26 |
|
| 27 |
test("it cannot create models that havent been registered", () => { |
| 28 |
expect(function () { |
| 29 |
server.schema.authors.create({ name: "Link" }); |
| 30 |
}).toThrow(); |
| 31 |
}); |
| 32 |
|
| 33 |
test("it can make new models and then save them", () => { |
| 34 |
let user = server.schema.users.new({ name: "Link" }); |
| 35 |
|
| 36 |
expect(user instanceof User).toBeTruthy(); |
| 37 |
expect(user.attrs).toEqual({ name: "Link" }); |
| 38 |
expect(server.db.users).toBeEmpty(); |
| 39 |
|
| 40 |
user.save(); |
| 41 |
|
| 42 |
expect(user.id).toBeTruthy(); |
| 43 |
expect(user.attrs).toEqual({ id: "1", name: "Link" }); |
| 44 |
expect(server.db.users).toIncludeSameMembers([{ id: "1", name: "Link" }]); |
| 45 |
}); |
| 46 |
|
| 47 |
test("it can create new models, saved directly to the db", () => { |
| 48 |
let user = server.schema.users.create({ name: "Link" }); |
| 49 |
|
| 50 |
expect(user instanceof Model).toBeTruthy(); |
| 51 |
expect(user instanceof User).toBeTruthy(); |
| 52 |
expect(user.attrs).toEqual({ id: "1", name: "Link" }); |
| 53 |
expect(server.db.users).toIncludeSameMembers([{ id: "1", name: "Link" }]); |
| 54 |
}); |
| 55 |
}); |