Debian Codegraph @

__tests__/external/shared/serializers/base/associations/polymorphic/top-level-test.js

raw
1 import { Server, Model, hasMany, Serializer } from "miragejs";
2
3 describe("External | Shared | Serializers | Base | Associations | Polymorphic | Top level", function () {
4 let server, user;
5
6 beforeEach(function () {
7 server = new Server({
8 models: {
9 user: Model.extend({
10 things: hasMany({ polymorphic: true }),
11 }),
12 picture: Model.extend(),
13 car: Model.extend(),
14 },
15 });
16 user = server.create("user", {
17 things: [
18 server.create("picture", { title: "Picture 1" }),
19 server.create("car", { name: "Car 1" }),
20 ],
21 });
22 });
23
24 afterEach(function () {
25 server.shutdown();
26 });
27
28 test(`it can serialize a polymorphic collection when root is false`, () => {
29 server.config({
30 serializers: {
31 application: Serializer.extend({
32 root: false,
33 embed: true,
34 }),
35 },
36 });
37 let json = server.serializerOrRegistry.serialize(user.things);
38
39 expect(json).toEqual([
40 {
41 id: "1",
42 title: "Picture 1",
43 },
44 {
45 id: "1",
46 name: "Car 1",
47 },
48 ]);
49 });
50
51 test(`it throws if trying to serialize a polymorphic collection when root is true`, () => {
52 server.config({
53 serializers: {
54 application: Serializer.extend({
55 root: true,
56 }),
57 },
58 });
59
60 expect(() => {
61 server.serializerOrRegistry.serialize(user.things);
62 }).toThrow();
63 });
64 });