-
-
Notifications
You must be signed in to change notification settings - Fork 631
/
Copy pathReactOnRails.test.js
135 lines (113 loc) · 4.01 KB
/
ReactOnRails.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
/* eslint-disable react/no-multi-comp */
import test from 'tape';
import { createStore } from 'redux';
import React from 'react';
import ReactOnRails from '../src/ReactOnRails';
import { canUseDOM } from 'fbjs/lib/ExecutionEnvironment';
import JsDom from 'jsdom';
if (!canUseDOM) {
global.document = JsDom.jsdom('<div id="root"></div>');
global.window = document.defaultView;
}
test('ReactOnRails render returns a virtual DOM element for component', (assert) => {
assert.plan(1);
const R1 = React.createClass({
render() {
return (
<div> WORLD </div>
);
},
});
ReactOnRails.register({ R1 });
const actual = ReactOnRails.render('R1', {}, 'root')._reactInternalInstance._currentElement.type;
assert.deepEqual(actual, R1,
'ReactOnRails render should return a virtual DOM element for component');
});
test('ReactOnRails accepts traceTurbolinks as an option true', (assert) => {
ReactOnRails.resetOptions();
assert.plan(1);
ReactOnRails.setOptions({ traceTurbolinks: true });
const actual = ReactOnRails.option('traceTurbolinks');
assert.equal(actual, true);
});
test('ReactOnRails accepts traceTurbolinks as an option false', (assert) => {
ReactOnRails.resetOptions();
assert.plan(1);
ReactOnRails.setOptions({ traceTurbolinks: false });
const actual = ReactOnRails.option('traceTurbolinks');
assert.equal(actual, false);
});
test('ReactOnRails accepts exceptionLogger as an option function', (assert) => {
ReactOnRails.resetOptions();
assert.plan(1);
const logger = function() {};
ReactOnRails.setOptions({ exceptionLogger: logger });
const actual = ReactOnRails.option('exceptionLogger');
assert.equal(actual, logger);
});
test('ReactOnRails not specified has traceTurbolinks as false', (assert) => {
ReactOnRails.resetOptions();
assert.plan(1);
ReactOnRails.setOptions({ });
const actual = ReactOnRails.option('traceTurbolinks');
assert.equal(actual, false);
});
test('serverRenderReactComponent throws error for invalid options', (assert) => {
ReactOnRails.resetOptions();
assert.plan(1);
assert.throws(
() => ReactOnRails.setOptions({ foobar: true }),
/Invalid option/,
'setOptions should throw an error for invalid options'
);
});
test('registerStore throws if passed a falsey object (null, undefined, etc)', (assert) => {
assert.plan(3);
assert.throws(
() => ReactOnRails.registerStore(null),
/null or undefined/,
'registerStore should throw an error if a falsey value is passed (null)'
);
assert.throws(
() => ReactOnRails.registerStore(undefined),
/null or undefined/,
'registerStore should throw an error if a falsey value is passed (undefined)'
);
assert.throws(
() => ReactOnRails.registerStore(false),
/null or undefined/,
'registerStore should throw an error if a falsey value is passed (false)'
);
});
test('register store and getStoreGenerator allow registration', (assert) => {
assert.plan(2);
function reducer(state = {}, action) {
return {};
}
function storeGenerator(props) {
return createStore(reducer, props);
};
ReactOnRails.registerStore({ storeGenerator });
const actual = ReactOnRails.getStoreGenerator('storeGenerator');
assert.equal(actual, storeGenerator,
`Could not find 'storeGenerator' amongst store generators \
${JSON.stringify(ReactOnRails.storeGenerators())}.`);
assert.deepEqual(ReactOnRails.storeGenerators(), new Map([['storeGenerator', storeGenerator]]));
});
test('setStore and getStore', (assert) => {
assert.plan(2);
function reducer(state = {}, action) {
return {};
}
function storeGenerator(props) {
return createStore(reducer, props);
};
const store = storeGenerator({});
ReactOnRails.setStore('storeGenerator', store);
const actual = ReactOnRails.getStore('storeGenerator');
assert.equal(actual, store,
`Could not find 'store' amongst store generators ${JSON.stringify(ReactOnRails.stores())}.`);
const expected = new Map();
expected.set('storeGenerator', store);
assert.deepEqual(ReactOnRails.stores(), expected);
});