45 lines
1.7 KiB
TypeScript
45 lines
1.7 KiB
TypeScript
import { render } from '@testing-library/react';
|
|||
|
|
import { TrackPlaceView } from '@/components/places/TrackPlaceView';
|
||
|
|
|
||
|
|
const trackMock = jest.fn();
|
||
|
|
jest.mock('@/lib/analytics', () => ({
|
||
|
|
track: (...args: unknown[]) => trackMock(...args),
|
||
|
|
getNavigationSource: () => 'search',
|
||
|
|
}));
|
||
|
|
|
||
|
|
describe('TrackPlaceView', () => {
|
||
|
|
beforeEach(() => trackMock.mockClear());
|
||
|
|
|
||
|
|
it('reports which kind of location page was viewed', () => {
|
||
|
|
/*
|
||
|
|
* `kind` is the reason this event exists. Whether to keep investing in the
|
||
|
|
* location layer turns on which *sort* of page earns engagement — towns,
|
||
|
|
* authorities or postcode districts — and a bare pageview cannot say,
|
||
|
|
* because all four families share the /schools/ prefix.
|
||
|
|
*/
|
||
|
|
render(<TrackPlaceView kind="authority" slug="kent" count={412} />);
|
||
|
|
expect(trackMock).toHaveBeenCalledWith('place_viewed', {
|
||
|
|
kind: 'authority', slug: 'kent', phase: 'all',
|
||
|
|
school_count: 412, from: 'search',
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
it('names the phase when the page is a phase variant', () => {
|
||
|
|
render(<TrackPlaceView kind="town" slug="brentwood" count={29} phase="primary" />);
|
||
|
|
expect(trackMock).toHaveBeenCalledWith('place_viewed',
|
||
|
|
expect.objectContaining({ phase: 'primary' }));
|
||
|
|
});
|
||
|
|
|
||
|
|
it('fires once, not once per render', () => {
|
||
|
|
const { rerender } = render(
|
||
|
|
<TrackPlaceView kind="town" slug="brentwood" count={29} />);
|
||
|
|
rerender(<TrackPlaceView kind="town" slug="brentwood" count={29} />);
|
||
|
|
expect(trackMock).toHaveBeenCalledTimes(1);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('renders nothing', () => {
|
||
|
|
const { container } = render(
|
||
|
|
<TrackPlaceView kind="town" slug="brentwood" count={29} />);
|
||
|
|
expect(container).toBeEmptyDOMElement();
|
||
|
|
});
|
||
|
|
});
|