From 215fcb0728e32fb393c5f48f43cc8669bcdfeb8b Mon Sep 17 00:00:00 2001 From: Emma Indal Date: Thu, 14 Apr 2022 16:00:59 +0200 Subject: [PATCH] test fixups Signed-off-by: Emma Indal --- .../SearchType/SearchType.Accordion.test.tsx | 75 ++++++++++--------- .../SearchType/SearchType.Tabs.test.tsx | 75 +++++++++---------- 2 files changed, 76 insertions(+), 74 deletions(-) diff --git a/plugins/search/src/components/SearchType/SearchType.Accordion.test.tsx b/plugins/search/src/components/SearchType/SearchType.Accordion.test.tsx index a62d061663..da1cae46b9 100644 --- a/plugins/search/src/components/SearchType/SearchType.Accordion.test.tsx +++ b/plugins/search/src/components/SearchType/SearchType.Accordion.test.tsx @@ -15,29 +15,30 @@ */ import React from 'react'; -import { ApiProvider } from '@backstage/core-app-api'; -import { TestApiRegistry } from '@backstage/test-utils'; +import { TestApiProvider } from '@backstage/test-utils'; import { act, render } from '@testing-library/react'; import user from '@testing-library/user-event'; - -import { searchApiRef } from '../../apis'; -import { SearchContext, SearchContextProvider } from '../SearchContext'; +import { + searchApiRef, + SearchContextProvider, +} from '@backstage/plugin-search-react'; import { SearchType } from './SearchType'; +const setTypesMock = jest.fn(); +const setPageCursorMock = jest.fn(); + +jest.mock('@backstage/plugin-search-react', () => ({ + ...jest.requireActual('@backstage/plugin-search-react'), + useSearch: jest.fn().mockReturnValue({ + types: [], + setTypes: (types: any) => setTypesMock(types), + pageCursor: '', + setPageCursor: (pageCursor: any) => setPageCursorMock(pageCursor), + }), +})); + describe('SearchType.Accordion', () => { const query = jest.fn(); - const mockApis = TestApiRegistry.from([searchApiRef, { query }]); - - const contextSpy = { - result: { loading: false, value: { results: [] } }, - term: '', - types: [], - filters: {}, - setTerm: jest.fn(), - setTypes: jest.fn(), - setFilters: jest.fn(), - setPageCursor: jest.fn(), - }; const expectedLabel = 'Expected Label'; const expectedType = { @@ -50,17 +51,19 @@ describe('SearchType.Accordion', () => { query.mockResolvedValue({ results: [] }); }); - afterEach(() => { - jest.resetAllMocks(); - }); + const Wrapper = ({ children }: { children: React.ReactNode }) => { + return ( + + {children} + + ); + }; it('should render as expected', async () => { const { getByText } = render( - - - - - , + + + , ); // The given label should be rendered. @@ -80,49 +83,49 @@ describe('SearchType.Accordion', () => { it('should set entire types array when a type is selected', async () => { const { getByText } = render( - + - , + , ); await user.click(getByText(expectedType.name)); - expect(contextSpy.setTypes).toHaveBeenCalledWith([expectedType.value]); + expect(setTypesMock).toHaveBeenCalledWith([expectedType.value]); }); it('should reset types array when all is selected', async () => { const { getByText } = render( - + - , + , ); await user.click(getByText('All')); - expect(contextSpy.setTypes).toHaveBeenCalledWith([]); + expect(setTypesMock).toHaveBeenCalledWith([]); }); it('should reset page cursor when a new type is selected', async () => { const { getByText } = render( - + - , + , ); await user.click(getByText(expectedType.name)); - expect(contextSpy.setPageCursor).toHaveBeenCalledWith(undefined); + expect(setPageCursorMock).toHaveBeenCalledWith(undefined); }); it('should collapse when a new type is selected', async () => { const { getByText, queryByText } = render( - + - , + , ); await user.click(getByText(expectedType.name)); diff --git a/plugins/search/src/components/SearchType/SearchType.Tabs.test.tsx b/plugins/search/src/components/SearchType/SearchType.Tabs.test.tsx index 2886b63ae7..16df3a8f65 100644 --- a/plugins/search/src/components/SearchType/SearchType.Tabs.test.tsx +++ b/plugins/search/src/components/SearchType/SearchType.Tabs.test.tsx @@ -15,50 +15,49 @@ */ import React from 'react'; -import { ApiProvider } from '@backstage/core-app-api'; -import { TestApiRegistry } from '@backstage/test-utils'; +import { TestApiProvider } from '@backstage/test-utils'; import { act, render } from '@testing-library/react'; import user from '@testing-library/user-event'; - -import { searchApiRef } from '../../apis'; -import { SearchContext, SearchContextProvider } from '../SearchContext'; +import { + SearchContextProvider, + searchApiRef, +} from '@backstage/plugin-search-react'; import { SearchType } from './SearchType'; -describe('SearchType.Tabs', () => { - const query = jest.fn(); - const mockApis = TestApiRegistry.from([searchApiRef, { query }]); +const setTypesMock = jest.fn(); +const setPageCursorMock = jest.fn(); - const contextSpy = { - result: { loading: false, value: { results: [] } }, - term: '', +jest.mock('@backstage/plugin-search-react', () => ({ + ...jest.requireActual('@backstage/plugin-search-react'), + useSearch: jest.fn().mockReturnValue({ types: [], - filters: {}, - setTerm: jest.fn(), - setTypes: jest.fn(), - setFilters: jest.fn(), - setPageCursor: jest.fn(), - }; + setTypes: (types: any) => setTypesMock(types), + pageCursor: '', + setPageCursor: (pageCursor: any) => setPageCursorMock(pageCursor), + }), +})); + +describe('SearchType.Tabs', () => { + const query = jest.fn().mockResolvedValue({}); const expectedType = { value: 'expected-type', name: 'Expected Type', }; - beforeEach(() => { - query.mockResolvedValue({ results: [] }); - }); - - afterEach(() => { - jest.resetAllMocks(); - }); + const Wrapper = ({ children }: { children: React.ReactNode }) => { + return ( + + {children} + + ); + }; it('should render as expected', async () => { const { getByText } = render( - - - - - , + + + , ); // The default "all" type should be rendered. @@ -72,40 +71,40 @@ describe('SearchType.Tabs', () => { it('should set entire types array when a type is selected', async () => { const { getByText } = render( - + - , + , ); await user.click(getByText(expectedType.name)); - expect(contextSpy.setTypes).toHaveBeenCalledWith([expectedType.value]); + expect(setTypesMock).toHaveBeenCalledWith([expectedType.value]); }); it('should reset types array when all is selected', async () => { const { getByText } = render( - + - , + , ); await user.click(getByText('All')); - expect(contextSpy.setTypes).toHaveBeenCalledWith([]); + expect(setTypesMock).toHaveBeenCalledWith([]); }); it('should reset page cursor when a new type is selected', async () => { const { getByText } = render( - + - , + , ); await user.click(getByText(expectedType.name)); - expect(contextSpy.setPageCursor).toHaveBeenCalledWith(undefined); + expect(setPageCursorMock).toHaveBeenCalledWith(undefined); }); });