| 1 |
import { Server, Model, ActiveModelSerializer } from "miragejs"; |
| 2 |
|
| 3 |
describe("Integration | Server | Resource shorthand", function () { |
| 4 |
let server; |
| 5 |
|
| 6 |
beforeEach(function () { |
| 7 |
server = new Server({ |
| 8 |
environment: "test", |
| 9 |
models: { |
| 10 |
contact: Model, |
| 11 |
blogPost: Model, |
| 12 |
}, |
| 13 |
serializers: { |
| 14 |
application: ActiveModelSerializer, |
| 15 |
}, |
| 16 |
}); |
| 17 |
server.timing = 0; |
| 18 |
server.logging = false; |
| 19 |
}); |
| 20 |
|
| 21 |
afterEach(function () { |
| 22 |
server.shutdown(); |
| 23 |
}); |
| 24 |
|
| 25 |
test("resource generates get shorthand for index action", async () => { |
| 26 |
expect.assertions(2); |
| 27 |
|
| 28 |
server.db.loadData({ |
| 29 |
contacts: [ |
| 30 |
{ id: 1, name: "Link" }, |
| 31 |
{ id: 2, name: "Zelda" }, |
| 32 |
], |
| 33 |
blogPosts: [ |
| 34 |
{ id: 1, title: "Post 1" }, |
| 35 |
{ id: 2, title: "Post 2" }, |
| 36 |
], |
| 37 |
}); |
| 38 |
|
| 39 |
server.resource("contacts"); |
| 40 |
|
| 41 |
let res = await fetch("/contacts"); |
| 42 |
let data = await res.json(); |
| 43 |
|
| 44 |
expect(res.status).toEqual(200); |
| 45 |
expect(data).toEqual({ |
| 46 |
contacts: [ |
| 47 |
{ id: "1", name: "Link" }, |
| 48 |
{ id: "2", name: "Zelda" }, |
| 49 |
], |
| 50 |
}); |
| 51 |
}); |
| 52 |
|
| 53 |
test("resource generates get shorthand for show action", async () => { |
| 54 |
expect.assertions(2); |
| 55 |
|
| 56 |
server.db.loadData({ |
| 57 |
contacts: [ |
| 58 |
{ id: 1, name: "Link" }, |
| 59 |
{ id: 2, name: "Zelda" }, |
| 60 |
], |
| 61 |
blogPosts: [ |
| 62 |
{ id: 1, title: "Post 1" }, |
| 63 |
{ id: 2, title: "Post 2" }, |
| 64 |
], |
| 65 |
}); |
| 66 |
|
| 67 |
server.resource("contacts"); |
| 68 |
server.resource("blog-posts", { path: "/posts" }); |
| 69 |
|
| 70 |
let res = await fetch("/contacts/2"); |
| 71 |
let data = await res.json(); |
| 72 |
|
| 73 |
expect(res.status).toEqual(200); |
| 74 |
expect(data).toEqual({ contact: { id: "2", name: "Zelda" } }); |
| 75 |
}); |
| 76 |
|
| 77 |
test("resource generates post shorthand", async () => { |
| 78 |
expect.assertions(2); |
| 79 |
|
| 80 |
server.resource("contacts"); |
| 81 |
|
| 82 |
let res = await fetch("/contacts", { |
| 83 |
method: "POST", |
| 84 |
body: JSON.stringify({ |
| 85 |
contact: { |
| 86 |
name: "Zelda", |
| 87 |
}, |
| 88 |
}), |
| 89 |
}); |
| 90 |
|
| 91 |
expect(res.status).toEqual(201); |
| 92 |
expect(server.db.contacts).toHaveLength(1); |
| 93 |
}); |
| 94 |
|
| 95 |
test("resource generates put shorthand", async () => { |
| 96 |
expect.assertions(2); |
| 97 |
|
| 98 |
server.db.loadData({ |
| 99 |
contacts: [{ id: 1, name: "Link" }], |
| 100 |
blogPosts: [{ id: 1, title: "Post 1" }], |
| 101 |
}); |
| 102 |
|
| 103 |
server.resource("contacts"); |
| 104 |
|
| 105 |
let res = await fetch("/contacts/1", { |
| 106 |
method: "PUT", |
| 107 |
body: JSON.stringify({ |
| 108 |
contact: { |
| 109 |
name: "Zelda", |
| 110 |
}, |
| 111 |
}), |
| 112 |
}); |
| 113 |
|
| 114 |
expect(res.status).toEqual(200); |
| 115 |
expect(server.db.contacts[0].name).toEqual("Zelda"); |
| 116 |
}); |
| 117 |
|
| 118 |
test("resource generates patch shorthand", async () => { |
| 119 |
expect.assertions(2); |
| 120 |
|
| 121 |
server.db.loadData({ |
| 122 |
contacts: [{ id: 1, name: "Link" }], |
| 123 |
blogPosts: [{ id: 1, title: "Post 1" }], |
| 124 |
}); |
| 125 |
|
| 126 |
server.resource("contacts"); |
| 127 |
|
| 128 |
let res = await fetch("/contacts/1", { |
| 129 |
method: "PATCH", |
| 130 |
body: JSON.stringify({ |
| 131 |
contact: { |
| 132 |
name: "Zelda", |
| 133 |
}, |
| 134 |
}), |
| 135 |
}); |
| 136 |
|
| 137 |
expect(res.status).toEqual(200); |
| 138 |
expect(server.db.contacts[0].name).toEqual("Zelda"); |
| 139 |
}); |
| 140 |
|
| 141 |
test("resource generates delete shorthand works", async () => { |
| 142 |
expect.assertions(2); |
| 143 |
|
| 144 |
server.db.loadData({ |
| 145 |
contacts: [{ id: 1, name: "Link" }], |
| 146 |
blogPosts: [{ id: 1, title: "Post 1" }], |
| 147 |
}); |
| 148 |
|
| 149 |
server.resource("contacts"); |
| 150 |
|
| 151 |
let res = await fetch("/contacts/1", { method: "DELETE" }); |
| 152 |
|
| 153 |
expect(res.status).toEqual(204); |
| 154 |
expect(server.db.contacts).toHaveLength(0); |
| 155 |
}); |
| 156 |
|
| 157 |
test("resource accepts a custom path for a resource", async () => { |
| 158 |
expect.assertions(6); |
| 159 |
|
| 160 |
server.db.loadData({ |
| 161 |
blogPosts: [ |
| 162 |
{ id: 1, title: "Post 1" }, |
| 163 |
{ id: 2, title: "Post 2" }, |
| 164 |
], |
| 165 |
}); |
| 166 |
|
| 167 |
server.resource("blog-posts", { path: "/posts" }); |
| 168 |
|
| 169 |
let indexResponse = await fetch("/posts"); |
| 170 |
expect(indexResponse.status).toEqual(200); |
| 171 |
|
| 172 |
let showResponse = await fetch("/posts/2"); |
| 173 |
expect(showResponse.status).toEqual(200); |
| 174 |
|
| 175 |
let createResponse = await fetch("/posts", { |
| 176 |
method: "POST", |
| 177 |
body: JSON.stringify({ |
| 178 |
blog_post: { |
| 179 |
name: "Post 1", |
| 180 |
}, |
| 181 |
}), |
| 182 |
}); |
| 183 |
expect(createResponse.status).toEqual(201); |
| 184 |
|
| 185 |
let updatePutResponse = await fetch("/posts/1", { |
| 186 |
method: "PUT", |
| 187 |
body: JSON.stringify({ |
| 188 |
blog_post: { |
| 189 |
name: "Post 2", |
| 190 |
}, |
| 191 |
}), |
| 192 |
}); |
| 193 |
expect(updatePutResponse.status).toEqual(200); |
| 194 |
|
| 195 |
let updatePatchResponse = await fetch("/posts/1", { |
| 196 |
method: "PATCH", |
| 197 |
body: JSON.stringify({ |
| 198 |
blog_post: { |
| 199 |
name: "Post 2", |
| 200 |
}, |
| 201 |
}), |
| 202 |
}); |
| 203 |
expect(updatePatchResponse.status).toEqual(200); |
| 204 |
|
| 205 |
let deleteResponse = await fetch("/posts/1", { method: "DELETE" }); |
| 206 |
expect(deleteResponse.status).toEqual(204); |
| 207 |
}); |
| 208 |
|
| 209 |
test("resource accepts singular name", async () => { |
| 210 |
expect.assertions(4); |
| 211 |
|
| 212 |
server.db.loadData({ |
| 213 |
contacts: [ |
| 214 |
{ id: 1, name: "Link" }, |
| 215 |
{ id: 2, name: "Zelda" }, |
| 216 |
], |
| 217 |
blogPosts: [ |
| 218 |
{ id: 1, title: "Post 1" }, |
| 219 |
{ id: 2, title: "Post 2" }, |
| 220 |
], |
| 221 |
}); |
| 222 |
|
| 223 |
server.resource("contact"); |
| 224 |
server.resource("blog-post", { path: "/posts" }); |
| 225 |
|
| 226 |
let contactsResponse = await fetch("/contacts"); |
| 227 |
let contactsResponseData = await contactsResponse.json(); |
| 228 |
|
| 229 |
expect(contactsResponse.status).toEqual(200); |
| 230 |
expect(contactsResponseData).toEqual({ |
| 231 |
contacts: [ |
| 232 |
{ id: "1", name: "Link" }, |
| 233 |
{ id: "2", name: "Zelda" }, |
| 234 |
], |
| 235 |
}); |
| 236 |
|
| 237 |
let postsResponse = await fetch("/posts"); |
| 238 |
let postsResponseData = await postsResponse.json(); |
| 239 |
|
| 240 |
expect(postsResponse.status).toEqual(200); |
| 241 |
expect(postsResponseData).toEqual({ |
| 242 |
blog_posts: [ |
| 243 |
{ id: "1", title: "Post 1" }, |
| 244 |
{ id: "2", title: "Post 2" }, |
| 245 |
], |
| 246 |
}); |
| 247 |
}); |
| 248 |
|
| 249 |
test("resource does not accept both :all and :except options", () => { |
| 250 |
expect(() => { |
| 251 |
server.resource("contacts", { only: ["index"], except: ["create"] }); |
| 252 |
}).toThrow("cannot use both :only and :except options"); |
| 253 |
}); |
| 254 |
|
| 255 |
test("resource generates shorthands which are whitelisted by :only option", async () => { |
| 256 |
expect.assertions(1); |
| 257 |
|
| 258 |
server.db.loadData({ |
| 259 |
contacts: [ |
| 260 |
{ id: 1, name: "Link" }, |
| 261 |
{ id: 2, name: "Zelda" }, |
| 262 |
], |
| 263 |
}); |
| 264 |
|
| 265 |
server.resource("contacts", { only: ["index"] }); |
| 266 |
|
| 267 |
let res = await fetch("/contacts"); |
| 268 |
|
| 269 |
expect(res.status).toEqual(200); |
| 270 |
}); |
| 271 |
|
| 272 |
test("resource does not generate shorthands which are not whitelisted with :only option", async () => { |
| 273 |
expect.assertions(5); |
| 274 |
|
| 275 |
server.db.loadData({ |
| 276 |
contacts: [{ id: 1, name: "Link" }], |
| 277 |
}); |
| 278 |
|
| 279 |
server.resource("contacts", { only: ["index"] }); |
| 280 |
|
| 281 |
await expect(fetch("/contacts/1")).rejects.toThrow( |
| 282 |
"Mirage: Your app tried to GET '/contacts/1'" |
| 283 |
); |
| 284 |
await expect(fetch("/contacts", { method: "POST" })).rejects.toThrow( |
| 285 |
"Mirage: Your app tried to POST '/contacts'" |
| 286 |
); |
| 287 |
await expect(fetch("/contacts/1", { method: "PUT" })).rejects.toThrow( |
| 288 |
"Mirage: Your app tried to PUT '/contacts/1'" |
| 289 |
); |
| 290 |
await expect(fetch("/contacts/1", { method: "PATCH" })).rejects.toThrow( |
| 291 |
"Mirage: Your app tried to PATCH '/contacts/1'" |
| 292 |
); |
| 293 |
await expect(fetch("/contacts/1", { method: "DELETE" })).rejects.toThrow( |
| 294 |
"Mirage: Your app tried to DELETE '/contacts/1'" |
| 295 |
); |
| 296 |
}); |
| 297 |
|
| 298 |
test("resource generates shorthands which are not blacklisted by :except option", async () => { |
| 299 |
expect.assertions(2); |
| 300 |
|
| 301 |
server.db.loadData({ |
| 302 |
contacts: [{ id: 1, name: "Link" }], |
| 303 |
}); |
| 304 |
|
| 305 |
server.resource("contacts", { except: ["create", "update", "delete"] }); |
| 306 |
|
| 307 |
let indexResponse = await fetch("/contacts"); |
| 308 |
expect(indexResponse.status).toEqual(200); |
| 309 |
|
| 310 |
let showResponse = await fetch("/contacts/1"); |
| 311 |
expect(showResponse.status).toEqual(200); |
| 312 |
}); |
| 313 |
|
| 314 |
test("resource does not generate shorthands which are blacklisted by :except option", async () => { |
| 315 |
expect.assertions(4); |
| 316 |
|
| 317 |
server.db.loadData({ |
| 318 |
contacts: [{ id: 1, name: "Link" }], |
| 319 |
}); |
| 320 |
|
| 321 |
server.resource("contacts", { except: ["create", "update", "delete"] }); |
| 322 |
|
| 323 |
await expect(fetch("/contacts", { method: "POST" })).rejects.toThrow( |
| 324 |
"Mirage: Your app tried to POST '/contacts'" |
| 325 |
); |
| 326 |
await expect(fetch("/contacts/1", { method: "PUT" })).rejects.toThrow( |
| 327 |
"Mirage: Your app tried to PUT '/contacts/1'" |
| 328 |
); |
| 329 |
await expect(fetch("/contacts/1", { method: "PATCH" })).rejects.toThrow( |
| 330 |
"Mirage: Your app tried to PATCH '/contacts/1'" |
| 331 |
); |
| 332 |
await expect(fetch("/contacts/1", { method: "DELETE" })).rejects.toThrow( |
| 333 |
"Mirage: Your app tried to DELETE '/contacts/1'" |
| 334 |
); |
| 335 |
}); |
| 336 |
}); |