-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
context.jsx
77 lines (68 loc) · 2.16 KB
/
context.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
import React from 'react';
import PropTypes from 'prop-types';
import { expect } from 'chai';
import {
itIf,
} from '../../_helpers';
import {
is,
} from '../../_helpers/version';
import {
createClass,
} from '../../_helpers/react-compat';
export default function describeContext({
Wrap,
WrapperName,
}) {
describe('.context()', () => {
const contextTypes = {
name: PropTypes.string,
};
const SimpleComponent = createClass({
contextTypes,
render() {
const { name } = this.context;
return <div>{name}</div>;
},
});
function SimpleComponentSFC(props, { name }) {
return <div>{name}</div>;
}
SimpleComponentSFC.contextTypes = contextTypes;
it('throws when not called on the root', () => {
const context = { name: <main /> };
const wrapper = Wrap(<SimpleComponent />, { context });
const main = wrapper.find('main');
expect(main).to.have.lengthOf(1);
expect(() => main.context()).to.throw(
Error,
`${WrapperName}::context() can only be called on the root`,
);
});
it('throws if it is called when wrapper didn’t include context', () => {
const wrapper = Wrap(<SimpleComponent />);
expect(() => wrapper.context()).to.throw(
Error,
`${WrapperName}::context() can only be called on a wrapper that was originally passed a context option`,
);
});
itIf(is('>= 16'), 'throws on SFCs that lack an instance', () => {
const context = { name: 'bob' };
const wrapper = Wrap(<SimpleComponentSFC />, { context });
expect(() => wrapper.context()).to.throw(
Error,
`${WrapperName}::context() can only be called on wrapped nodes that have a non-null instance`,
);
});
it('works with no arguments', () => {
const context = { name: {} };
const wrapper = Wrap(<SimpleComponentSFC />, { context });
expect(wrapper.context()).to.eql(context);
});
it('works with a key name', () => {
const context = { name: {} };
const wrapper = Wrap(<SimpleComponentSFC />, { context });
expect(wrapper.context('name')).to.equal(context.name);
});
});
}