diff --git a/packages/router/src/__tests__/links.test.tsx b/packages/router/src/__tests__/links.test.tsx index 0b364ea11cb9..929b3c68104f 100644 --- a/packages/router/src/__tests__/links.test.tsx +++ b/packages/router/src/__tests__/links.test.tsx @@ -2,9 +2,8 @@ import React from 'react' import { render } from '@testing-library/react' -import { NavLink, useMatch, Link } from '../links' +import { NavLink } from '../links' import { LocationProvider } from '../location' -import { flattenSearchParams } from '../util' function createDummyLocation(pathname: string, search = '') { return { @@ -279,77 +278,3 @@ describe('', () => { expect(getByText(/Dunder Mifflin/)).not.toHaveClass('activeTest') }) }) - -describe('useMatch', () => { - const MyLink = ({ - to, - ...rest - }: React.ComponentPropsWithoutRef) => { - const [pathname, queryString] = to.split('?') - const matchInfo = useMatch(pathname, { - searchParams: flattenSearchParams(queryString), - }) - - return ( - - ) - } - - it('returns a match on the same pathname', () => { - const mockLocation = createDummyLocation('/dunder-mifflin') - - const { getByText } = render( - - Dunder Mifflin - - ) - - expect(getByText(/Dunder Mifflin/)).toHaveStyle('color: green') - }) - - it('returns a match on the same pathname with search parameters', () => { - const mockLocation = createDummyLocation( - '/search-params', - '?page=1&tab=main' - ) - - const { getByText } = render( - - Dunder Mifflin - - ) - - expect(getByText(/Dunder Mifflin/)).toHaveStyle('color: green') - }) - - it('does NOT receive active class on different path', () => { - const mockLocation = createDummyLocation('/staples') - - const { getByText } = render( - - Dunder Mifflin - - ) - - expect(getByText(/Dunder Mifflin/)).toHaveStyle('color: red') - }) - - it('does NOT receive active class on the same pathname with different parameters', () => { - const mockLocation = createDummyLocation( - '/search-params', - '?tab=main&page=1' - ) - - const { getByText } = render( - - Dunder Mifflin - - ) - - expect(getByText(/Dunder Mifflin/)).toHaveStyle('color: red') - }) -}) diff --git a/packages/router/src/__tests__/useMatch.test.tsx b/packages/router/src/__tests__/useMatch.test.tsx new file mode 100644 index 000000000000..89d7bcdd8419 --- /dev/null +++ b/packages/router/src/__tests__/useMatch.test.tsx @@ -0,0 +1,100 @@ +import React from 'react' + +import { render } from '@testing-library/react' + +import { Link } from '../links' +import { LocationProvider } from '../location' +import { useMatch } from '../useMatch' +import { flattenSearchParams } from '../util' + +function createDummyLocation(pathname: string, search = '') { + return { + pathname, + hash: '', + host: '', + hostname: '', + href: '', + ancestorOrigins: null, + assign: () => null, + reload: () => null, + replace: () => null, + origin: '', + port: '', + protocol: '', + search, + } +} + +describe('useMatch', () => { + const MyLink = ({ + to, + ...rest + }: React.ComponentPropsWithoutRef) => { + const [pathname, queryString] = to.split('?') + const matchInfo = useMatch(pathname, { + searchParams: flattenSearchParams(queryString), + }) + + return ( + + ) + } + + it('returns a match on the same pathname', () => { + const mockLocation = createDummyLocation('/dunder-mifflin') + + const { getByText } = render( + + Dunder Mifflin + + ) + + expect(getByText(/Dunder Mifflin/)).toHaveStyle('color: green') + }) + + it('returns a match on the same pathname with search parameters', () => { + const mockLocation = createDummyLocation( + '/search-params', + '?page=1&tab=main' + ) + + const { getByText } = render( + + Dunder Mifflin + + ) + + expect(getByText(/Dunder Mifflin/)).toHaveStyle('color: green') + }) + + it('does NOT receive active class on different path', () => { + const mockLocation = createDummyLocation('/staples') + + const { getByText } = render( + + Dunder Mifflin + + ) + + expect(getByText(/Dunder Mifflin/)).toHaveStyle('color: red') + }) + + it('does NOT receive active class on the same pathname with different parameters', () => { + const mockLocation = createDummyLocation( + '/search-params', + '?tab=main&page=1' + ) + + const { getByText } = render( + + Dunder Mifflin + + ) + + expect(getByText(/Dunder Mifflin/)).toHaveStyle('color: red') + }) +})