| 1 |
import { Server, JSONAPISerializer, Model, belongsTo, hasMany } from "miragejs"; |
| 2 |
|
| 3 |
describe("Integration | Server | Shorthands | Patch with relationships", function () { |
| 4 |
let newServerWithSchema, server; |
| 5 |
|
| 6 |
beforeEach(function () { |
| 7 |
newServerWithSchema = function (schema) { |
| 8 |
server = new Server({ |
| 9 |
environment: "development", |
| 10 |
models: schema, |
| 11 |
serializers: { |
| 12 |
application: JSONAPISerializer, |
| 13 |
}, |
| 14 |
}); |
| 15 |
server.timing = 0; |
| 16 |
server.logging = false; |
| 17 |
|
| 18 |
return server; |
| 19 |
}; |
| 20 |
}); |
| 21 |
|
| 22 |
afterEach(function () { |
| 23 |
server.shutdown(); |
| 24 |
}); |
| 25 |
|
| 26 |
test("it can null out belongs to relationships", async () => { |
| 27 |
let server = newServerWithSchema({ |
| 28 |
author: Model.extend({ |
| 29 |
posts: hasMany(), |
| 30 |
}), |
| 31 |
post: Model.extend({ |
| 32 |
author: belongsTo(), |
| 33 |
}), |
| 34 |
}); |
| 35 |
|
| 36 |
server.patch("/posts/:id"); |
| 37 |
|
| 38 |
let author = server.create("author"); |
| 39 |
let post = server.create("post", { author }); |
| 40 |
|
| 41 |
await fetch(`/posts/${post.id}`, { |
| 42 |
method: "PATCH", |
| 43 |
body: JSON.stringify({ |
| 44 |
data: { |
| 45 |
attributes: { |
| 46 |
title: "Post 1", |
| 47 |
}, |
| 48 |
relationships: { |
| 49 |
author: { |
| 50 |
data: null, |
| 51 |
}, |
| 52 |
}, |
| 53 |
}, |
| 54 |
}), |
| 55 |
}); |
| 56 |
|
| 57 |
post.reload(); |
| 58 |
expect(post.author).toBeNull(); |
| 59 |
}); |
| 60 |
|
| 61 |
test("it can null out belongs to polymorphic relationships", async () => { |
| 62 |
let server = newServerWithSchema({ |
| 63 |
video: Model.extend(), |
| 64 |
post: Model.extend(), |
| 65 |
comment: Model.extend({ |
| 66 |
commentable: belongsTo({ polymorphic: true }), |
| 67 |
}), |
| 68 |
}); |
| 69 |
server.patch("/comments/:id"); |
| 70 |
|
| 71 |
let video = server.create("video"); |
| 72 |
let comment = server.create("comment", { |
| 73 |
commentable: video, |
| 74 |
}); |
| 75 |
|
| 76 |
await fetch(`/comments/${comment.id}`, { |
| 77 |
method: "PATCH", |
| 78 |
body: JSON.stringify({ |
| 79 |
data: { |
| 80 |
attributes: { |
| 81 |
title: "Post 1", |
| 82 |
}, |
| 83 |
relationships: { |
| 84 |
commentable: { |
| 85 |
data: null, |
| 86 |
}, |
| 87 |
}, |
| 88 |
}, |
| 89 |
}), |
| 90 |
}); |
| 91 |
|
| 92 |
comment.reload(); |
| 93 |
expect(comment.commentable).toBeNull(); |
| 94 |
}); |
| 95 |
|
| 96 |
test("it can null out has many polymorphic relationships", async () => { |
| 97 |
let server = newServerWithSchema({ |
| 98 |
car: Model.extend(), |
| 99 |
watch: Model.extend(), |
| 100 |
user: Model.extend({ |
| 101 |
collectibles: hasMany({ polymorphic: true }), |
| 102 |
}), |
| 103 |
}); |
| 104 |
server.patch("/users/:id"); |
| 105 |
|
| 106 |
let car = server.create("car"); |
| 107 |
let watch = server.create("watch"); |
| 108 |
let user = server.create("user", { |
| 109 |
collectibles: [car, watch], |
| 110 |
}); |
| 111 |
|
| 112 |
await fetch(`/users/${user.id}`, { |
| 113 |
method: "PATCH", |
| 114 |
body: JSON.stringify({ |
| 115 |
data: { |
| 116 |
attributes: {}, |
| 117 |
relationships: { |
| 118 |
collectibles: { |
| 119 |
data: null, |
| 120 |
}, |
| 121 |
}, |
| 122 |
}, |
| 123 |
}), |
| 124 |
}); |
| 125 |
|
| 126 |
user.reload(); |
| 127 |
expect(user.collectibles).toHaveLength(0); |
| 128 |
}); |
| 129 |
|
| 130 |
test("it camelizes relationship names", async () => { |
| 131 |
let server = newServerWithSchema({ |
| 132 |
postAuthor: Model.extend({ |
| 133 |
posts: hasMany(), |
| 134 |
}), |
| 135 |
post: Model.extend({ |
| 136 |
postAuthor: belongsTo(), |
| 137 |
}), |
| 138 |
}); |
| 139 |
|
| 140 |
server.patch("/posts/:id"); |
| 141 |
|
| 142 |
let postAuthor = server.create("post-author"); |
| 143 |
let post = server.create("post"); |
| 144 |
|
| 145 |
await fetch(`/posts/${post.id}`, { |
| 146 |
method: "PATCH", |
| 147 |
body: JSON.stringify({ |
| 148 |
data: { |
| 149 |
attributes: {}, |
| 150 |
relationships: { |
| 151 |
"post-author": { |
| 152 |
data: { |
| 153 |
id: postAuthor.id, |
| 154 |
type: "post-authors", |
| 155 |
}, |
| 156 |
}, |
| 157 |
}, |
| 158 |
}, |
| 159 |
}), |
| 160 |
}); |
| 161 |
|
| 162 |
post.reload(); |
| 163 |
expect(post.postAuthorId).toEqual(postAuthor.id); |
| 164 |
}); |
| 165 |
}); |