initial tests

Signed-off-by: Emma Indal <emma.indahl@gmail.com>
Signed-off-by: Eric Peterson <ericpeterson@spotify.com>
This commit is contained in:
Emma Indal
2021-05-24 18:27:00 +02:00
committed by Eric Peterson
parent d92b8ec5ea
commit b264028394
3 changed files with 127 additions and 0 deletions
@@ -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('<SearchBarNext />', () => {
it('renders without exploding', async () => {
const { getByRole } = await renderInTestApp(<SearchBarNext />);
expect(
getByRole('textbox', { name: 'search backstage' }),
).toBeInTheDocument();
});
});
@@ -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 <h6>{term}</h6>;
};
describe('useSearch', () => {
afterEach(() => {
jest.resetAllMocks();
});
it('context should use initial term', async () => {
jest.spyOn(SearchContext, 'useSearch');
const { getByRole } = await renderInTestApp(<MockSearchContextConsumer />);
expect(getByRole('heading')).toBeInTheDocument();
});
it('context should use mocked term', async () => {
jest
.spyOn(SearchContext, 'useSearch')
.mockImplementation(() => mockContextState({ term: 'new-term' }));
const { getByRole } = await renderInTestApp(<MockSearchContextConsumer />);
expect(getByRole('heading', { name: 'new-term' })).toBeInTheDocument();
});
});
@@ -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 <h6>{name}</h6>;
};
describe('<SearchFilterNext />', () => {
it('renders without exploding', async () => {
const props = {
name: 'filter name',
};
const { getByRole } = await renderInTestApp(
<SearchFilterNext {...props} component={MockFilterComponent} />,
);
expect(getByRole('heading', { name: 'filter name' })).toBeInTheDocument();
});
});