| 1 |
import { Server, Model } from "miragejs"; |
| 2 |
import uniqBy from "lodash.uniqby"; |
| 3 |
|
| 4 |
describe("External | Shared | Serializers | Base | Basic", function () { |
| 5 |
let server; |
| 6 |
|
| 7 |
beforeEach(function () { |
| 8 |
server = new Server({ |
| 9 |
models: { |
| 10 |
wordSmith: Model, |
| 11 |
}, |
| 12 |
}); |
| 13 |
}); |
| 14 |
|
| 15 |
afterEach(function () { |
| 16 |
server.shutdown(); |
| 17 |
}); |
| 18 |
|
| 19 |
test("it returns objects unaffected", () => { |
| 20 |
let result = server.serializerOrRegistry.serialize({ oh: "hai" }); |
| 21 |
|
| 22 |
expect(result).toEqual({ oh: "hai" }); |
| 23 |
}); |
| 24 |
|
| 25 |
test("it returns arrays unaffected", () => { |
| 26 |
let data = [ |
| 27 |
{ id: "1", name: "Link" }, |
| 28 |
{ id: "2", name: "Zelda" }, |
| 29 |
]; |
| 30 |
let result = server.serializerOrRegistry.serialize(data); |
| 31 |
|
| 32 |
expect(result).toEqual(data); |
| 33 |
}); |
| 34 |
|
| 35 |
test("it returns empty arrays unaffected", () => { |
| 36 |
let result = server.serializerOrRegistry.serialize([]); |
| 37 |
|
| 38 |
expect(result).toEqual([]); |
| 39 |
}); |
| 40 |
|
| 41 |
test(`it serializes a model by returning its attrs under a root`, () => { |
| 42 |
let wordSmith = server.schema.wordSmiths.create({ |
| 43 |
id: 1, |
| 44 |
name: "Link", |
| 45 |
}); |
| 46 |
let result = server.serializerOrRegistry.serialize(wordSmith); |
| 47 |
|
| 48 |
expect(result).toEqual({ |
| 49 |
wordSmith: { |
| 50 |
id: "1", |
| 51 |
name: "Link", |
| 52 |
}, |
| 53 |
}); |
| 54 |
}); |
| 55 |
|
| 56 |
test(`it serializes a collection of models by returning an array of their attrs under a pluralized root`, () => { |
| 57 |
server.schema.wordSmiths.create({ id: 1, name: "Link" }); |
| 58 |
server.schema.wordSmiths.create({ id: 2, name: "Zelda" }); |
| 59 |
|
| 60 |
let wordSmiths = server.schema.wordSmiths.all(); |
| 61 |
|
| 62 |
let result = server.serializerOrRegistry.serialize(wordSmiths); |
| 63 |
|
| 64 |
expect(result).toEqual({ |
| 65 |
wordSmiths: [ |
| 66 |
{ id: "1", name: "Link" }, |
| 67 |
{ id: "2", name: "Zelda" }, |
| 68 |
], |
| 69 |
}); |
| 70 |
}); |
| 71 |
|
| 72 |
test(`it can serialize an empty collection`, () => { |
| 73 |
let wordSmiths = server.schema.wordSmiths.all(); |
| 74 |
let result = server.serializerOrRegistry.serialize(wordSmiths); |
| 75 |
|
| 76 |
expect(result).toEqual({ |
| 77 |
wordSmiths: [], |
| 78 |
}); |
| 79 |
}); |
| 80 |
|
| 81 |
test("it returns POJAs of models unaffected", () => { |
| 82 |
server.schema.wordSmiths.create({ name: "Sam" }); |
| 83 |
server.schema.wordSmiths.create({ name: "Sam" }); |
| 84 |
server.schema.wordSmiths.create({ name: "Ganondorf" }); |
| 85 |
|
| 86 |
let wordSmiths = server.schema.wordSmiths.all().models; |
| 87 |
let uniqueNames = uniqBy(wordSmiths, "name"); |
| 88 |
let result = server.serializerOrRegistry.serialize(uniqueNames); |
| 89 |
|
| 90 |
expect(result).toEqual(uniqueNames); |
| 91 |
}); |
| 92 |
}); |