Skip to content

Commit

Permalink
ignore link click errors in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nmanu1 committed Feb 14, 2024
1 parent 9680ae9 commit a3e242f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
17 changes: 16 additions & 1 deletion tests/__utils__/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,19 @@ export function mockAnswersHooks({
mockedUtils && mockAnswersUtils(mockedUtils);
mockedState && mockAnswersState(mockedState);
mockedActions && mockSearchActions(mockedActions);
}
}

const originalConsoleError = console.error.bind(console.error);

export function ignoreLinkClickErrors() {
jest.spyOn(global.console, 'error')
.mockImplementation((msg, ...params) => {
/**
* Suppress errors about 'navigation' not being defined in jsdom when
* clicking on links.
*/
if (!msg.toString().match(/Error: Not implemented: navigation/)) {
originalConsoleError(msg, ...params);
}
});
};
3 changes: 2 additions & 1 deletion tests/components/DirectAnswer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { render, screen } from '@testing-library/react';
import { DirectAnswerState } from '@yext/search-headless-react';
import { useAnalytics } from '../../src/hooks/useAnalytics';
import { DirectAnswer } from '../../src/components/DirectAnswer';
import { RecursivePartial, mockAnswersState } from '../__utils__/mocks';
import { RecursivePartial, ignoreLinkClickErrors, mockAnswersState } from '../__utils__/mocks';
import { fieldValueDAState, featuredSnippetDAState } from '../__fixtures__/data/directanswers';
import userEvent from '@testing-library/user-event';
import React from 'react';
Expand Down Expand Up @@ -35,6 +35,7 @@ describe('Featured snippet direct answer analytics', () => {
async function runAnalyticsTestSuite() {
it('reports link click analytics', async () => {
render(<DirectAnswer />);
ignoreLinkClickErrors();
const link = screen.getByRole('link');
await userEvent.click(link);
expect(useAnalytics()?.report).toHaveBeenCalledTimes(1);
Expand Down
2 changes: 2 additions & 0 deletions tests/components/FeaturedSnippetDirectAnswer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { FeaturedSnippetDirectAnswer } from '../../src/components/FeaturedSnippe
import { featuredSnippetDAState } from '../__fixtures__/data/directanswers';
import userEvent from '@testing-library/user-event';
import React from 'react';
import { ignoreLinkClickErrors } from '../__utils__/mocks';

const featuredSnippetDAResult = featuredSnippetDAState.result as FeaturedSnippetDirectAnswerType;

Expand Down Expand Up @@ -39,6 +40,7 @@ describe('FeaturedSnippet direct answer', () => {
result={featuredSnippetDAResult}
readMoreClickHandler={readMoreClickHandler}
/>);
ignoreLinkClickErrors();
await userEvent.click(screen.getByRole('link', { name: '[relatedResult.name]' }));
expect(readMoreClickHandler).toHaveBeenCalledTimes(1);
});
Expand Down
3 changes: 2 additions & 1 deletion tests/components/FieldValueDirectAnswer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ import userEvent from '@testing-library/user-event';
import { fieldValueDAState } from '../__fixtures__/data/directanswers';
import { UnknownFieldTypeDisplayComponent } from '../../src/components/DirectAnswer';
import React from 'react';
import { ignoreLinkClickErrors } from '../__utils__/mocks';

const fieldValueDAResult = fieldValueDAState.result as FieldValueDirectAnswerType;

describe('FieldValue direct answer', () => {

it('executes viewDetailsClickHandler when click on "View Details" link', async () => {
const viewDetailsClickHandler = jest.fn();
render(<FieldValueDirectAnswer
result={fieldValueDAResult}
viewDetailsClickHandler={viewDetailsClickHandler}/>
);
ignoreLinkClickErrors();
const viewDetailsLink = screen.getByRole('link', { name: 'View Details' });
await userEvent.click(viewDetailsLink);

Expand Down

0 comments on commit a3e242f

Please sign in to comment.