/
test.js
138 lines (118 loc) · 3.99 KB
/
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* eslint-disable no-unused-vars, no-return-assign */
import {serial as test} from 'ava';
import React from 'react';
import {renderIntoDocument} from 'react-dom/test-utils';
import render from 'react-test-renderer';
import browserEnv from 'browser-env';
import { // eslint-disable-line unicorn/import-index
classNames,
If,
For,
Choose,
RootClass,
BodyClass,
isStatelessComponent,
getDisplayName
} from '../index';
browserEnv();
const snapshotJSX = (t, jsx) => t.snapshot(render.create(jsx).toJSON());
test('classNames', t => {
t.is(classNames('x'), 'x');
t.is(classNames('x', 'y'), 'x y');
t.is(classNames(null, 'x', undefined, NaN, 0, 4, true, 'y', false, ''), 'x y');
t.is(classNames('x', {
y: true,
nope: false,
z: 'foo'
}), 'x y z');
});
test('<If>', t => {
snapshotJSX(t, <If condition={true}><button>🦄</button></If>);
snapshotJSX(t, <If condition={false}><button>🦄</button></If>);
let evaluated = false;
snapshotJSX(t, <If condition={false} render={() => (
<button>{evaluated = true}</button>
)}/>);
t.false(evaluated);
});
test('<Choose>', t => {
snapshotJSX(t, <Choose>
<Choose.When condition={true}><button>😎</button></Choose.When>
<Choose.When condition={false}><button>🦄</button></Choose.When>
<Choose.Otherwise><button>🌈</button></Choose.Otherwise>
</Choose>);
snapshotJSX(t, <Choose>
<Choose.When condition={true}><button>🦄</button></Choose.When>
<Choose.When condition={true}><button>😎</button></Choose.When>
<Choose.Otherwise><button>🌈</button></Choose.Otherwise>
</Choose>);
snapshotJSX(t, <Choose>
<Choose.When condition={false}><button>😎</button></Choose.When>
<Choose.When condition={true}><button>🦄</button></Choose.When>
<Choose.Otherwise><button>🌈</button></Choose.Otherwise>
</Choose>);
snapshotJSX(t, <Choose>
<Choose.When condition={false}><button>😎</button></Choose.When>
<Choose.When condition={false}><button>🦄</button></Choose.When>
<Choose.Otherwise><button>🌈</button></Choose.Otherwise>
</Choose>);
snapshotJSX(t, <Choose>
<Choose.Otherwise><button>🌈</button></Choose.Otherwise>
<Choose.When condition={false}><button>😎</button></Choose.When>
<Choose.When condition={true}><button>🦄</button></Choose.When>
</Choose>);
let evaluated = false;
snapshotJSX(t, <Choose>
<Choose.When condition={false}><button>🦄</button></Choose.When>
<Choose.When condition={false}><button>😎</button></Choose.When>
<Choose.Otherwise render={() => (
<button>{evaluated = true}</button>
)}/>
</Choose>);
t.true(evaluated);
evaluated = false;
snapshotJSX(t, <Choose>
<Choose.When condition={true} render={() => (
<button>{evaluated = true}</button>
)}/>
<Choose.When condition={false}><button>😎</button></Choose.When>
<Choose.Otherwise><button>🌈</button></Choose.Otherwise>
</Choose>);
t.true(evaluated);
});
test('<For>', t => {
snapshotJSX(t, <For of={['🌈', '🦄', '😎']} render={(item, index) =>
<button key={index}>{item}</button>
}/>);
});
test('<RootClass/>', t => {
const el = document.documentElement;
renderIntoDocument(<RootClass add="foo"/>);
t.true(el.classList.contains('foo'));
renderIntoDocument(<RootClass remove="foo"/>);
t.false(el.classList.contains('foo'));
el.className = '';
renderIntoDocument(<RootClass add="foo bar"/>);
t.true(el.classList.contains('foo'));
t.true(el.classList.contains('bar'));
renderIntoDocument(<RootClass remove="foo"/>);
t.false(el.classList.contains('foo'));
t.true(el.classList.contains('bar'));
el.className = '';
});
test('<BodyClass/>', t => {
const el = document.body;
renderIntoDocument(<BodyClass add="foo"/>);
t.true(el.classList.contains('foo'));
renderIntoDocument(<BodyClass remove="foo"/>);
t.false(el.classList.contains('foo'));
el.className = '';
});
test('isStatelessComponent()', t => {
t.false(isStatelessComponent(BodyClass));
t.true(isStatelessComponent(() => {}));
});
test('getDisplayName()', t => {
t.is(getDisplayName(BodyClass), 'BodyClass');
t.is(getDisplayName(() => {}), 'Component');
});