This repository has been archived by the owner on Feb 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Choosing.spec.tsx
136 lines (111 loc) · 3.05 KB
/
Choosing.spec.tsx
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
import React from 'react';
import {
fireEvent,
RenderResult,
waitFor as rtlWaitFor,
act,
} from '@testing-library/react';
import { fakeServer, FakeServer } from 'nise';
import { defaultGlobals, GlobalsProvider } from '../../globals';
import { searchBlobSelected, searchRepoSelected } from '../actions';
import Choosing from '../Choosing';
import { createSearchBlob, createSearchRepo } from '../../mocks';
const createInstance = ({
container,
getByText,
getByTestId,
}: RenderResult) => {
const elements = {
input: () => getByTestId('search-input'),
snippetList: () => getByTestId('snippet-list'),
selectButton: () => getByText('Select'),
errorNotice: () => container.querySelector('.components-notice.is-error'),
};
const fire = {
inputChange: (value: string) =>
fireEvent.change(elements.input(), { target: { value } }),
selectButtonClick: () => fireEvent.click(elements.selectButton()),
};
const waitFor = {
snippetList: () => rtlWaitFor(elements.snippetList),
};
return { elements, fire, waitFor };
};
const root = '/api/';
const element = (
<GlobalsProvider value={{ ...defaultGlobals, root }}>
<Choosing collection="blobs" />
</GlobalsProvider>
);
const blob = createSearchBlob();
const repo = createSearchRepo();
describe('Choosing', () => {
let server: FakeServer;
beforeEach(() => {
server?.restore();
server = fakeServer.create();
server.respondImmediately = true;
});
afterAll(() => {
server?.restore();
});
it('should emit a blob on successful choice', () => {
server.respondWith(
'GET',
`${root}search/blobs?s=js`,
JSON.stringify([blob]),
);
expect(element).toEmitFromJunction(
[[350, KTU.value(searchBlobSelected(blob))]],
(rr, tick) => {
const { fire } = createInstance(rr);
fire.inputChange('js');
act(() => {
tick(350);
});
fire.selectButtonClick();
},
);
});
it('should emit a repo on successful choice', () => {
server.respondWith(
'GET',
`${root}search/repos?s=js`,
JSON.stringify([repo]),
);
expect(
<GlobalsProvider value={{ ...defaultGlobals, root }}>
<Choosing collection="repos" />
</GlobalsProvider>,
).toEmitFromJunction(
[[350, KTU.value(searchRepoSelected(repo))]],
(rr, tick) => {
const { fire } = createInstance(rr);
fire.inputChange('js');
act(() => {
tick(350);
});
fire.selectButtonClick();
},
);
});
it('should show an error on failed API response', () => {
server.respondWith('GET', `${root}search/blobs?s=js`, xhr => {
xhr.respond(
500,
{},
JSON.stringify({
error: 'Something went wrong',
}),
);
});
expect(element).toEmitFromJunction([], (rr, tick) => {
const { fire, elements } = createInstance(rr);
fire.inputChange('js');
act(() => {
tick(350);
});
expect(elements.errorNotice()).toBeInTheDocument();
});
});
});