|
1 | 1 | import { expect, fetchMock } from "../test-helper"
|
2 |
| -import { Person, PersonWithExtraAttr } from "../fixtures" |
| 2 | +import { Person, PersonWithExtraAttr, BackgroundJob } from "../fixtures" |
3 | 3 | import { JsonapiRequestDoc, JsonapiResponseDoc } from "../../src/index"
|
4 | 4 |
|
5 | 5 | after(() => {
|
@@ -274,6 +274,54 @@ describe("Model persistence", () => {
|
274 | 274 | })
|
275 | 275 | })
|
276 | 276 | })
|
| 277 | + |
| 278 | + describe("when the server returns 202 accepted", () => { |
| 279 | + beforeEach(() => { |
| 280 | + fetchMock.restore() |
| 281 | + fetchMock.post("http://example.com/api/v1/people", { status: 202 }) |
| 282 | + }) |
| 283 | + |
| 284 | + afterEach(() => { |
| 285 | + resetMocks() |
| 286 | + }) |
| 287 | + |
| 288 | + it("does not blow up", async () => { |
| 289 | + expect(instance.isPersisted).to.eq(false) |
| 290 | + const success = await instance.save() |
| 291 | + |
| 292 | + expect(success).to.be.true |
| 293 | + expect(instance.isPersisted).to.eq(false) |
| 294 | + }) |
| 295 | + }) |
| 296 | + |
| 297 | + describe("when the server returns 202 and a background job object", () => { |
| 298 | + beforeEach(() => { |
| 299 | + fetchMock.restore() |
| 300 | + |
| 301 | + fetchMock.post("http://example.com/api/v1/people", { |
| 302 | + status: 202, |
| 303 | + body: { data: { id: "23", type: "background_jobs", attributes: {} } } |
| 304 | + }) |
| 305 | + }) |
| 306 | + |
| 307 | + afterEach(() => { |
| 308 | + resetMocks() |
| 309 | + }) |
| 310 | + |
| 311 | + it("deserialize the response and return the job object", async () => { |
| 312 | + let job: BackgroundJob = new BackgroundJob() |
| 313 | + let populateJobOnResponse = (response: any) => { |
| 314 | + job = response |
| 315 | + } |
| 316 | + instance.onDeferredUpdate = populateJobOnResponse |
| 317 | + expect(instance.isPersisted).to.eq(false) |
| 318 | + await instance.save() |
| 319 | + |
| 320 | + expect(instance.isPersisted).to.eq(false) |
| 321 | + expect(job.id).to.eq("23") |
| 322 | + expect(job.klass).to.eq(BackgroundJob) |
| 323 | + }) |
| 324 | + }) |
277 | 325 | })
|
278 | 326 |
|
279 | 327 | describe("#destroy", () => {
|
@@ -321,21 +369,48 @@ describe("Model persistence", () => {
|
321 | 369 | beforeEach(() => {
|
322 | 370 | fetchMock.restore()
|
323 | 371 |
|
324 |
| - fetchMock.mock({ |
325 |
| - matcher: "http://example.com/api/v1/people/1", |
326 |
| - response: new Response({ status: 202 } as any) |
327 |
| - }) |
| 372 | + fetchMock.delete("http://example.com/api/v1/people/1", { status: 202 }) |
328 | 373 | })
|
329 | 374 |
|
330 | 375 | afterEach(() => {
|
331 | 376 | resetMocks()
|
332 | 377 | })
|
333 | 378 |
|
334 | 379 | it("does not blow up", async () => {
|
| 380 | + expect(instance.isPersisted).to.eq(true) |
| 381 | + const success = await instance.destroy() |
| 382 | + |
| 383 | + expect(success).to.be.true |
| 384 | + expect(instance.isPersisted).to.eq(true) |
| 385 | + }) |
| 386 | + }) |
| 387 | + |
| 388 | + describe("when the server returns 202 and a background job object", () => { |
| 389 | + beforeEach(() => { |
| 390 | + fetchMock.restore() |
| 391 | + |
| 392 | + fetchMock.delete("http://example.com/api/v1/people/1", { |
| 393 | + status: 202, |
| 394 | + body: { data: { id: "23", type: "background_jobs", attributes: {} } } |
| 395 | + }) |
| 396 | + }) |
| 397 | + |
| 398 | + afterEach(() => { |
| 399 | + resetMocks() |
| 400 | + }) |
| 401 | + |
| 402 | + it("deserialize the response and return the job object", async () => { |
| 403 | + let job: BackgroundJob = new BackgroundJob() |
| 404 | + let populateJobOnResponse = (response: any) => { |
| 405 | + job = response |
| 406 | + } |
| 407 | + instance.onDeferredDestroy = populateJobOnResponse |
335 | 408 | expect(instance.isPersisted).to.eq(true)
|
336 | 409 | await instance.destroy()
|
337 | 410 |
|
338 |
| - expect(instance.isPersisted).to.eq(false) |
| 411 | + expect(instance.isPersisted).to.eq(true) |
| 412 | + expect(job.id).to.eq("23") |
| 413 | + expect(job.klass).to.eq(BackgroundJob) |
339 | 414 | })
|
340 | 415 | })
|
341 | 416 |
|
|
0 commit comments