-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
94 lines (87 loc) · 2.54 KB
/
index.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
import jsdom from 'jsdom';
import test from 'ava';
import React, { PropTypes } from 'react';
import { mount } from 'enzyme';
import indie, { get } from '../src/index.js';
(function init() {
const doc = jsdom.jsdom('<!doctype html><html><body></body></html>');
const win = doc.defaultView;
global.document = doc;
global.window = win;
}());
const MyComponent = (props) => (<div>{ props.someValue }</div>);
MyComponent.propTypes = {
someValue: PropTypes.string,
};
test.cb('indie starts with initial data and then loads new data', t => {
const propsConfig = {
someValue: [
'initial',
'loaded',
],
};
const Indie = indie(MyComponent, propsConfig);
const mounted = mount(<Indie />);
t.is(mounted.html(), '<div>initial</div>', 'show initial data');
setTimeout(() => {
t.is(mounted.html(), '<div>loaded</div>', 'show loaded data');
t.end();
});
});
test.cb('indie handles server error', t => {
const propsConfig = {
someValue: [
'initial',
new Promise((resolve, reject) => {
reject(new Error('Some server error'));
}),
'error',
],
};
const Indie = indie(MyComponent, propsConfig);
const mounted = mount(<Indie />);
setTimeout(() => {
t.is(mounted.html(), '<div>error</div>', 'handle error');
t.end();
});
});
test.cb('indie handles server error and pass error in callback', t => {
const propsConfig = {
someValue: [
'initial',
new Promise((resolve, reject) => {
reject(new Error('Some server error'));
}),
(err) => err.message,
],
};
const Indie = indie(MyComponent, propsConfig);
const mounted = mount(<Indie />);
setTimeout(() => {
t.is(mounted.html(), '<div>Some server error</div>', 'pass error in callback');
t.end();
});
});
test.cb('indie handles onResolve', t => {
const propsConfig = {
someValue: [
'initial',
'loaded',
null,
],
};
const onResolve = () => (props, component) => {component.setState({ someValue: 'resolved' });};
const Indie = indie(MyComponent, propsConfig, onResolve);
const mounted = mount(<Indie />);
setTimeout(() => {
t.is(mounted.html(), '<div>resolved</div>', 'Run onResolve function onResolve');
t.end();
});
});
test('indie `get` util deconstrucs promise result by a given key or object path', t => {
const myPromise = Promise.resolve({ x: 1, y: { z: 2 } });
return Promise.all([
get(myPromise, 'x').then(res => t.is(res, 1), 'get data from key'),
get(myPromise, 'y.z').then(res => t.is(res, 2, 'get data from nested path')),
]);
});