Skip to content

Commit

Permalink
✅ Tests Container search onError, refs #6
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreMiras committed Dec 14, 2020
1 parent 2069a90 commit 06d3855
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 26 deletions.
70 changes: 45 additions & 25 deletions src/components/Container.test.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import { ApolloError } from '@apollo/client';
import { fireEvent, render, screen } from '@testing-library/react';
import renderer from 'react-test-renderer';
import { library } from '@fortawesome/fontawesome-svg-core';
Expand All @@ -10,30 +11,6 @@ import Container from './Container';
library.add(fab, fas);
jest.mock('../utils/search');

const forks = [
{
nameWithOwner: 'django-nonrel/django',
stargazerCount: 214,
forkCount: 84,
pushedAt: '2020-08-29T14:23:26Z',
object: {
history: {
totalCount: 13990,
},
},
},
];

const searchResult = {
data: {
repository: {
forks: {
nodes: forks,
},
},
},
};

test('renders', () => {
const tree = renderer.create(
<Container />,
Expand All @@ -46,12 +23,55 @@ test('search forks', (done) => {
const searchInput = screen.getByPlaceholderText(/github.com/);
const submitButton = screen.getByRole('button', { type: 'submit' });
const forkId = 'django-nonrel/django';
searchPopularForks.mockImplementationOnce((url, onResult) => {
const forks = [
{
nameWithOwner: forkId,
stargazerCount: 214,
forkCount: 84,
pushedAt: '2020-08-29T14:23:26Z',
object: {
history: {
totalCount: 13990,
},
},
},
];

const searchResult = {
data: {
repository: {
forks: {
nodes: forks,
},
},
},
};
searchPopularForks.mockImplementationOnce((url, onResult, onError) => {
onResult(searchResult);
onError; // peaces linter mind
done();
});
expect(screen.queryByText(forkId)).toBeNull();
fireEvent.change(searchInput, { target: { value: 'https://github.com/django/django' } });
fireEvent.click(submitButton);
expect(screen.queryByText(forkId)).toBeInTheDocument();
});

test('search forks onError', (done) => {
render(<Container />);
const searchInput = screen.getByPlaceholderText(/github.com/);
const submitButton = screen.getByRole('button', { type: 'submit' });
const forkId = 'django-nonrel/django-404';
const expected = `Could not resolve to a Repository with the name '${forkId}'.`;
const searchError = new ApolloError({ errorMessage: expected });
searchPopularForks.mockImplementationOnce((url, onResult, onError) => {
onResult; // pieaces linter mind
onError(searchError);
done();
});
expect(screen.queryByText(forkId)).toBeNull();
fireEvent.change(searchInput, { target: { value: `https://github.com/${forkId}` } });
expect(screen.queryByText('Error')).not.toBeInTheDocument();
fireEvent.click(submitButton);
expect(screen.queryByText('Error')).toBeInTheDocument();
});
2 changes: 1 addition & 1 deletion src/components/ErrorDialog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ ErrorDialog.propTypes = {
onClose: PropTypes.func,
};
ErrorDialog.defaultProps = {
onClose: () => null,
onClose: null,
};

export default ErrorDialog;

0 comments on commit 06d3855

Please sign in to comment.