| 1 |
import { Server, Model, belongsTo, hasMany, JSONAPISerializer } from "miragejs"; |
| 2 |
|
| 3 |
describe("External | Shared | Serializers | JSON API Serializer | Associations | Polymorphic", () => { |
| 4 |
test("it works for belongs to polymorphic relationships", () => { |
| 5 |
let server = new Server({ |
| 6 |
models: { |
| 7 |
photo: Model.extend(), |
| 8 |
video: Model.extend(), |
| 9 |
comment: Model.extend({ |
| 10 |
commentable: belongsTo({ polymorphic: true }), |
| 11 |
}), |
| 12 |
}, |
| 13 |
serializers: { |
| 14 |
application: JSONAPISerializer, |
| 15 |
comment: JSONAPISerializer.extend({ |
| 16 |
include: ["commentable"], |
| 17 |
}), |
| 18 |
}, |
| 19 |
}); |
| 20 |
|
| 21 |
let schema = server.schema; |
| 22 |
let photo = schema.photos.create({ title: "Foo" }); |
| 23 |
schema.comments.create({ text: "Pretty foo!", commentable: photo }); |
| 24 |
|
| 25 |
let video = schema.videos.create({ title: "Bar" }); |
| 26 |
schema.comments.create({ text: "Love the bar!", commentable: video }); |
| 27 |
|
| 28 |
let result = server.serializerOrRegistry.serialize(schema.comments.all()); |
| 29 |
expect(result).toEqual({ |
| 30 |
data: [ |
| 31 |
{ |
| 32 |
attributes: { |
| 33 |
text: "Pretty foo!", |
| 34 |
}, |
| 35 |
id: "1", |
| 36 |
relationships: { |
| 37 |
commentable: { |
| 38 |
data: { id: "1", type: "photos" }, |
| 39 |
}, |
| 40 |
}, |
| 41 |
type: "comments", |
| 42 |
}, |
| 43 |
{ |
| 44 |
attributes: { |
| 45 |
text: "Love the bar!", |
| 46 |
}, |
| 47 |
id: "2", |
| 48 |
relationships: { |
| 49 |
commentable: { |
| 50 |
data: { id: "1", type: "videos" }, |
| 51 |
}, |
| 52 |
}, |
| 53 |
type: "comments", |
| 54 |
}, |
| 55 |
], |
| 56 |
included: [ |
| 57 |
{ |
| 58 |
attributes: { |
| 59 |
title: "Foo", |
| 60 |
}, |
| 61 |
id: "1", |
| 62 |
type: "photos", |
| 63 |
}, |
| 64 |
{ |
| 65 |
attributes: { |
| 66 |
title: "Bar", |
| 67 |
}, |
| 68 |
id: "1", |
| 69 |
type: "videos", |
| 70 |
}, |
| 71 |
], |
| 72 |
}); |
| 73 |
|
| 74 |
server.shutdown(); |
| 75 |
}); |
| 76 |
|
| 77 |
test("it works for has many polymorphic relationships", () => { |
| 78 |
let server = new Server({ |
| 79 |
models: { |
| 80 |
user: Model.extend({ |
| 81 |
things: hasMany({ polymorphic: true }), |
| 82 |
}), |
| 83 |
car: Model.extend(), |
| 84 |
watch: Model.extend(), |
| 85 |
}, |
| 86 |
serializers: { |
| 87 |
application: JSONAPISerializer, |
| 88 |
user: JSONAPISerializer.extend({ |
| 89 |
include: ["things"], |
| 90 |
}), |
| 91 |
}, |
| 92 |
}); |
| 93 |
|
| 94 |
let schema = server.schema; |
| 95 |
let car = schema.cars.create({ make: "Infiniti" }); |
| 96 |
let watch = schema.watches.create({ make: "Citizen" }); |
| 97 |
let user = schema.users.create({ |
| 98 |
name: "Sam", |
| 99 |
things: [car, watch], |
| 100 |
}); |
| 101 |
|
| 102 |
let json = server.serializerOrRegistry.serialize(user); |
| 103 |
|
| 104 |
expect(json).toEqual({ |
| 105 |
data: { |
| 106 |
attributes: { |
| 107 |
name: "Sam", |
| 108 |
}, |
| 109 |
id: "1", |
| 110 |
relationships: { |
| 111 |
things: { |
| 112 |
data: [ |
| 113 |
{ id: "1", type: "cars" }, |
| 114 |
{ id: "1", type: "watches" }, |
| 115 |
], |
| 116 |
}, |
| 117 |
}, |
| 118 |
type: "users", |
| 119 |
}, |
| 120 |
included: [ |
| 121 |
{ |
| 122 |
attributes: { |
| 123 |
make: "Infiniti", |
| 124 |
}, |
| 125 |
id: "1", |
| 126 |
type: "cars", |
| 127 |
}, |
| 128 |
{ |
| 129 |
attributes: { |
| 130 |
make: "Citizen", |
| 131 |
}, |
| 132 |
id: "1", |
| 133 |
type: "watches", |
| 134 |
}, |
| 135 |
], |
| 136 |
}); |
| 137 |
|
| 138 |
server.shutdown(); |
| 139 |
}); |
| 140 |
|
| 141 |
test("it works for has many polymorphic relationships included via query params", () => { |
| 142 |
let server = new Server({ |
| 143 |
models: { |
| 144 |
user: Model.extend({ |
| 145 |
things: hasMany({ polymorphic: true }), |
| 146 |
}), |
| 147 |
car: Model.extend(), |
| 148 |
watch: Model.extend(), |
| 149 |
}, |
| 150 |
serializers: { |
| 151 |
application: JSONAPISerializer, |
| 152 |
}, |
| 153 |
}); |
| 154 |
|
| 155 |
let schema = server.schema; |
| 156 |
let car = schema.cars.create({ make: "Infiniti" }); |
| 157 |
let watch = schema.watches.create({ make: "Citizen" }); |
| 158 |
let user = schema.users.create({ |
| 159 |
name: "Sam", |
| 160 |
things: [car, watch], |
| 161 |
}); |
| 162 |
|
| 163 |
let json = server.serializerOrRegistry.serialize(user, { |
| 164 |
queryParams: { include: "things" }, |
| 165 |
}); |
| 166 |
|
| 167 |
expect(json).toEqual({ |
| 168 |
data: { |
| 169 |
attributes: { |
| 170 |
name: "Sam", |
| 171 |
}, |
| 172 |
id: "1", |
| 173 |
relationships: { |
| 174 |
things: { |
| 175 |
data: [ |
| 176 |
{ id: "1", type: "cars" }, |
| 177 |
{ id: "1", type: "watches" }, |
| 178 |
], |
| 179 |
}, |
| 180 |
}, |
| 181 |
type: "users", |
| 182 |
}, |
| 183 |
included: [ |
| 184 |
{ |
| 185 |
attributes: { |
| 186 |
make: "Infiniti", |
| 187 |
}, |
| 188 |
id: "1", |
| 189 |
type: "cars", |
| 190 |
}, |
| 191 |
{ |
| 192 |
attributes: { |
| 193 |
make: "Citizen", |
| 194 |
}, |
| 195 |
id: "1", |
| 196 |
type: "watches", |
| 197 |
}, |
| 198 |
], |
| 199 |
}); |
| 200 |
|
| 201 |
server.shutdown(); |
| 202 |
}); |
| 203 |
|
| 204 |
test("it works for a top-level polymorphic collection", () => { |
| 205 |
let server = new Server({ |
| 206 |
models: { |
| 207 |
user: Model.extend({ |
| 208 |
things: hasMany({ polymorphic: true }), |
| 209 |
}), |
| 210 |
car: Model.extend(), |
| 211 |
watch: Model.extend(), |
| 212 |
}, |
| 213 |
serializers: { |
| 214 |
application: JSONAPISerializer, |
| 215 |
user: JSONAPISerializer.extend({ |
| 216 |
include: ["things"], |
| 217 |
}), |
| 218 |
}, |
| 219 |
}); |
| 220 |
|
| 221 |
let schema = server.schema; |
| 222 |
let car = schema.cars.create({ make: "Infiniti" }); |
| 223 |
let watch = schema.watches.create({ make: "Citizen" }); |
| 224 |
let user = schema.users.create({ |
| 225 |
name: "Sam", |
| 226 |
things: [car, watch], |
| 227 |
}); |
| 228 |
|
| 229 |
let json = server.serializerOrRegistry.serialize(user.things); |
| 230 |
|
| 231 |
expect(json).toEqual({ |
| 232 |
data: [ |
| 233 |
{ |
| 234 |
attributes: { |
| 235 |
make: "Infiniti", |
| 236 |
}, |
| 237 |
id: "1", |
| 238 |
type: "cars", |
| 239 |
}, |
| 240 |
{ |
| 241 |
attributes: { |
| 242 |
make: "Citizen", |
| 243 |
}, |
| 244 |
id: "1", |
| 245 |
type: "watches", |
| 246 |
}, |
| 247 |
], |
| 248 |
}); |
| 249 |
|
| 250 |
server.shutdown(); |
| 251 |
}); |
| 252 |
}); |