| 1 |
import { Server, Model } from "miragejs"; |
| 2 |
|
| 3 |
describe("External | Shared | ORM | attrs", () => { |
| 4 |
let server; |
| 5 |
|
| 6 |
beforeEach(() => { |
| 7 |
server = new Server({ |
| 8 |
environment: "test", |
| 9 |
models: { |
| 10 |
user: Model, |
| 11 |
}, |
| 12 |
}); |
| 13 |
|
| 14 |
server.db.loadData({ |
| 15 |
users: [{ id: 1, name: "Link", evil: false }], |
| 16 |
}); |
| 17 |
}); |
| 18 |
|
| 19 |
afterEach(() => { |
| 20 |
server.shutdown(); |
| 21 |
}); |
| 22 |
|
| 23 |
test("attrs returns the models attributes", () => { |
| 24 |
let user = server.schema.users.find(1); |
| 25 |
|
| 26 |
expect(user.attrs).toEqual({ id: "1", name: "Link", evil: false }); |
| 27 |
}); |
| 28 |
|
| 29 |
test("attributes can be read via plain property access", () => { |
| 30 |
let user = server.schema.users.find(1); |
| 31 |
|
| 32 |
expect(user.name).toEqual("Link"); |
| 33 |
}); |
| 34 |
|
| 35 |
test("toJSON should return a copy of attrs so they cannot be mutated", () => { |
| 36 |
let user = server.schema.users.find(1); |
| 37 |
|
| 38 |
let json = user.toJSON(); |
| 39 |
expect(json).toEqual({ id: "1", name: "Link", evil: false }); |
| 40 |
|
| 41 |
json.name = "Young Link"; |
| 42 |
expect(user.name).toEqual("Link"); |
| 43 |
}); |
| 44 |
}); |