This repository was archived by the owner on May 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathindex.browser.js
More file actions
183 lines (165 loc) · 5.32 KB
/
index.browser.js
File metadata and controls
183 lines (165 loc) · 5.32 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/** Copyright (c) 2018 Uber Technologies, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import test from 'tape-cup';
import MockEmitter from 'events';
import App, {createPlugin, createToken} from 'fusion-core';
import {FetchToken} from 'fusion-tokens';
import {getSimulator} from 'fusion-test-utils';
import type {Token} from 'fusion-core';
import {UniversalEventsToken} from 'fusion-plugin-universal-events';
import RPCPlugin from '../browser';
import type {IEmitter} from '../types.js';
import createMockEmitter from './create-mock-emitter';
const MockPluginToken: Token<any> = createToken('test-plugin-token');
function createTestFixture() {
const mockFetch = (...args) =>
Promise.resolve({json: () => ({status: 'success', data: args})});
const mockEmitter: IEmitter = (new MockEmitter(): any);
const mockEmitterPlugin = createPlugin({
provides: () => mockEmitter,
});
const app = new App('content', el => el);
// $FlowFixMe
app.register(FetchToken, mockFetch);
app.register(UniversalEventsToken, mockEmitterPlugin);
app.register(MockPluginToken, RPCPlugin);
return app;
}
test('success status request', t => {
const mockEmitter = createMockEmitter({
emit(type, payload) {
t.equal(type, 'rpc:method-client');
t.equal(payload.method, 'test');
t.equal(payload.status, 'success');
t.equal(typeof payload.timing, 'number');
},
});
const app = createTestFixture();
// $FlowFixMe
app.register(UniversalEventsToken, mockEmitter);
let wasResolved = false;
getSimulator(
app,
createPlugin({
deps: {rpcFactory: MockPluginToken},
provides: deps => {
const rpc = deps.rpcFactory.from();
t.equals(typeof rpc.request, 'function', 'has method');
t.ok(rpc.request('test') instanceof Promise, 'has right return type');
rpc
.request('test')
.then(([url, options]) => {
t.equals(url, '/api/test', 'has right url');
t.equals(options.method, 'POST', 'has right http method');
t.equals(
options.headers['Content-Type'],
'application/json',
'has right content-type'
);
t.equals(options.body, '{}', 'has right body');
})
.catch(e => {
t.fail(e);
});
wasResolved = true;
},
})
);
t.true(wasResolved, 'plugin was resolved');
t.end();
});
test('success status request w/args and header', t => {
const mockEmitter = createMockEmitter({
emit(type, payload) {
t.equal(type, 'rpc:method-client');
t.equal(payload.method, 'test');
t.equal(payload.status, 'success');
t.equal(typeof payload.timing, 'number');
},
});
const app = createTestFixture();
// $FlowFixMe
app.register(UniversalEventsToken, mockEmitter);
let wasResolved = false;
getSimulator(
app,
createPlugin({
deps: {rpcFactory: MockPluginToken},
provides: deps => {
const rpc = deps.rpcFactory.from();
t.equals(typeof rpc.request, 'function', 'has method');
t.ok(rpc.request('test') instanceof Promise, 'has right return type');
rpc
.request('test', {args: 1}, {'test-header': 'header value'})
.then(([url, options]) => {
t.equals(url, '/api/test', 'has right url');
t.equals(options.method, 'POST', 'has right http method');
t.equals(
options.headers['Content-Type'],
'application/json',
'has right content-type'
);
t.equals(
options.headers['test-header'],
'header value',
'header is passed'
);
t.equals(options.body, '{"args":1}', 'has right body');
})
.catch(e => {
t.fail(e);
});
wasResolved = true;
},
})
);
t.true(wasResolved, 'plugin was resolved');
t.end();
});
test('failure status request', t => {
const mockFetchAsFailure = () =>
Promise.resolve({json: () => ({status: 'failure', data: 'failure data'})});
const mockEmitter = createMockEmitter({
emit(type, payload) {
t.equal(type, 'rpc:method-client');
t.equal(payload.method, 'test');
t.equal(payload.status, 'failure');
t.equal(typeof payload.timing, 'number');
t.equal(payload.error, 'failure data');
},
});
const app = createTestFixture();
// $FlowFixMe
app.register(FetchToken, mockFetchAsFailure);
// $FlowFixMe
app.register(UniversalEventsToken, mockEmitter);
let wasResolved = false;
getSimulator(
app,
createPlugin({
deps: {rpcFactory: MockPluginToken},
provides: deps => {
const rpc = deps.rpcFactory.from();
t.equals(typeof rpc.request, 'function', 'has method');
const testRequest = rpc.request('test');
t.ok(testRequest instanceof Promise, 'has right return type');
testRequest
.then(() => {
// $FlowFixMe
t.fail(() => new Error('should reject promise'));
})
.catch(e => {
t.equal(e, 'failure data', 'should pass failure data through');
});
wasResolved = true;
},
})
);
t.true(wasResolved, 'plugin was resolved');
t.end();
});