| 1 |
import { Server, Model, hasMany, belongsTo, Serializer } from "miragejs"; |
| 2 |
|
| 3 |
describe("External | Shared | Serializers | Base | Associations | Embedded Models", function () { |
| 4 |
let server, BaseSerializer; |
| 5 |
|
| 6 |
beforeEach(function () { |
| 7 |
server = new Server({ |
| 8 |
models: { |
| 9 |
wordSmith: Model.extend({ |
| 10 |
posts: hasMany("blogPost", { inverse: "author" }), |
| 11 |
}), |
| 12 |
blogPost: Model.extend({ |
| 13 |
author: belongsTo("wordSmith", { inverse: "posts" }), |
| 14 |
comments: hasMany("fineComment", { inverse: "post" }), |
| 15 |
}), |
| 16 |
fineComment: Model.extend({ |
| 17 |
post: belongsTo("blogPost"), |
| 18 |
}), |
| 19 |
}, |
| 20 |
}); |
| 21 |
|
| 22 |
let wordSmith = server.schema.wordSmiths.create({ name: "Link" }); |
| 23 |
let post = wordSmith.createPost({ title: "Lorem" }); |
| 24 |
post.createComment({ text: "pwned" }); |
| 25 |
|
| 26 |
wordSmith.createPost({ title: "Ipsum" }); |
| 27 |
|
| 28 |
server.schema.wordSmiths.create({ name: "Zelda" }); |
| 29 |
|
| 30 |
BaseSerializer = Serializer.extend({ |
| 31 |
embed: true, |
| 32 |
}); |
| 33 |
}); |
| 34 |
|
| 35 |
afterEach(function () { |
| 36 |
server.shutdown(); |
| 37 |
}); |
| 38 |
|
| 39 |
test(`it can embed has-many relationships`, () => { |
| 40 |
server.config({ |
| 41 |
serializers: { |
| 42 |
application: BaseSerializer, |
| 43 |
wordSmith: BaseSerializer.extend({ |
| 44 |
include: ["posts"], |
| 45 |
}), |
| 46 |
}, |
| 47 |
}); |
| 48 |
|
| 49 |
let link = server.schema.wordSmiths.find(1); |
| 50 |
let result = server.serializerOrRegistry.serialize(link); |
| 51 |
|
| 52 |
expect(result).toEqual({ |
| 53 |
wordSmith: { |
| 54 |
id: "1", |
| 55 |
name: "Link", |
| 56 |
posts: [ |
| 57 |
{ id: "1", title: "Lorem" }, |
| 58 |
{ id: "2", title: "Ipsum" }, |
| 59 |
], |
| 60 |
}, |
| 61 |
}); |
| 62 |
}); |
| 63 |
|
| 64 |
test(`it can embed a chain of has-many relationships`, () => { |
| 65 |
server.config({ |
| 66 |
serializers: { |
| 67 |
application: BaseSerializer, |
| 68 |
wordSmith: BaseSerializer.extend({ |
| 69 |
include: ["posts"], |
| 70 |
}), |
| 71 |
blogPost: BaseSerializer.extend({ |
| 72 |
include: ["comments"], |
| 73 |
}), |
| 74 |
}, |
| 75 |
}); |
| 76 |
|
| 77 |
let wordSmith = server.schema.wordSmiths.find(1); |
| 78 |
let result = server.serializerOrRegistry.serialize(wordSmith); |
| 79 |
|
| 80 |
expect(result).toEqual({ |
| 81 |
wordSmith: { |
| 82 |
id: "1", |
| 83 |
name: "Link", |
| 84 |
posts: [ |
| 85 |
{ id: "1", title: "Lorem", comments: [{ id: "1", text: "pwned" }] }, |
| 86 |
{ id: "2", title: "Ipsum", comments: [] }, |
| 87 |
], |
| 88 |
}, |
| 89 |
}); |
| 90 |
}); |
| 91 |
|
| 92 |
test(`it can embed a belongs-to relationship`, () => { |
| 93 |
server.config({ |
| 94 |
serializers: { |
| 95 |
application: BaseSerializer, |
| 96 |
blogPost: BaseSerializer.extend({ |
| 97 |
embed: true, |
| 98 |
include: ["author"], |
| 99 |
}), |
| 100 |
}, |
| 101 |
}); |
| 102 |
|
| 103 |
let blogPost = server.schema.blogPosts.find(1); |
| 104 |
let result = server.serializerOrRegistry.serialize(blogPost); |
| 105 |
|
| 106 |
expect(result).toEqual({ |
| 107 |
blogPost: { |
| 108 |
id: "1", |
| 109 |
title: "Lorem", |
| 110 |
author: { id: "1", name: "Link" }, |
| 111 |
}, |
| 112 |
}); |
| 113 |
}); |
| 114 |
|
| 115 |
test(`it can serialize a chain of belongs-to relationships`, () => { |
| 116 |
server.config({ |
| 117 |
serializers: { |
| 118 |
application: BaseSerializer, |
| 119 |
fineComment: BaseSerializer.extend({ |
| 120 |
include: ["post"], |
| 121 |
}), |
| 122 |
blogPost: BaseSerializer.extend({ |
| 123 |
include: ["author"], |
| 124 |
}), |
| 125 |
}, |
| 126 |
}); |
| 127 |
|
| 128 |
let fineComment = server.schema.fineComments.find(1); |
| 129 |
let result = server.serializerOrRegistry.serialize(fineComment); |
| 130 |
|
| 131 |
expect(result).toEqual({ |
| 132 |
fineComment: { |
| 133 |
id: "1", |
| 134 |
text: "pwned", |
| 135 |
post: { |
| 136 |
id: "1", |
| 137 |
title: "Lorem", |
| 138 |
author: { |
| 139 |
id: "1", |
| 140 |
name: "Link", |
| 141 |
}, |
| 142 |
}, |
| 143 |
}, |
| 144 |
}); |
| 145 |
}); |
| 146 |
|
| 147 |
test(`it can have a null value on a belongs-to relationship`, () => { |
| 148 |
server.config({ |
| 149 |
serializers: { |
| 150 |
application: BaseSerializer, |
| 151 |
blogPost: BaseSerializer.extend({ |
| 152 |
embed: true, |
| 153 |
include: ["author"], |
| 154 |
}), |
| 155 |
}, |
| 156 |
}); |
| 157 |
|
| 158 |
let blogPost = server.schema.blogPosts.find(1); |
| 159 |
blogPost.update("author", null); |
| 160 |
let result = server.serializerOrRegistry.serialize(blogPost); |
| 161 |
|
| 162 |
expect(result).toEqual({ |
| 163 |
blogPost: { |
| 164 |
id: "1", |
| 165 |
title: "Lorem", |
| 166 |
}, |
| 167 |
}); |
| 168 |
}); |
| 169 |
|
| 170 |
test(`it ignores relationships that refer to serialized ancestor resources`, () => { |
| 171 |
server.config({ |
| 172 |
serializers: { |
| 173 |
application: BaseSerializer, |
| 174 |
wordSmith: BaseSerializer.extend({ |
| 175 |
include: ["posts"], |
| 176 |
}), |
| 177 |
blogPost: BaseSerializer.extend({ |
| 178 |
include: ["author"], |
| 179 |
}), |
| 180 |
}, |
| 181 |
}); |
| 182 |
|
| 183 |
let wordSmith = server.schema.wordSmiths.find(1); |
| 184 |
let result = server.serializerOrRegistry.serialize(wordSmith); |
| 185 |
|
| 186 |
expect(result).toEqual({ |
| 187 |
wordSmith: { |
| 188 |
id: "1", |
| 189 |
name: "Link", |
| 190 |
posts: [ |
| 191 |
{ id: "1", title: "Lorem" }, |
| 192 |
{ id: "2", title: "Ipsum" }, |
| 193 |
], |
| 194 |
}, |
| 195 |
}); |
| 196 |
}); |
| 197 |
|
| 198 |
test(`it ignores relationships that refer to serialized ancestor resources, multiple levels down`, () => { |
| 199 |
server.config({ |
| 200 |
serializers: { |
| 201 |
application: BaseSerializer, |
| 202 |
wordSmith: BaseSerializer.extend({ |
| 203 |
embed: true, |
| 204 |
include: ["posts"], |
| 205 |
}), |
| 206 |
blogPost: BaseSerializer.extend({ |
| 207 |
include: ["author", "comments"], |
| 208 |
}), |
| 209 |
fineComment: BaseSerializer.extend({ |
| 210 |
include: ["post"], |
| 211 |
}), |
| 212 |
}, |
| 213 |
}); |
| 214 |
|
| 215 |
let wordSmith = server.schema.wordSmiths.find(1); |
| 216 |
let result = server.serializerOrRegistry.serialize(wordSmith); |
| 217 |
|
| 218 |
expect(result).toEqual({ |
| 219 |
wordSmith: { |
| 220 |
id: "1", |
| 221 |
name: "Link", |
| 222 |
posts: [ |
| 223 |
{ id: "1", title: "Lorem", comments: [{ id: "1", text: "pwned" }] }, |
| 224 |
{ id: "2", title: "Ipsum", comments: [] }, |
| 225 |
], |
| 226 |
}, |
| 227 |
}); |
| 228 |
}); |
| 229 |
}); |