-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Context.test.jsx
83 lines (65 loc) · 1.89 KB
/
Context.test.jsx
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
import React from 'react';
import { mount } from 'enzyme';
const initial = <main />;
const Context = React.createContext(initial);
const { Consumer, Provider } = Context;
class Component extends React.Component {
static contextType = Context;
render() {
return this.context || null;
}
}
function SFC() {
return <Consumer>{(value) => value}</Consumer>;
}
/*
function WithHook() {
const context = React.useContext(Context);
return context || null;
}
describe('shallow', () => {
});
*/
describe('mount', () => {
[
[Component, 'a class component'],
[SFC, 'an SFC using Consumer'],
// [WithHook, 'an SFC with useContext hook'],
].forEach(([C, desc]) => {
it(`works with ${desc}`, () => {
const wrapper = mount(<C />);
expect(wrapper.debug()).toEqual(`<${C.name}>
<main />
</${C.name}>`);
});
it(`works with ${desc} with an inline Provider`, () => {
const wrapper = mount(<Provider><C /></Provider>);
expect(wrapper.debug()).toEqual(`<${C.name} />`);
});
it(`works with ${desc} with an inline Provider with a value`, () => {
const wrapper = mount(<Provider value={<aside />}><C /></Provider>);
expect(wrapper.debug()).toEqual(`<${C.name}>
<aside />
</${C.name}>`);
wrapper.setProps({ value: <span /> });
expect(wrapper.debug()).toEqual(`<${C.name}>
<span />
</${C.name}>`);
});
it(`works with ${desc} with a wrappingComponent Provider`, () => {
const wrapper = mount(<C />, {
wrappingComponent: Provider,
});
expect(wrapper.debug()).toEqual(`<${C.name} />`);
});
it(`works with ${desc} with a wrappingComponent Provider with a value`, () => {
const wrapper = mount(<C />, {
wrappingComponent: Provider,
wrappingComponentProps: { value: <nav /> },
});
expect(wrapper.debug()).toEqual(`<${C.name}>
<nav />
</${C.name}>`);
});
});
});