summaryrefslogtreecommitdiff
path: root/preact/hooks/test/browser/hooks.options.test.js
blob: ca88d1f4dc9e46456910129e5a4854b0a7e62b09 (plain)
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
import {
	afterDiffSpy,
	beforeRenderSpy,
	unmountSpy,
	hookSpy
} from '../../../test/_util/optionSpies';

import { setupRerender, act } from 'preact/test-utils';
import { createElement, render, createContext, options } from 'preact';
import { setupScratch, teardown } from '../../../test/_util/helpers';
import {
	useState,
	useReducer,
	useEffect,
	useLayoutEffect,
	useRef,
	useImperativeHandle,
	useMemo,
	useCallback,
	useContext,
	useErrorBoundary
} from 'preact/hooks';

/** @jsx createElement */

describe('hook options', () => {
	/** @type {HTMLDivElement} */
	let scratch;

	/** @type {() => void} */
	let rerender;

	/** @type {() => void} */
	let increment;

	beforeEach(() => {
		scratch = setupScratch();
		rerender = setupRerender();

		afterDiffSpy.resetHistory();
		unmountSpy.resetHistory();
		beforeRenderSpy.resetHistory();
		hookSpy.resetHistory();
	});

	afterEach(() => {
		teardown(scratch);
	});

	function App() {
		const [count, setCount] = useState(0);
		increment = () => setCount(prevCount => prevCount + 1);
		return <div>{count}</div>;
	}

	it('should call old options on mount', () => {
		render(<App />, scratch);

		expect(beforeRenderSpy).to.have.been.called;
		expect(afterDiffSpy).to.have.been.called;
	});

	it('should call old options.diffed on update', () => {
		render(<App />, scratch);

		increment();
		rerender();

		expect(beforeRenderSpy).to.have.been.called;
		expect(afterDiffSpy).to.have.been.called;
	});

	it('should call old options on unmount', () => {
		render(<App />, scratch);
		render(null, scratch);

		expect(unmountSpy).to.have.been.called;
	});

	it('should detect hooks', () => {
		const USE_STATE = 1;
		const USE_REDUCER = 2;
		const USE_EFFECT = 3;
		const USE_LAYOUT_EFFECT = 4;
		const USE_REF = 5;
		const USE_IMPERATIVE_HANDLE = 6;
		const USE_MEMO = 7;
		const USE_CALLBACK = 8;
		const USE_CONTEXT = 9;
		const USE_ERROR_BOUNDARY = 10;

		const Ctx = createContext(null);

		function App() {
			useState(0);
			useReducer(x => x, 0);
			useEffect(() => null, []);
			useLayoutEffect(() => null, []);
			const ref = useRef(null);
			useImperativeHandle(ref, () => null);
			useMemo(() => null, []);
			useCallback(() => null, []);
			useContext(Ctx);
			useErrorBoundary(() => null);
		}

		render(
			<Ctx.Provider value="a">
				<App />
			</Ctx.Provider>,
			scratch
		);

		expect(hookSpy.args.map(arg => [arg[1], arg[2]])).to.deep.equal([
			[0, USE_STATE],
			[1, USE_REDUCER],
			[2, USE_EFFECT],
			[3, USE_LAYOUT_EFFECT],
			[4, USE_REF],
			[5, USE_IMPERATIVE_HANDLE],
			[6, USE_MEMO],
			[7, USE_CALLBACK],
			[8, USE_CONTEXT],
			[9, USE_ERROR_BOUNDARY],
			// Belongs to useErrorBoundary that uses multiple native hooks.
			[10, USE_STATE]
		]);
	});

	describe('Effects', () => {
		beforeEach(() => {
			options._skipEffects = options.__s = true;
		});

		afterEach(() => {
			options._skipEffects = options.__s = false;
		});

		it('should skip effect hooks', () => {
			const spy = sinon.spy();
			function App() {
				useEffect(spy, []);
				useLayoutEffect(spy, []);
				return null;
			}

			act(() => {
				render(<App />, scratch);
			});

			expect(spy.callCount).to.equal(0);
		});
	});
});