-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathpreact.test.js
62 lines (44 loc) · 1.65 KB
/
preact.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
/* @jsx Preact.h */
import 'jest-dom/extend-expect';
import Preact from 'preact';
import {getQueriesForElement, fireEvent, wait} from 'dom-testing-library';
import {fireEventAsync} from './helpers/fire-event-async';
import {Counter} from '../src/preact';
const render = ui => {
const container = document.createElement('div');
Preact.render(ui, container);
return {
...getQueriesForElement(container),
container,
};
};
describe('Counter', () => {
// we need to make this test async, because unlike React, Preact doens't
// render synchronously, and instead will render the result of state changes
// on the next tick.
// We need to wait for that next tick, and one method is using async / await
// Alternatively, setTimeout can be used
test('updates state on clicks', async () => {
const {container, getByText} = render(<Counter />);
console.log(container);
const counter = getByText('0');
fireEvent.click(counter);
// use dom-testing-library's wait to wait for the next tick in the event
// loop before we run any assertions, otherwise state changes will not
// reflect in our tests
await wait();
expect(counter).toHaveTextContent('1');
fireEvent.click(counter);
await wait();
expect(counter).toHaveTextContent('2');
});
test('updates state on clicks, with helper', async () => {
const {container, cleanup, getByText} = render(<Counter />);
console.log(container);
const counter = getByText('0');
await fireEventAsync.click(counter);
expect(counter).toHaveTextContent('1');
await fireEventAsync.click(counter);
expect(counter).toHaveTextContent('2');
});
});