-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
603db89
commit c639448
Showing
1 changed file
with
43 additions
and
17 deletions.
There are no files selected for viewing
60 changes: 43 additions & 17 deletions
60
...-ui/src/components/summary-grid/cell/location-cell-renderer/LocationCellRenderer.spec.tsx
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 |
---|---|---|
@@ -1,36 +1,62 @@ | ||
import '@testing-library/jest-dom' | ||
import { render, screen } from '@testing-library/react' | ||
import { act, render, screen } from '@testing-library/react' | ||
import { userEvent } from '@testing-library/user-event' | ||
import { BrowserRouter } from 'react-router-dom' | ||
|
||
import { LocationCellRenderer } from './LocationCellRenderer' | ||
|
||
describe('LocationCellRenderer component', () => { | ||
const city = 'Bristol' | ||
const eGridCell = document.createElement('div') | ||
const mockAddEventListener = jest.fn() | ||
|
||
const renderComponent = () => { | ||
const user = userEvent.setup() | ||
render(<LocationCellRenderer value={city} eGridCell={eGridCell} />, { | ||
wrapper: BrowserRouter, | ||
}) | ||
return { user } | ||
describe('LocationCellRenderer component', () => { | ||
const eGridCell: Partial<HTMLElement> = { | ||
addEventListener: mockAddEventListener, | ||
setAttribute: () => {}, | ||
removeEventListener: () => {}, | ||
} | ||
|
||
const renderComponent = (city: string) => | ||
render( | ||
<LocationCellRenderer | ||
value={city} | ||
eGridCell={eGridCell as HTMLElement} | ||
/>, | ||
{ | ||
wrapper: BrowserRouter, | ||
}, | ||
) | ||
|
||
afterEach(() => { | ||
mockAddEventListener.mockClear() | ||
}) | ||
|
||
it('renders the location', () => { | ||
renderComponent() | ||
const city = 'London' | ||
renderComponent(city) | ||
expect(screen.getByRole('link')).toHaveTextContent(city) | ||
}) | ||
|
||
it('Navigates to single city page on click', async () => { | ||
const { user } = renderComponent() | ||
const city = 'Paris' | ||
const user = userEvent.setup() | ||
renderComponent(city) | ||
await user.click(screen.getByRole('link')) | ||
expect(document.location.pathname).toBe(`/city/${city}`) | ||
expect(window.location.pathname).toBe(`/city/${city}`) | ||
}) | ||
|
||
it('Navigates to single city page on typing Enter', async () => { | ||
const { user } = renderComponent() | ||
await user.type(eGridCell, '[Enter]') | ||
expect(document.location.pathname).toBe(`/city/${city}`) | ||
it('Binds keyboard event handler to eGridCell', async () => { | ||
// Cannot test actual interaction because eGridCell is not added to the DOM | ||
// by LocationCellRenderer, so all we can do is test calls | ||
const city = 'Bristol' | ||
renderComponent(city) | ||
|
||
expect(mockAddEventListener).toHaveBeenCalledTimes(1) | ||
const [eventName, eventCallback] = mockAddEventListener.mock | ||
.calls[0] as Parameters<HTMLElement['addEventListener']> | ||
expect(eventName).toEqual('keydown') | ||
|
||
act(() => { | ||
;(eventCallback as EventListener)({ code: 'Enter' } as KeyboardEvent) | ||
}) | ||
expect(window.location.pathname).toBe(`/city/${city}`) | ||
}) | ||
}) |