/
adapter-utils-spec.jsx
118 lines (97 loc) · 3.2 KB
/
adapter-utils-spec.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
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
import React from 'react';
import { expect } from 'chai';
import {
displayNameOfNode,
ensureKeyOrUndefined,
getMaskedContext,
} from 'enzyme-adapter-utils';
import './_helpers/setupAdapters';
import { describeIf } from './_helpers';
import { is } from './_helpers/version';
describe('enzyme-adapter-utils', () => {
describe('ensureKeyOrUndefined', () => {
it('returns the key if truthy', () => {
[true, 42, 'foo', [], {}, () => {}].forEach((truthy) => {
expect(ensureKeyOrUndefined(truthy)).to.equal(truthy);
});
});
it('returns the empty string if the key is the empty string', () => {
expect(ensureKeyOrUndefined('')).to.equal('');
});
it('returns undefined if falsy and not the empty string', () => {
[null, undefined, false, 0, NaN].forEach((falsy) => {
expect(ensureKeyOrUndefined(falsy)).to.equal(undefined);
});
});
});
describe('displayNameOfNode', () => {
describe('given a node with displayName', () => {
it('returns the displayName', () => {
class Foo extends React.Component {
render() { return <div />; }
}
Foo.displayName = 'CustomWrapper';
expect(displayNameOfNode(<Foo />)).to.equal('CustomWrapper');
});
describeIf(is('> 0.13'), 'stateless function components', () => {
it('returns the displayName', () => {
const Foo = () => <div />;
Foo.displayName = 'CustomWrapper';
expect(displayNameOfNode(<Foo />)).to.equal('CustomWrapper');
});
});
});
describe('given a node without displayName', () => {
it('returns the name', () => {
class Foo extends React.Component {
render() { return <div />; }
}
expect(displayNameOfNode(<Foo />)).to.equal('Foo');
});
it('returns the name even if it is falsy', () => {
const makeFoo = () => () => <div />;
const Foo = makeFoo();
expect(displayNameOfNode(<Foo />)).to.equal('');
});
describeIf(is('> 0.13'), 'stateless function components', () => {
it('returns the name', () => {
const Foo = () => <div />;
expect(displayNameOfNode(<Foo />)).to.equal('Foo');
});
});
});
describe('given a DOM node', () => {
it('returns the type', () => {
expect(displayNameOfNode(<div />)).to.equal('div');
});
});
});
describe('getMaskedContext', () => {
const contextTypes = {
a() {},
c() {},
};
const unmaskedContext = {
a: 1,
b: 2,
c: 3,
};
const falsies = [undefined, null, false, '', NaN, 0];
it('returns an empty object with falsy `contextTypes`', () => {
falsies.forEach((falsy) => {
expect(getMaskedContext(falsy, unmaskedContext)).to.eql({});
});
});
it('returns an empty object with falsy `unmaskedContext`', () => {
falsies.forEach((falsy) => {
expect(getMaskedContext(contextTypes, falsy)).to.eql({});
});
});
it('filters `unmaskedContext` down to `contextTypes`', () => {
expect(getMaskedContext(contextTypes, unmaskedContext)).to.eql({
a: unmaskedContext.a,
c: unmaskedContext.c,
});
});
});
});