| 1 |
import { Server, Model, Serializer } from "miragejs"; |
| 2 |
|
| 3 |
describe("External | Shared | Serializers | Base | Attrs List", function () { |
| 4 |
let server; |
| 5 |
|
| 6 |
beforeEach(function () { |
| 7 |
server = new Server({ |
| 8 |
models: { |
| 9 |
wordSmith: Model, |
| 10 |
}, |
| 11 |
serializers: { |
| 12 |
application: Serializer, |
| 13 |
wordSmith: Serializer.extend({ |
| 14 |
attrs: ["id", "name"], |
| 15 |
}), |
| 16 |
}, |
| 17 |
}); |
| 18 |
}); |
| 19 |
|
| 20 |
afterEach(function () { |
| 21 |
server.shutdown(); |
| 22 |
}); |
| 23 |
|
| 24 |
test(`it returns only the whitelisted attrs when serializing a model`, () => { |
| 25 |
let wordSmith = server.schema.wordSmiths.create({ |
| 26 |
id: 1, |
| 27 |
name: "Link", |
| 28 |
age: 123, |
| 29 |
}); |
| 30 |
|
| 31 |
let result = server.serializerOrRegistry.serialize(wordSmith); |
| 32 |
|
| 33 |
expect(result).toEqual({ |
| 34 |
wordSmith: { |
| 35 |
id: "1", |
| 36 |
name: "Link", |
| 37 |
}, |
| 38 |
}); |
| 39 |
}); |
| 40 |
|
| 41 |
test(`it returns only the whitelisted attrs when serializing a collection`, () => { |
| 42 |
server.schema.wordSmiths.create({ id: 1, name: "Link", age: 123 }); |
| 43 |
server.schema.wordSmiths.create({ id: 2, name: "Zelda", age: 456 }); |
| 44 |
|
| 45 |
let collection = server.schema.wordSmiths.all(); |
| 46 |
let result = server.serializerOrRegistry.serialize(collection); |
| 47 |
|
| 48 |
expect(result).toEqual({ |
| 49 |
wordSmiths: [ |
| 50 |
{ id: "1", name: "Link" }, |
| 51 |
{ id: "2", name: "Zelda" }, |
| 52 |
], |
| 53 |
}); |
| 54 |
}); |
| 55 |
}); |