forked from redwoodjs/redwood
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feat/api-skip-prebuild-try-2' of github.com:dac09/redwo…
…od into feat/api-skip-prebuild-try-2 * 'feat/api-skip-prebuild-try-2' of github.com:dac09/redwood: chore(router): Miniscule fixes chore(router): Move useMatch to its own file (redwoodjs#9770) Allow GraphQL documents to be typed with a TypedDocumentNode for fully-typed result and variables objects (redwoodjs#9619) chore(ci): retry detectChanges on error, and await result (redwoodjs#9772)
- Loading branch information
Showing
12 changed files
with
196 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.