|
1 | 1 | import assert from 'assert'; |
2 | 2 | import xs from 'xstream'; |
3 | | -import fromDiagram from 'xstream/extra/fromDiagram'; |
4 | | -import {mockTimeSource} from '@cycle/time'; |
| 3 | + |
| 4 | +import { assertSourcesSinks } from './helpers'; |
5 | 5 | import * as ActionTypes from '../../ActionTypes'; |
6 | 6 | import * as actions from '../../actions'; |
7 | 7 |
|
8 | 8 | import { fetchReposByUser, searchUsers } from '../'; |
9 | 9 |
|
10 | | -function assertSourcesSinks(sources, sinks, main, done) { |
11 | | - const Time = mockTimeSource(); |
12 | | - const _sources = Object.keys(sources) |
13 | | - .reduce((_sources, sourceKey) => { |
14 | | - const sourceObj = sources[sourceKey]; |
15 | | - const diagram = Object.keys(sourceObj)[0]; |
16 | | - const sourceOpts = sourceObj[diagram]; |
17 | | - |
18 | | - let obj = {}; |
19 | | - if (diagram === 'stream') { |
20 | | - obj = { |
21 | | - [sourceKey]: sourceOpts |
22 | | - } |
23 | | - } else { |
24 | | - let firstKey = Object.keys(sourceOpts)[0]; |
25 | | - if (typeof sourceOpts[firstKey] === 'function') { |
26 | | - obj = { |
27 | | - [sourceKey]: { |
28 | | - [firstKey]: () => Time.diagram(diagram, sourceOpts[firstKey]()) |
29 | | - } |
30 | | - } |
31 | | - } else { |
32 | | - obj = { |
33 | | - [sourceKey]: Time.diagram(diagram, sourceOpts) |
34 | | - } |
35 | | - } |
36 | | - } |
37 | | - |
38 | | - return Object.assign(_sources, obj); |
39 | | - }, {}) |
40 | | - |
41 | | - const _sinks = Object.keys(sinks) |
42 | | - .reduce((_sinks, sinkKey) => { |
43 | | - const sinkObj = sinks[sinkKey]; |
44 | | - const diagram = Object.keys(sinkObj)[0]; |
45 | | - const sinkOpts = sinkObj[diagram]; |
46 | | - |
47 | | - return Object.assign(_sinks, { [sinkKey]: Time.diagram(diagram, sinkOpts) }); |
48 | | - }, {}); |
49 | | - |
50 | | - const _main = main(_sources); |
51 | | - |
52 | | - Object.keys(sinks) |
53 | | - .map(sinkKey => Time.assertEqual(_main[sinkKey], _sinks[sinkKey])); |
54 | | - |
55 | | - Time.run(err => { |
56 | | - expect(err).toBeFalsy(); |
57 | | - done(); |
58 | | - }); |
59 | | -} |
60 | | - |
61 | 10 | describe('Cycles', function() { |
62 | 11 | describe('fetchReposByUser', function() { |
63 | 12 | it('should emit HTTP requests given ACTIONs', function(done) { |
@@ -125,31 +74,35 @@ describe('Cycles', function() { |
125 | 74 | }); |
126 | 75 | }); |
127 | 76 |
|
128 | | - // describe('searchUsers', () => { |
129 | | - // it('should emit HTTP requests given ACTIONs, in an async fashion', (done) => { |
130 | | - // const actionSource = { |
131 | | - // a: actions.searchUsers('l'), |
132 | | - // b: actions.searchUsers('lu'), |
133 | | - // c: actions.searchUsers('luc') |
134 | | - // }; |
135 | | - // const httpSource = { |
136 | | - // select: () => null |
137 | | - // } |
138 | | - // const actionSink = { |
139 | | - // a: { |
140 | | - // url: `https://api.github.com/search/users?q=luc`, |
141 | | - // category: 'query' |
142 | | - // } |
143 | | - // } |
144 | | - // |
145 | | - // |
146 | | - // assertSourcesSinks({ |
147 | | - // ACTION: { '-a-b-c------|': actionSource }, |
148 | | - // HTTP: { '-------|': httpSource }, |
149 | | - // Time: { stream: mockTimeSource() } |
150 | | - // }, { |
151 | | - // HTTP: { '--a----|': actionSink } |
152 | | - // }, searchUsers, done); |
153 | | - // }) |
154 | | - // }) |
| 77 | + describe('searchUsers', () => { |
| 78 | + it('should emit HTTP requests given many debounced ACTIONs, and should emit ACTION given HTTP response', (done) => { |
| 79 | + const actionSource = { |
| 80 | + a: actions.searchUsers('l'), |
| 81 | + b: actions.searchUsers('lu'), |
| 82 | + c: actions.searchUsers('luc') |
| 83 | + }; |
| 84 | + const httpSource = { |
| 85 | + select: () => ({ |
| 86 | + r: xs.of({ body: { items: ['foo'] } }) |
| 87 | + }) |
| 88 | + } |
| 89 | + const httpSink = { |
| 90 | + a: { |
| 91 | + url: `https://api.github.com/search/users?q=luc`, |
| 92 | + category: 'query' |
| 93 | + } |
| 94 | + } |
| 95 | + const actionSink = { |
| 96 | + r: actions.receiveUsers(['foo']), |
| 97 | + } |
| 98 | + |
| 99 | + assertSourcesSinks({ |
| 100 | + ACTION: { '-a-b-c----|': actionSource }, |
| 101 | + HTTP: { '---r------|': httpSource }, |
| 102 | + }, { |
| 103 | + HTTP: { '---------a|': httpSink }, |
| 104 | + ACTION: { '---r------|': actionSink }, |
| 105 | + }, searchUsers, done, { interval: 200 }); |
| 106 | + }) |
| 107 | + }) |
155 | 108 | }); |
0 commit comments