|
1 | 1 | import * as React from 'react'; |
2 | | -import { screen, render, waitFor } from '@testing-library/react'; |
| 2 | +import { screen, render, waitFor, act } from '@testing-library/react'; |
3 | 3 | import { QueryClient } from '@tanstack/react-query'; |
4 | 4 | import expect from 'expect'; |
5 | 5 |
|
6 | 6 | import { testDataProvider } from './testDataProvider'; |
7 | 7 | import { CoreAdminContext } from '../core'; |
8 | 8 | import { useUpdateMany } from './useUpdateMany'; |
9 | | -import { UndefinedValues } from './useUpdateMany.stories'; |
| 9 | +import { UndefinedValues, WithMiddlewares } from './useUpdateMany.stories'; |
10 | 10 |
|
11 | 11 | describe('useUpdateMany', () => { |
12 | 12 | it('returns a callback that can be used with update arguments', async () => { |
@@ -436,4 +436,198 @@ describe('useUpdateMany', () => { |
436 | 436 | ); // and not [{"title":"world"},{"title":"world"}] |
437 | 437 | }); |
438 | 438 | }); |
| 439 | + |
| 440 | + describe('middlewares', () => { |
| 441 | + it('when pessimistic, it accepts middlewares and displays result and success side effects when dataProvider promise resolves', async () => { |
| 442 | + render(<WithMiddlewares mutationMode="pessimistic" timeout={10} />); |
| 443 | + screen.getByText('Update title').click(); |
| 444 | + await waitFor(() => { |
| 445 | + expect(screen.queryByText('success')).toBeNull(); |
| 446 | + expect( |
| 447 | + screen.queryByText('Hello World from middleware') |
| 448 | + ).toBeNull(); |
| 449 | + expect(screen.queryByText('mutating')).not.toBeNull(); |
| 450 | + }); |
| 451 | + await waitFor(() => { |
| 452 | + expect(screen.queryByText('success')).not.toBeNull(); |
| 453 | + expect( |
| 454 | + // We could expect 'Hello World from middleware' here, but |
| 455 | + // updateMany's result only contains the ids, not the updated data |
| 456 | + // so the cache can only be updated with the call-time params, |
| 457 | + // which do not include the middleware's result. |
| 458 | + // I guess it's OK for most cases though... |
| 459 | + screen.queryByText('Hello World') |
| 460 | + ).not.toBeNull(); |
| 461 | + expect(screen.queryByText('mutating')).toBeNull(); |
| 462 | + }); |
| 463 | + screen.getByText('Refetch').click(); |
| 464 | + await waitFor(() => { |
| 465 | + expect(screen.queryByText('success')).not.toBeNull(); |
| 466 | + expect( |
| 467 | + screen.queryByText('Hello World from middleware') |
| 468 | + ).not.toBeNull(); |
| 469 | + expect(screen.queryByText('mutating')).toBeNull(); |
| 470 | + }); |
| 471 | + }); |
| 472 | + |
| 473 | + it('when pessimistic, it accepts middlewares and displays error and error side effects when dataProvider promise rejects', async () => { |
| 474 | + jest.spyOn(console, 'error').mockImplementation(() => {}); |
| 475 | + render( |
| 476 | + <WithMiddlewares |
| 477 | + mutationMode="pessimistic" |
| 478 | + shouldError |
| 479 | + timeout={10} |
| 480 | + /> |
| 481 | + ); |
| 482 | + screen.getByText('Update title').click(); |
| 483 | + await waitFor(() => { |
| 484 | + expect(screen.queryByText('success')).toBeNull(); |
| 485 | + expect(screen.queryByText('something went wrong')).toBeNull(); |
| 486 | + expect( |
| 487 | + screen.queryByText('Hello World from middleware') |
| 488 | + ).toBeNull(); |
| 489 | + expect(screen.queryByText('mutating')).not.toBeNull(); |
| 490 | + }); |
| 491 | + await waitFor(() => { |
| 492 | + expect(screen.queryByText('success')).toBeNull(); |
| 493 | + expect( |
| 494 | + screen.queryByText('something went wrong') |
| 495 | + ).not.toBeNull(); |
| 496 | + expect( |
| 497 | + screen.queryByText('Hello World from middleware') |
| 498 | + ).toBeNull(); |
| 499 | + expect(screen.queryByText('mutating')).toBeNull(); |
| 500 | + }); |
| 501 | + }); |
| 502 | + |
| 503 | + it('when optimistic, it accepts middlewares and displays result and success side effects right away', async () => { |
| 504 | + render(<WithMiddlewares mutationMode="optimistic" timeout={10} />); |
| 505 | + screen.getByText('Update title').click(); |
| 506 | + await waitFor(() => { |
| 507 | + expect(screen.queryByText('success')).not.toBeNull(); |
| 508 | + expect( |
| 509 | + screen.queryByText('Hello World from middleware') |
| 510 | + ).not.toBeNull(); |
| 511 | + }); |
| 512 | + await waitFor(() => { |
| 513 | + expect(screen.queryByText('success')).not.toBeNull(); |
| 514 | + expect( |
| 515 | + screen.queryByText('Hello World from middleware') |
| 516 | + ).not.toBeNull(); |
| 517 | + expect(screen.queryByText('mutating')).toBeNull(); |
| 518 | + }); |
| 519 | + }); |
| 520 | + it('when optimistic, it accepts middlewares and displays error and error side effects when dataProvider promise rejects', async () => { |
| 521 | + jest.spyOn(console, 'error').mockImplementation(() => {}); |
| 522 | + render( |
| 523 | + <WithMiddlewares |
| 524 | + mutationMode="optimistic" |
| 525 | + shouldError |
| 526 | + timeout={10} |
| 527 | + /> |
| 528 | + ); |
| 529 | + screen.getByText('Update title').click(); |
| 530 | + await waitFor(() => { |
| 531 | + expect(screen.queryByText('success')).not.toBeNull(); |
| 532 | + expect(screen.queryByText('Hello World')).not.toBeNull(); |
| 533 | + expect(screen.queryByText('mutating')).not.toBeNull(); |
| 534 | + }); |
| 535 | + await waitFor(() => { |
| 536 | + expect(screen.queryByText('success')).toBeNull(); |
| 537 | + expect( |
| 538 | + screen.queryByText('something went wrong') |
| 539 | + ).not.toBeNull(); |
| 540 | + expect( |
| 541 | + screen.queryByText('Hello World from middleware') |
| 542 | + ).toBeNull(); |
| 543 | + expect(screen.queryByText('mutating')).toBeNull(); |
| 544 | + }); |
| 545 | + await screen.findByText('Hello'); |
| 546 | + }); |
| 547 | + |
| 548 | + it('when undoable, it accepts middlewares and displays result and success side effects right away and fetched on confirm', async () => { |
| 549 | + render(<WithMiddlewares mutationMode="undoable" timeout={10} />); |
| 550 | + act(() => { |
| 551 | + screen.getByText('Update title').click(); |
| 552 | + }); |
| 553 | + await waitFor(() => { |
| 554 | + expect(screen.queryByText('success')).not.toBeNull(); |
| 555 | + expect(screen.queryByText('Hello World')).not.toBeNull(); |
| 556 | + expect(screen.queryByText('mutating')).toBeNull(); |
| 557 | + }); |
| 558 | + act(() => { |
| 559 | + screen.getByText('Confirm').click(); |
| 560 | + }); |
| 561 | + await waitFor(() => { |
| 562 | + expect(screen.queryByText('success')).not.toBeNull(); |
| 563 | + expect(screen.queryByText('Hello World')).not.toBeNull(); |
| 564 | + expect(screen.queryByText('mutating')).not.toBeNull(); |
| 565 | + }); |
| 566 | + await waitFor( |
| 567 | + () => { |
| 568 | + expect(screen.queryByText('mutating')).toBeNull(); |
| 569 | + }, |
| 570 | + { timeout: 4000 } |
| 571 | + ); |
| 572 | + expect(screen.queryByText('success')).not.toBeNull(); |
| 573 | + expect( |
| 574 | + screen.queryByText('Hello World from middleware') |
| 575 | + ).not.toBeNull(); |
| 576 | + }); |
| 577 | + it('when undoable, it accepts middlewares and displays result and success side effects right away and reverts on cancel', async () => { |
| 578 | + render(<WithMiddlewares mutationMode="undoable" timeout={10} />); |
| 579 | + await screen.findByText('Hello'); |
| 580 | + act(() => { |
| 581 | + screen.getByText('Update title').click(); |
| 582 | + }); |
| 583 | + await waitFor(() => { |
| 584 | + expect(screen.queryByText('success')).not.toBeNull(); |
| 585 | + expect(screen.queryByText('Hello World')).not.toBeNull(); |
| 586 | + expect(screen.queryByText('mutating')).toBeNull(); |
| 587 | + }); |
| 588 | + act(() => { |
| 589 | + screen.getByText('Cancel').click(); |
| 590 | + }); |
| 591 | + await waitFor(() => { |
| 592 | + expect(screen.queryByText('Hello World')).toBeNull(); |
| 593 | + }); |
| 594 | + expect(screen.queryByText('mutating')).toBeNull(); |
| 595 | + await screen.findByText('Hello'); |
| 596 | + }); |
| 597 | + it('when undoable, it accepts middlewares and displays result and success side effects right away and reverts on error', async () => { |
| 598 | + jest.spyOn(console, 'error').mockImplementation(() => {}); |
| 599 | + render( |
| 600 | + <WithMiddlewares |
| 601 | + mutationMode="undoable" |
| 602 | + shouldError |
| 603 | + timeout={10} |
| 604 | + /> |
| 605 | + ); |
| 606 | + await screen.findByText('Hello'); |
| 607 | + screen.getByText('Update title').click(); |
| 608 | + await waitFor(() => { |
| 609 | + expect(screen.queryByText('success')).not.toBeNull(); |
| 610 | + expect(screen.queryByText('Hello World')).not.toBeNull(); |
| 611 | + expect(screen.queryByText('mutating')).toBeNull(); |
| 612 | + }); |
| 613 | + screen.getByText('Confirm').click(); |
| 614 | + await waitFor(() => { |
| 615 | + expect(screen.queryByText('success')).not.toBeNull(); |
| 616 | + expect(screen.queryByText('Hello World')).not.toBeNull(); |
| 617 | + expect(screen.queryByText('mutating')).not.toBeNull(); |
| 618 | + }); |
| 619 | + await screen.findByText('Hello', undefined, { timeout: 4000 }); |
| 620 | + await waitFor(() => { |
| 621 | + expect(screen.queryByText('success')).toBeNull(); |
| 622 | + expect( |
| 623 | + screen.queryByText('Hello World from middleware') |
| 624 | + ).toBeNull(); |
| 625 | + expect(screen.queryByText('mutating')).toBeNull(); |
| 626 | + }); |
| 627 | + }); |
| 628 | + }); |
| 629 | +}); |
| 630 | + |
| 631 | +afterEach(() => { |
| 632 | + jest.restoreAllMocks(); |
439 | 633 | }); |
0 commit comments