| 1 |
import { |
| 2 |
_ormSchema as Schema, |
| 3 |
_Db as Db, |
| 4 |
Model, |
| 5 |
hasMany, |
| 6 |
belongsTo, |
| 7 |
JSONAPISerializer, |
| 8 |
} from "@lib"; |
| 9 |
|
| 10 |
/* |
| 11 |
This test is heavily coupled to the implementation and can be deleted |
| 12 |
during a future refactoring. |
| 13 |
*/ |
| 14 |
describe("Unit | Serializers | JSON API Serializer | #_createIncludesGraphSnapshot models", function () { |
| 15 |
let serializer = null; |
| 16 |
let registry = null; |
| 17 |
let type = null; |
| 18 |
let request = {}; |
| 19 |
|
| 20 |
beforeEach(function () { |
| 21 |
registry = { |
| 22 |
serializerFor() { |
| 23 |
return serializer; |
| 24 |
}, |
| 25 |
}; |
| 26 |
type = "foo"; |
| 27 |
request = {}; |
| 28 |
|
| 29 |
serializer = new JSONAPISerializer(registry, type, request); |
| 30 |
}); |
| 31 |
|
| 32 |
test("it works on models with no includes", () => { |
| 33 |
let schema = new Schema(new Db(), { |
| 34 |
wordSmith: Model.extend({}), |
| 35 |
}); |
| 36 |
let wordSmith = schema.wordSmiths.create(); |
| 37 |
|
| 38 |
serializer._createRequestedIncludesGraph(wordSmith); |
| 39 |
|
| 40 |
expect(serializer.request._includesGraph).toEqual({ |
| 41 |
data: { |
| 42 |
"word-smith:1": {}, |
| 43 |
}, |
| 44 |
}); |
| 45 |
}); |
| 46 |
|
| 47 |
test("it doesn't choke on an empty belongsTo relationship", () => { |
| 48 |
let schema = new Schema(new Db(), { |
| 49 |
wordSmith: Model.extend({ |
| 50 |
blogPost: belongsTo(), |
| 51 |
}), |
| 52 |
blogPost: Model.extend({ |
| 53 |
happyCategory: belongsTo(), |
| 54 |
}), |
| 55 |
happyCategory: Model.extend(), |
| 56 |
}); |
| 57 |
let wordSmith = schema.wordSmiths.create(); |
| 58 |
wordSmith.createBlogPost(); |
| 59 |
|
| 60 |
serializer.request = { |
| 61 |
queryParams: { include: "blog-post.happy-category" }, |
| 62 |
}; |
| 63 |
|
| 64 |
serializer._createRequestedIncludesGraph(wordSmith); |
| 65 |
|
| 66 |
expect(serializer.request._includesGraph).toEqual({ |
| 67 |
data: { |
| 68 |
"word-smith:1": { |
| 69 |
relationships: { |
| 70 |
"blog-post": "blog-post:1", |
| 71 |
}, |
| 72 |
}, |
| 73 |
}, |
| 74 |
included: { |
| 75 |
"blog-posts": { |
| 76 |
"blog-post:1": { |
| 77 |
relationships: { |
| 78 |
"happy-category": undefined, |
| 79 |
}, |
| 80 |
}, |
| 81 |
}, |
| 82 |
}, |
| 83 |
}); |
| 84 |
}); |
| 85 |
|
| 86 |
test("it works on models with belongsTo relationships", () => { |
| 87 |
let schema = new Schema(new Db(), { |
| 88 |
wordSmith: Model.extend({ |
| 89 |
redTag: belongsTo(), |
| 90 |
bluePost: belongsTo(), |
| 91 |
}), |
| 92 |
bluePost: Model.extend({ |
| 93 |
redTag: belongsTo(), |
| 94 |
}), |
| 95 |
redTag: Model.extend(), |
| 96 |
}); |
| 97 |
let wordSmith = schema.wordSmiths.create(); |
| 98 |
wordSmith.createRedTag(); |
| 99 |
|
| 100 |
let bluePost = wordSmith.createBluePost(); |
| 101 |
bluePost.createRedTag(); |
| 102 |
|
| 103 |
serializer.request = { queryParams: { include: "red-tag,blue-post" } }; |
| 104 |
|
| 105 |
serializer._createRequestedIncludesGraph(wordSmith); |
| 106 |
|
| 107 |
expect(serializer.request._includesGraph).toEqual({ |
| 108 |
data: { |
| 109 |
"word-smith:1": { |
| 110 |
relationships: { |
| 111 |
"red-tag": "red-tag:1", |
| 112 |
"blue-post": "blue-post:1", |
| 113 |
}, |
| 114 |
}, |
| 115 |
}, |
| 116 |
included: { |
| 117 |
"red-tags": { |
| 118 |
"red-tag:1": {}, |
| 119 |
}, |
| 120 |
"blue-posts": { |
| 121 |
"blue-post:1": {}, |
| 122 |
}, |
| 123 |
}, |
| 124 |
}); |
| 125 |
}); |
| 126 |
|
| 127 |
test("it works on models with belongsTo relationships and dot-path includes", () => { |
| 128 |
let schema = new Schema(new Db(), { |
| 129 |
wordSmith: Model.extend({ |
| 130 |
redTag: belongsTo(), |
| 131 |
bluePost: belongsTo(), |
| 132 |
}), |
| 133 |
bluePost: Model.extend({ |
| 134 |
redTag: belongsTo(), |
| 135 |
}), |
| 136 |
redTag: Model.extend({ |
| 137 |
someColor: belongsTo(), |
| 138 |
}), |
| 139 |
someColor: Model.extend({}), |
| 140 |
}); |
| 141 |
let wordSmith = schema.wordSmiths.create(); |
| 142 |
wordSmith.createRedTag(); |
| 143 |
|
| 144 |
let bluePost = wordSmith.createBluePost(); |
| 145 |
let redTag = bluePost.createRedTag(); |
| 146 |
redTag.createSomeColor(); |
| 147 |
|
| 148 |
serializer.request = { |
| 149 |
queryParams: { include: "red-tag,blue-post.red-tag.some-color" }, |
| 150 |
}; |
| 151 |
|
| 152 |
serializer._createRequestedIncludesGraph(wordSmith); |
| 153 |
|
| 154 |
expect(serializer.request._includesGraph).toEqual({ |
| 155 |
data: { |
| 156 |
"word-smith:1": { |
| 157 |
relationships: { |
| 158 |
"red-tag": "red-tag:1", |
| 159 |
"blue-post": "blue-post:1", |
| 160 |
}, |
| 161 |
}, |
| 162 |
}, |
| 163 |
included: { |
| 164 |
"red-tags": { |
| 165 |
"red-tag:1": {}, |
| 166 |
"red-tag:2": { |
| 167 |
relationships: { |
| 168 |
"some-color": "some-color:1", |
| 169 |
}, |
| 170 |
}, |
| 171 |
}, |
| 172 |
"blue-posts": { |
| 173 |
"blue-post:1": { |
| 174 |
relationships: { |
| 175 |
"red-tag": "red-tag:2", |
| 176 |
}, |
| 177 |
}, |
| 178 |
}, |
| 179 |
"some-colors": { |
| 180 |
"some-color:1": {}, |
| 181 |
}, |
| 182 |
}, |
| 183 |
}); |
| 184 |
}); |
| 185 |
|
| 186 |
test("it works on models with hasMany relationships", () => { |
| 187 |
let schema = new Schema(new Db(), { |
| 188 |
wordSmith: Model.extend({ |
| 189 |
redTags: hasMany(), |
| 190 |
bluePosts: hasMany(), |
| 191 |
}), |
| 192 |
bluePost: Model.extend({ |
| 193 |
redTags: hasMany(), |
| 194 |
}), |
| 195 |
redTag: Model.extend(), |
| 196 |
}); |
| 197 |
let wordSmith = schema.wordSmiths.create(); |
| 198 |
wordSmith.createRedTag(); |
| 199 |
wordSmith.createRedTag(); |
| 200 |
|
| 201 |
let bluePost = wordSmith.createBluePost(); |
| 202 |
bluePost.createRedTag(); |
| 203 |
|
| 204 |
serializer.request = { queryParams: { include: "red-tags,blue-posts" } }; |
| 205 |
|
| 206 |
serializer._createRequestedIncludesGraph(wordSmith); |
| 207 |
|
| 208 |
expect(serializer.request._includesGraph).toEqual({ |
| 209 |
data: { |
| 210 |
"word-smith:1": { |
| 211 |
relationships: { |
| 212 |
"red-tags": ["red-tag:1", "red-tag:2"], |
| 213 |
"blue-posts": ["blue-post:1"], |
| 214 |
}, |
| 215 |
}, |
| 216 |
}, |
| 217 |
included: { |
| 218 |
"red-tags": { |
| 219 |
"red-tag:1": {}, |
| 220 |
"red-tag:2": {}, |
| 221 |
}, |
| 222 |
"blue-posts": { |
| 223 |
"blue-post:1": {}, |
| 224 |
}, |
| 225 |
}, |
| 226 |
}); |
| 227 |
}); |
| 228 |
|
| 229 |
test("it works on models with hasMany relationships and dot-path includes", () => { |
| 230 |
let schema = new Schema(new Db(), { |
| 231 |
wordSmith: Model.extend({ |
| 232 |
redTags: hasMany(), |
| 233 |
bluePosts: hasMany(), |
| 234 |
}), |
| 235 |
bluePost: Model.extend({ |
| 236 |
redTags: hasMany(), |
| 237 |
}), |
| 238 |
redTag: Model.extend({ |
| 239 |
someColors: hasMany(), |
| 240 |
}), |
| 241 |
someColor: Model.extend(), |
| 242 |
}); |
| 243 |
let wordSmith = schema.wordSmiths.create(); |
| 244 |
wordSmith.createRedTag(); |
| 245 |
wordSmith.createRedTag(); |
| 246 |
|
| 247 |
let bluePost = wordSmith.createBluePost(); |
| 248 |
let redTag = bluePost.createRedTag(); |
| 249 |
redTag.createSomeColor(); |
| 250 |
|
| 251 |
serializer.request = { |
| 252 |
queryParams: { include: "red-tags,blue-posts.red-tags.some-colors" }, |
| 253 |
}; |
| 254 |
|
| 255 |
serializer._createRequestedIncludesGraph(wordSmith); |
| 256 |
|
| 257 |
expect(serializer.request._includesGraph).toEqual({ |
| 258 |
data: { |
| 259 |
"word-smith:1": { |
| 260 |
relationships: { |
| 261 |
"red-tags": ["red-tag:1", "red-tag:2"], |
| 262 |
"blue-posts": ["blue-post:1"], |
| 263 |
}, |
| 264 |
}, |
| 265 |
}, |
| 266 |
included: { |
| 267 |
"red-tags": { |
| 268 |
"red-tag:1": {}, |
| 269 |
"red-tag:2": {}, |
| 270 |
"red-tag:3": { |
| 271 |
relationships: { |
| 272 |
"some-colors": ["some-color:1"], |
| 273 |
}, |
| 274 |
}, |
| 275 |
}, |
| 276 |
"blue-posts": { |
| 277 |
"blue-post:1": { |
| 278 |
relationships: { |
| 279 |
"red-tags": ["red-tag:3"], |
| 280 |
}, |
| 281 |
}, |
| 282 |
}, |
| 283 |
"some-colors": { |
| 284 |
"some-color:1": {}, |
| 285 |
}, |
| 286 |
}, |
| 287 |
}); |
| 288 |
}); |
| 289 |
}); |