|
1 |
| -import { IDLE_NAVIGATION } from "../index"; |
| 1 | +import { |
| 2 | + IDLE_NAVIGATION, |
| 3 | + createBrowserHistory, |
| 4 | + createMemoryHistory, |
| 5 | + createRouter, |
| 6 | +} from "../index"; |
| 7 | +import { replace } from "../utils"; |
2 | 8 | import type { TestRouteObject } from "./utils/data-router-setup";
|
3 | 9 | import { cleanup, setup } from "./utils/data-router-setup";
|
4 |
| -import { createFormData } from "./utils/utils"; |
| 10 | +import { createFormData, tick } from "./utils/utils"; |
5 | 11 |
|
6 | 12 | describe("redirects", () => {
|
7 | 13 | afterEach(() => cleanup());
|
@@ -642,6 +648,70 @@ describe("redirects", () => {
|
642 | 648 | });
|
643 | 649 | });
|
644 | 650 |
|
| 651 | + it("supports replace() redirects", async () => { |
| 652 | + let router = createRouter({ |
| 653 | + history: createMemoryHistory(), |
| 654 | + routes: [ |
| 655 | + { |
| 656 | + path: "/", |
| 657 | + }, |
| 658 | + { |
| 659 | + path: "/a", |
| 660 | + }, |
| 661 | + { |
| 662 | + path: "/b", |
| 663 | + loader: () => replace("/c"), |
| 664 | + }, |
| 665 | + { |
| 666 | + path: "/c", |
| 667 | + }, |
| 668 | + ], |
| 669 | + }); |
| 670 | + router.initialize(); |
| 671 | + await tick(); |
| 672 | + |
| 673 | + // ['/'] |
| 674 | + expect(router.state).toMatchObject({ |
| 675 | + historyAction: "POP", |
| 676 | + location: { |
| 677 | + pathname: "/", |
| 678 | + state: null, |
| 679 | + }, |
| 680 | + }); |
| 681 | + |
| 682 | + // Push /a: ['/', '/a'] |
| 683 | + await router.navigate("/a"); |
| 684 | + expect(router.state).toMatchObject({ |
| 685 | + historyAction: "PUSH", |
| 686 | + location: { |
| 687 | + pathname: "/a", |
| 688 | + state: null, |
| 689 | + }, |
| 690 | + }); |
| 691 | + |
| 692 | + // Push /b which calls replace('/c'): ['/', '/c'] |
| 693 | + await router.navigate("/b"); |
| 694 | + expect(router.state).toMatchObject({ |
| 695 | + historyAction: "REPLACE", |
| 696 | + location: { |
| 697 | + pathname: "/c", |
| 698 | + state: { |
| 699 | + _isRedirect: true, |
| 700 | + }, |
| 701 | + }, |
| 702 | + }); |
| 703 | + |
| 704 | + // Pop: ['/'] |
| 705 | + await router.navigate(-1); |
| 706 | + expect(router.state).toMatchObject({ |
| 707 | + historyAction: "POP", |
| 708 | + location: { |
| 709 | + pathname: "/", |
| 710 | + state: null, |
| 711 | + }, |
| 712 | + }); |
| 713 | + }); |
| 714 | + |
645 | 715 | describe("redirect status code handling", () => {
|
646 | 716 | it("should not treat 300 as a redirect", async () => {
|
647 | 717 | let t = setup({ routes: REDIRECT_ROUTES });
|
|
0 commit comments