Skip to content

Commit

Permalink
also move tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobbe committed Dec 28, 2023
1 parent 010b76a commit 346fd7c
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 76 deletions.
77 changes: 1 addition & 76 deletions packages/router/src/__tests__/links.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -279,77 +278,3 @@ describe('<NavLink />', () => {
expect(getByText(/Dunder Mifflin/)).not.toHaveClass('activeTest')
})
})

describe('useMatch', () => {
const MyLink = ({
to,
...rest
}: React.ComponentPropsWithoutRef<typeof Link>) => {
const [pathname, queryString] = to.split('?')
const matchInfo = useMatch(pathname, {
searchParams: flattenSearchParams(queryString),
})

return (
<Link
to={to}
style={{ color: matchInfo.match ? 'green' : 'red' }}
{...rest}
/>
)
}

it('returns a match on the same pathname', () => {
const mockLocation = createDummyLocation('/dunder-mifflin')

const { getByText } = render(
<LocationProvider location={mockLocation}>
<MyLink to="/dunder-mifflin">Dunder Mifflin</MyLink>
</LocationProvider>
)

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(
<LocationProvider location={mockLocation}>
<MyLink to={`/search-params?tab=main&page=1`}>Dunder Mifflin</MyLink>
</LocationProvider>
)

expect(getByText(/Dunder Mifflin/)).toHaveStyle('color: green')
})

it('does NOT receive active class on different path', () => {
const mockLocation = createDummyLocation('/staples')

const { getByText } = render(
<LocationProvider location={mockLocation}>
<MyLink to="/dunder-mifflin">Dunder Mifflin</MyLink>
</LocationProvider>
)

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(
<LocationProvider location={mockLocation}>
<MyLink to={`/search-params?page=2&tab=main`}>Dunder Mifflin</MyLink>
</LocationProvider>
)

expect(getByText(/Dunder Mifflin/)).toHaveStyle('color: red')
})
})
100 changes: 100 additions & 0 deletions packages/router/src/__tests__/useMatch.test.tsx
Original file line number Diff line number Diff line change
@@ -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<typeof Link>) => {
const [pathname, queryString] = to.split('?')
const matchInfo = useMatch(pathname, {
searchParams: flattenSearchParams(queryString),
})

return (
<Link
to={to}
style={{ color: matchInfo.match ? 'green' : 'red' }}
{...rest}
/>
)
}

it('returns a match on the same pathname', () => {
const mockLocation = createDummyLocation('/dunder-mifflin')

const { getByText } = render(
<LocationProvider location={mockLocation}>
<MyLink to="/dunder-mifflin">Dunder Mifflin</MyLink>
</LocationProvider>
)

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(
<LocationProvider location={mockLocation}>
<MyLink to={`/search-params?tab=main&page=1`}>Dunder Mifflin</MyLink>
</LocationProvider>
)

expect(getByText(/Dunder Mifflin/)).toHaveStyle('color: green')
})

it('does NOT receive active class on different path', () => {
const mockLocation = createDummyLocation('/staples')

const { getByText } = render(
<LocationProvider location={mockLocation}>
<MyLink to="/dunder-mifflin">Dunder Mifflin</MyLink>
</LocationProvider>
)

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(
<LocationProvider location={mockLocation}>
<MyLink to={`/search-params?page=2&tab=main`}>Dunder Mifflin</MyLink>
</LocationProvider>
)

expect(getByText(/Dunder Mifflin/)).toHaveStyle('color: red')
})
})

0 comments on commit 346fd7c

Please sign in to comment.