-
Notifications
You must be signed in to change notification settings - Fork 87
/
index.test.js
165 lines (146 loc) · 5.88 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
*
* Tests for HomeContainer
*
*/
import React from 'react';
import { Router } from 'react-router';
import { useHistory } from 'react-router-dom';
import { fireEvent } from '@testing-library/dom';
import { timeout, renderProvider } from '@utils/testUtils';
import { HomeContainerTest as HomeContainer, mapDispatchToProps } from '../index';
import { homeContainerTypes } from '../reducer';
import { createBrowserHistory } from 'history';
import { translate } from '@app/utils';
describe('<HomeContainer /> tests', () => {
let submitSpy;
beforeEach(() => {
submitSpy = jest.fn();
});
it('should render and match the snapshot', () => {
const { baseElement } = renderProvider(<HomeContainer dispatchGithubRepos={submitSpy} />);
expect(baseElement).toMatchSnapshot();
});
it('should call dispatchClearGithubRepos on empty change', async () => {
const getGithubReposSpy = jest.fn();
const clearGithubReposSpy = jest.fn();
const { getByTestId } = renderProvider(
<HomeContainer dispatchClearGithubRepos={clearGithubReposSpy} dispatchGithubRepos={getGithubReposSpy} />
);
fireEvent.change(getByTestId('search-bar'), {
target: { value: 'a' }
});
await timeout(500);
expect(getGithubReposSpy).toBeCalled();
fireEvent.change(getByTestId('search-bar'), {
target: { value: '' }
});
await timeout(500);
expect(clearGithubReposSpy).toBeCalled();
});
it('should call dispatchGithubRepos on change and after enter', async () => {
const repoName = 'react-template';
const { getByTestId } = renderProvider(<HomeContainer dispatchGithubRepos={submitSpy} />);
const searchBar = getByTestId('search-bar');
fireEvent.change(searchBar, {
target: { value: repoName }
});
await timeout(500);
expect(submitSpy).toBeCalledWith(repoName);
fireEvent.keyDown(searchBar, {
key: 'Enter',
code: 13,
charCode: 13
});
expect(submitSpy).toBeCalledWith(repoName);
});
it('should call dispatchGithubRepos on clicking the search icon', async () => {
const repoName = 'react-template';
const { getByTestId } = renderProvider(<HomeContainer dispatchGithubRepos={submitSpy} repoName={repoName} />);
fireEvent.click(getByTestId('search-icon'));
await timeout(500);
expect(submitSpy).toBeCalledWith(repoName);
});
it('should dispatchGithubRepos on update on mount if repoName is already persisted', async () => {
const repoName = 'react-template';
renderProvider(<HomeContainer repoName={repoName} reposData={null} dispatchGithubRepos={submitSpy} />);
await timeout(500);
expect(submitSpy).toBeCalledWith(repoName);
});
it('should validate mapDispatchToProps actions', async () => {
const dispatchReposSearchSpy = jest.fn();
const repoName = 'react-template';
const actions = {
dispatchGithubRepos: { repoName, type: homeContainerTypes.REQUEST_GET_GITHUB_REPOS },
dispatchClearGithubRepos: { type: homeContainerTypes.CLEAR_GITHUB_REPOS }
};
const props = mapDispatchToProps(dispatchReposSearchSpy);
props.dispatchGithubRepos(repoName);
expect(dispatchReposSearchSpy).toHaveBeenCalledWith(actions.dispatchGithubRepos);
await timeout(500);
props.dispatchClearGithubRepos();
expect(dispatchReposSearchSpy).toHaveBeenCalledWith(actions.dispatchClearGithubRepos);
});
it('should render default error message when search goes wrong', () => {
const defaultError = translate('something_went_wrong');
const { getByTestId } = renderProvider(<HomeContainer reposError={defaultError} />);
expect(getByTestId('error-message')).toBeInTheDocument();
expect(getByTestId('error-message').textContent).toBe(defaultError);
});
it('should render the default message when searchBox is empty and reposError is null', () => {
const defaultMessage = translate('repo_search_default');
const { getByTestId } = renderProvider(<HomeContainer />);
expect(getByTestId('default-message')).toBeInTheDocument();
expect(getByTestId('default-message').textContent).toBe(defaultMessage);
});
it('should render the data when loading becomes false', () => {
const reposData = { items: [{ repoOne: 'react-template' }] };
const { getByTestId } = renderProvider(<HomeContainer reposData={reposData} dispatchGithubRepos={submitSpy} />);
expect(getByTestId('for')).toBeInTheDocument();
});
it('should render exact number of RepoCards as per totalCount in result', () => {
const totalCount = 3;
const reposData = {
totalCount,
items: [
{
name: 'react-tempalte',
fullName: 'wednesday-solutions/react-template',
stargazersCount: 200
},
{
name: 'react',
fullName: 'wednesday-solutions/react',
stargazersCount: 100
},
{
name: 'react-tempalte2',
fullName: 'wednesday-solutions/react-template2',
stargazersCount: 300
}
]
};
const { getAllByTestId } = renderProvider(<HomeContainer reposData={reposData} dispatchGithubRepos={submitSpy} />);
expect(getAllByTestId('repo-card').length).toBe(totalCount);
});
it('should redirect to /stories when clicked on Clickable component', async () => {
const history = createBrowserHistory();
const { getByTestId } = renderProvider(
<Router history={history}>
<HomeContainer />
</Router>
);
const h = useHistory();
const historySpy = jest.spyOn(h, 'push');
fireEvent.click(getByTestId('redirect'));
await timeout(500);
expect(historySpy).toHaveBeenCalledWith('/stories');
});
it('should render Skeleton Comp when "loading" is true', async () => {
const repoName = 'some repo';
const { getByTestId, getAllByTestId } = renderProvider(
<HomeContainer loading dispatchGithubRepos={submitSpy} repoName={repoName} />
);
expect(getAllByTestId('skeleton').length).toBe(3);
});
});