-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindex-test.js
119 lines (102 loc) · 3.16 KB
/
index-test.js
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
/* eslint-env mocha */
/* eslint react/prop-types:0 */
import React from 'react';
import expect from 'expect';
import expectJSX from './index';
expect.extend(expectJSX);
class TestComponent extends React.Component {
render() {
return <div>Hi! {this.props.name}</div>;
}
}
describe('expect(ReactElement).toEqualJSX(ReactElement)', () => {
context('api', () => {
it('has toEqualJSX', () => {
expect(expect().toEqualJSX).toBeA('function');
});
it('has toNotEqualJSX', () => {
expect(expect().toNotEqualJSX).toBeA('function');
});
it('has toIncludeJSX', () => {
expect(expect().toIncludeJSX).toBeA('function');
});
});
context('toEqualJSX', () => {
it('can diff React elements', () => {
expect(
<TestComponent />
).toEqualJSX(
<TestComponent />
);
});
it('throws when elements are different', () => {
try {
expect(<TestComponent extra="neous" />).toEqualJSX(<TestComponent />);
} catch (err) {
expect(err instanceof Error).toBe(true);
expect(err.message)
.toEqual('Expected \'<TestComponent extra="neous" />\' to equal \'<TestComponent />\'');
}
});
it('does not care about function', () => {
const fns = {
one() { return 'one'; },
two() { return 'two'; },
};
expect(
<TestComponent fn={fns.one} />
).toEqualJSX(
<TestComponent fn={fns.two} />
);
});
it('handle render method with interpolation', () => {
expect(
<TestComponent name="Jon" />
).toNotEqual(
<TestComponent name="Marry" />
);
});
});
context('toNotEqualJSX', () => {
it('works', () => {
expect(<div />).toNotEqualJSX(<div Hello=", world!" />);
});
it('throws when elements are the same', () => {
try {
expect(<div />).toNotEqualJSX(<div />);
} catch (err) {
expect(err instanceof Error).toBe(true);
expect(err.message).toEqual('Expected \'<div />\' to not equal \'<div />\'');
}
});
});
context('toIncludeJSX', () => {
it('works', () => {
expect(<div><div><TestComponent Hello=", world!"/></div></div>)
.toIncludeJSX(<div><TestComponent Hello=", world!"/></div>);
});
it('throws when element is not included', () => {
try {
expect(<div />).toIncludeJSX(<div Hello=", world!" />);
} catch (err) {
expect(err instanceof Error).toBe(true);
expect(err.message).toEqual('Expected \'<div />\' to include \'<div Hello=", world!" />\'');
}
});
});
context('toNotIncludeJSX', () => {
it('works', () => {
expect(<div><div><TestComponent Hello=", world!"/></div></div>)
.toNotIncludeJSX(<div><TestComponent Jello=", world!"/></div>);
});
it('throws when element is included', () => {
try {
expect(<div Hello=", world!" />).toNotIncludeJSX(<div Hello=", world!" />);
} catch (err) {
expect(err instanceof Error).toBe(true);
const message = 'Expected \'<div Hello=", world!" />\' to exclude \'<div Hello=", world!" />\'';
expect(err.message).toEqual(message);
}
});
});
});