From b264028394c27f4e2a8b459e4572b36456f2a04b Mon Sep 17 00:00:00 2001 From: Emma Indal Date: Mon, 24 May 2021 18:27:00 +0200 Subject: [PATCH] initial tests Signed-off-by: Emma Indal Signed-off-by: Eric Peterson --- .../SearchBarNext/SearchBarNext.test.tsx | 29 +++++++++ .../SearchContext/SearchContext.test.tsx | 61 +++++++++++++++++++ .../SearchFilterNext.test.tsx | 37 +++++++++++ 3 files changed, 127 insertions(+) create mode 100644 plugins/search/src/components/SearchBarNext/SearchBarNext.test.tsx create mode 100644 plugins/search/src/components/SearchContext/SearchContext.test.tsx create mode 100644 plugins/search/src/components/SearchFilterNext/SearchFilterNext.test.tsx diff --git a/plugins/search/src/components/SearchBarNext/SearchBarNext.test.tsx b/plugins/search/src/components/SearchBarNext/SearchBarNext.test.tsx new file mode 100644 index 0000000000..3a5cb3e97a --- /dev/null +++ b/plugins/search/src/components/SearchBarNext/SearchBarNext.test.tsx @@ -0,0 +1,29 @@ +/* + * Copyright 2021 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import React from 'react'; +import { renderInTestApp } from '@backstage/test-utils'; +import { SearchBarNext } from './SearchBarNext'; + +describe('', () => { + it('renders without exploding', async () => { + const { getByRole } = await renderInTestApp(); + + expect( + getByRole('textbox', { name: 'search backstage' }), + ).toBeInTheDocument(); + }); +}); diff --git a/plugins/search/src/components/SearchContext/SearchContext.test.tsx b/plugins/search/src/components/SearchContext/SearchContext.test.tsx new file mode 100644 index 0000000000..cce315f62b --- /dev/null +++ b/plugins/search/src/components/SearchContext/SearchContext.test.tsx @@ -0,0 +1,61 @@ +/* + * Copyright 2021 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import React from 'react'; +import { renderInTestApp } from '@backstage/test-utils'; +import * as SearchContext from './SearchContext'; + +const mockContextState = ({ term }: { term: string }) => { + return { + term, + pageCursor: '', + filters: {}, + types: ['*'], + result: { results: [], loading: false, error: undefined }, + setTerm: jest.fn(), + setFilters: jest.fn(), + setTypes: jest.fn(), + setPageCursor: jest.fn(), + }; +}; + +const MockSearchContextConsumer = () => { + const { term } = SearchContext.useSearch(); + + return
{term}
; +}; + +describe('useSearch', () => { + afterEach(() => { + jest.resetAllMocks(); + }); + + it('context should use initial term', async () => { + jest.spyOn(SearchContext, 'useSearch'); + const { getByRole } = await renderInTestApp(); + expect(getByRole('heading')).toBeInTheDocument(); + }); + + it('context should use mocked term', async () => { + jest + .spyOn(SearchContext, 'useSearch') + .mockImplementation(() => mockContextState({ term: 'new-term' })); + + const { getByRole } = await renderInTestApp(); + + expect(getByRole('heading', { name: 'new-term' })).toBeInTheDocument(); + }); +}); diff --git a/plugins/search/src/components/SearchFilterNext/SearchFilterNext.test.tsx b/plugins/search/src/components/SearchFilterNext/SearchFilterNext.test.tsx new file mode 100644 index 0000000000..63822b9e00 --- /dev/null +++ b/plugins/search/src/components/SearchFilterNext/SearchFilterNext.test.tsx @@ -0,0 +1,37 @@ +/* + * Copyright 2021 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import React from 'react'; +import { renderInTestApp } from '@backstage/test-utils'; +import { SearchFilterNext } from './SearchFilterNext'; + +const MockFilterComponent = ({ name }: { name: string }) => { + return
{name}
; +}; + +describe('', () => { + it('renders without exploding', async () => { + const props = { + name: 'filter name', + }; + + const { getByRole } = await renderInTestApp( + , + ); + + expect(getByRole('heading', { name: 'filter name' })).toBeInTheDocument(); + }); +});