From c03cd5410821bcd9ac46dac2e5ecbd55e9e123a3 Mon Sep 17 00:00:00 2001 From: Lars Mikkelsen Date: Thu, 12 Nov 2020 09:13:40 -0500 Subject: [PATCH] test: add tests for match --- src/tests/utils.spec.ts | 42 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/tests/utils.spec.ts b/src/tests/utils.spec.ts index bb6a55b..0d5ce5d 100644 --- a/src/tests/utils.spec.ts +++ b/src/tests/utils.spec.ts @@ -1,4 +1,5 @@ import { normalizePathname, shouldPushState } from '../utils/helpers'; +import { match } from '../utils/match'; describe('utils', () => { it('shouldPushState if search are different', () => { @@ -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({}); + }); });