-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.spec.tsx
166 lines (129 loc) · 4.27 KB
/
index.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
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
166
import { Subscribe } from './index';
import { of, interval, BehaviorSubject, Observable } from 'rxjs';
import { map, scan } from 'rxjs/operators';
import { mount, ReactWrapper } from 'enzyme';
import * as React from 'react';
describe('test', () => {
describe('with data', () => {
it('should render the value', () => {
const source$ = of(1);
const el = mount(<Subscribe>{source$}</Subscribe>);
expect(el.text()).toEqual('1');
el.unmount();
});
it('should render next values', async () => {
jest.useFakeTimers();
const source$ = interval(100);
const el = mount(<Subscribe>{source$}</Subscribe>);
for (let i = 0; i < 10; ++i) {
jest.advanceTimersByTime(100);
el.update();
expect(el.text()).toEqual(String(i));
}
el.unmount();
});
it('works fine with operators', () => {
jest.useFakeTimers();
const source$ = interval(100);
const el = mount(
<Subscribe>
{source$.pipe(
map(val => 10 * val),
scan((acc: number, val) => acc + val, 0)
)}
</Subscribe>
);
jest.advanceTimersByTime(100);
el.update();
expect(el.text()).toEqual('0');
jest.advanceTimersByTime(100);
el.update();
expect(el.text()).toEqual('10');
jest.advanceTimersByTime(100);
el.update();
expect(el.text()).toEqual('30');
jest.advanceTimersByTime(100);
el.update();
expect(el.text()).toEqual('60');
jest.advanceTimersByTime(100);
el.update();
expect(el.text()).toEqual('100');
el.unmount();
});
it('allows rendering elements', () => {
jest.useFakeTimers();
const source$ = interval(100);
const el = mount(
<Subscribe>
{source$.pipe(
map(val => 10 * val),
scan((acc, val) => acc + val, 0),
map(val => <input value={val} />)
)}
</Subscribe>
);
let input: HTMLInputElement;
// @todo why el.find('input') or el.is('input') are not working?
jest.advanceTimersByTime(100);
input = el.getDOMNode();
expect(input.tagName).toBe('INPUT');
expect(input.value).toBe('0');
jest.advanceTimersByTime(100);
input = el.getDOMNode();
expect(input.tagName).toBe('INPUT');
expect(input.value).toBe('10');
jest.advanceTimersByTime(100);
input = el.getDOMNode();
expect(input.tagName).toBe('INPUT');
expect(input.value).toBe('30');
jest.advanceTimersByTime(100);
input = el.getDOMNode();
expect(input.tagName).toBe('INPUT');
expect(input.value).toBe('60');
jest.advanceTimersByTime(100);
input = el.getDOMNode();
expect(input.tagName).toBe('INPUT');
expect(input.value).toBe('100');
el.unmount();
});
it('should work with BehaviourSubject', () => {
const source$ = new BehaviorSubject(123);
const el = mount(<Subscribe>{source$}</Subscribe>);
expect(el.text()).toEqual('123');
el.unmount();
});
});
describe('lack of data', () => {
it('should return empty render for an observable of undefined', () => {
const source$ = of(undefined);
const el = mount(<Subscribe>{source$}</Subscribe>);
expect(el.childAt(0).isEmptyRender()).toBe(true);
el.unmount();
});
it('should return empty render for an observable of null', () => {
const source$ = of(null);
const el = mount(<Subscribe>{source$}</Subscribe>);
expect(el.childAt(0).isEmptyRender()).toBe(true);
el.unmount();
});
it('should return empty render for an observable without a value', () => {
const source$ = new Observable<any>();
const el = mount(<Subscribe>{source$}</Subscribe>);
expect(el.childAt(0).isEmptyRender()).toBe(true);
el.unmount();
});
});
describe('errors', () => {
it('should throw an error when no obserable is passed', () => {
const source$ = {} as any;
// stfu React
jest.spyOn(console, 'error').mockImplementation(() => undefined);
let el: ReactWrapper | undefined;
const test = () => {
el = mount(<Subscribe>{source$}</Subscribe>);
};
expect(test).toThrow();
el && el.unmount();
});
});
});