This repository has been archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 827
/
Copy pathmini.spec.js
86 lines (82 loc) · 2.83 KB
/
mini.spec.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
import { createComponent, render, createStore } from '../src/js/mini';
const wait = duration => new Promise(resolve => setTimeout(resolve, duration));
function Parent(...children) {
return createComponent({
children,
componentDidMount: jest.fn(),
render() {
return `
<div class="parent">
<children>
</div>`;
}
});
}
function Child(props, componentDidMount) {
return createComponent({
props,
componentDidMount,
render({ text }) {
return `<span class="child">${text}</span>`;
}
});
}
describe('Tests for mini framework', () => {
describe('render/component', () => {
it('renders a single component without children', () => {
const root = document.createElement('div');
render(Child({ text: 'Child1' }), root);
expect(root.innerHTML).toMatchSnapshot();
});
it('renders a component with children', () => {
const root = document.createElement('div');
render(Parent(Child({ text: 'Child1' }), Child({ text: 'Child2' })), root);
expect(root.innerHTML).toMatchSnapshot();
});
it('registers event listeners passed as props', () => {
const onClick = jest.fn();
const root = document.createElement('div');
render(Child({ onClick }), root);
root.firstElementChild.click();
expect(onClick).toHaveBeenCalled();
});
it('calls componentDidMount after the component has been rendered', () => {
const childOneDidMount = jest.fn();
const childTwoDidMount = jest.fn();
const root = document.createElement('div');
const parent = Parent(
Child({ text: 'Child1' }, childOneDidMount),
Child({ text: 'Child2' }, childTwoDidMount)
);
render(parent, root);
expect(parent.componentDidMount).toHaveBeenCalledTimes(1);
expect(childOneDidMount).toHaveBeenCalledTimes(1);
expect(childTwoDidMount).toHaveBeenCalledTimes(1);
});
});
describe('store', () => {
it('updates connected components on state change', async () => {
const actionsCreators = { increment: ({ count }) => ({ count: count + 1 }) };
const initialState = { count: 0 };
const { connect, actions } = createStore(initialState, actionsCreators);
function ConnectedChild(props) {
return connect(
createComponent({
props,
componentDidUpdate: jest.fn(),
render({ count }) {
return `<span>${count}</span>`;
}
})
);
}
const root = document.createElement('div');
const connectedChild = ConnectedChild();
render(Parent(Child({ text: 'Not connected' }), connectedChild), root);
actions.increment();
await wait(30);
expect(root.innerHTML).toMatchSnapshot();
expect(connectedChild.componentDidUpdate).toHaveBeenCalledTimes(1);
});
});
});