-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathuseContextExample.test.ts
59 lines (51 loc) · 2.2 KB
/
useContextExample.test.ts
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
import 'jest';
import useContextExample from '../useContextExample';
import useContextExampleDeux from '../useContextExampleDeux';
import { Context1, Context2, Context3 } from '../ExampleContext';
import init from '../Jooks'; // In your code, do: import init from 'jooks';
describe('Testing useContext hook', () => {
const jooks = init(() => useContextExample());
beforeEach(() => {
// First you need to set all contexts, in the same order they are called, for every test.
jooks.setContext(Context1, { foo: 'baz' });
jooks.setContext(Context2, { ping: 'pung' });
});
it('It should give the correct values', () => {
// Then you can run your hook normally and expect that `useContext`
// will return the value you set in the beforeEach
const foo = jooks.run();
expect(foo).toBe('baz:pung');
});
it('It should give the correct values when set within the test', () => {
// If you want to change that context for a specific test, you can always reset the context values
// and set new ones.
jooks.resetContext();
jooks.setContext(Context1, { foo: 'buz' });
jooks.setContext(Context2, { ping: 'pang' });
const foo = jooks.run();
expect(foo).toBe('buz:pang');
});
});
describe('Testing useContext hook with two contexts sharing the same properties', () => {
const jooks = init(() => useContextExampleDeux());
beforeEach(() => {
// First you need to set all contexts, in the same order they are called, for every test.
jooks.setContext(Context2, { ping: 'pong' });
jooks.setContext(Context3, { ping: 'boom' });
});
it('It should give the correct values', () => {
// Then you can run your hook normally and expect that `useContext`
// will return the value you set in the beforeEach
const foo = jooks.run();
expect(foo).toBe('pong:boom');
});
it('It should give the correct values when set within the test', () => {
// If you want to change that context for a specific test, you can always reset the context values
// and set new ones.
jooks.resetContext();
jooks.setContext(Context2, { ping: 'buz' });
jooks.setContext(Context3, { ping: 'pang' });
const foo = jooks.run();
expect(foo).toBe('buz:pang');
});
});