Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add tests for match #118

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/tests/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { normalizePathname, shouldPushState } from '../utils/helpers';
import { match } from '../utils/match';

describe('utils', () => {
it('shouldPushState if search are different', () => {
Expand Down Expand Up @@ -57,4 +58,45 @@ describe('utils', () => {
),
).toBe('/pathname');
});

it('should return a match function', () => {
expect(match('/one')).toBeInstanceOf(Function);
});

it('should match exact', () => {
expect(match('/one', { exact: true })('/one')).toMatchObject({});
expect(match('/one', { exact: true })('/one/')).toMatchObject({});
expect(match('/one', { exact: true })('/one/two')).toBeUndefined();
expect(match('/one', { exact: false })('/one/two')).toMatchObject({});
});

it('should match strict', () => {
expect(match('/one/', { strict: true })('/one')).toBeUndefined();
expect(match('/one/', { strict: true })('/one/')).toMatchObject({});
expect(match('/one/', { strict: true })('/one/two')).toMatchObject({});
expect(match('/one/', { strict: false })('/one')).toMatchObject({});
});

it('should match exact strict', () => {
expect(match('/one', { exact: true, strict: true })('/one')).toMatchObject({});
expect(match('/one', { exact: true, strict: true })('/one/')).toBeUndefined();
expect(match('/one', { exact: true, strict: true })('/one/two')).toBeUndefined();
});

it('should match tokens', () => {
expect(match('/users/:id')('/users/1')).toMatchObject({ id: '1' });
});

it('should match an array', () => {
expect(match(['/one', '/two'])('/one')).toMatchObject({});
expect(match(['/one', '/two'])('/two')).toMatchObject({});
expect(match(['/one', '/two'])('/three')).toBeUndefined();
});

it('should match a regexp', () => {
expect(match(/^\/one/)('/one')).toMatchObject({});
expect(match(/^\/one/)('/two')).toBeUndefined();
expect(match(/^\/two/)('/one')).toBeUndefined();
expect(match(/^\/two/)('/two')).toMatchObject({});
});
});