Skip to content

Commit

Permalink
chore: add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lensbart committed Feb 18, 2024
1 parent c463168 commit 1ac8863
Showing 1 changed file with 161 additions and 0 deletions.
161 changes: 161 additions & 0 deletions packages/react-router/__tests__/context-test.tsx
@@ -0,0 +1,161 @@
/* eslint-disable @typescript-eslint/no-unused-vars -- type tests */
/* eslint-disable jest/expect-expect -- type tests */
import * as React from "react";
import {
UNSAFE_LocationContext as LocationContext,
NavigationType,
} from "react-router";

const location = {
pathname: "",
search: "",
hash: "",
state: null,
key: "default",
} as const;

describe("LocationContext", () => {
it("accepts an object with the correct `pathname`", () => {
const validCases = [
<LocationContext.Provider
value={{
location: { ...location, pathname: "/" },
navigationType: NavigationType.Pop,
}}
/>,
<LocationContext.Provider
value={{
location: { ...location, pathname: "/something" },
navigationType: NavigationType.Pop,
}}
/>,
<LocationContext.Provider
value={{
location: { ...location, pathname: "" },
navigationType: NavigationType.Pop,
}}
/>,
];
});

it("accepts an object with the correct `hash`", () => {
const validCases = [
<LocationContext.Provider
value={{
location: { ...location, hash: "#" },
navigationType: NavigationType.Pop,
}}
/>,
<LocationContext.Provider
value={{
location: { ...location, hash: "#something" },
navigationType: NavigationType.Pop,
}}
/>,
<LocationContext.Provider
value={{
location: { ...location, hash: "" },
navigationType: NavigationType.Pop,
}}
/>,
];
});

it("accepts an object with the correct `search`", () => {
const validCases = [
<LocationContext.Provider
value={{
location: { ...location, search: "?" },
navigationType: NavigationType.Pop,
}}
/>,
<LocationContext.Provider
value={{
location: { ...location, search: "?something" },
navigationType: NavigationType.Pop,
}}
/>,
<LocationContext.Provider
value={{
location: { ...location, search: "" },
navigationType: NavigationType.Pop,
}}
/>,
];
});
});

it("rejects an object with the wrong `pathname`", () => {
const invalidCases = [
<LocationContext.Provider
value={{
location: {
...location,
// @ts-expect-error
pathname: "something",
},
navigationType: NavigationType.Pop,
}}
/>,
<LocationContext.Provider
value={{
location: {
...location,
// @ts-expect-error
pathname: "some/thing",
},
navigationType: NavigationType.Pop,
}}
/>,
];

it("rejects an object with the wrong `hash`", () => {
const invalidCases = [
<LocationContext.Provider
value={{
location: {
...location,
// @ts-expect-error
hash: "something",
},
navigationType: NavigationType.Pop,
}}
/>,
<LocationContext.Provider
value={{
location: {
...location,
// @ts-expect-error
hash: "some#thing",
},
navigationType: NavigationType.Pop,
}}
/>,
];

it("rejects an object with the wrong `search`", () => {
const invalidCases = [
<LocationContext.Provider
value={{
location: {
...location,
// @ts-expect-error
search: "something",
},
navigationType: NavigationType.Pop,
}}
/>,
<LocationContext.Provider
value={{
location: {
...location,
// @ts-expect-error
search: "some?thing",
},
navigationType: NavigationType.Pop,
}}
/>,
];
});
});
});

0 comments on commit 1ac8863

Please sign in to comment.