Skip to content

Commit 03b423d

Browse files
authored
Merge pull request #24 from cyclejs-community/async-example-tests
Add async tests for example autocomplete using @cyclejs/time
2 parents 4a0a18d + 51eb6bd commit 03b423d

File tree

2 files changed

+82
-80
lines changed

2 files changed

+82
-80
lines changed

example/cycle/test/helpers.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import {mockTimeSource} from '@cycle/time';
2+
3+
export function assertSourcesSinks(sources, sinks, main, done, timeOpts = {}) {
4+
const Time = mockTimeSource(timeOpts);
5+
const _sources = Object.keys(sources)
6+
.reduce((_sources, sourceKey) => {
7+
const sourceObj = sources[sourceKey];
8+
const diagram = Object.keys(sourceObj)[0];
9+
const sourceOpts = sourceObj[diagram];
10+
11+
let obj = {};
12+
let firstKey = Object.keys(sourceOpts)[0];
13+
if (typeof sourceOpts[firstKey] === 'function') {
14+
obj = {
15+
[sourceKey]: {
16+
[firstKey]: () => Time.diagram(diagram, sourceOpts[firstKey]())
17+
}
18+
}
19+
} else {
20+
obj = {
21+
[sourceKey]: Time.diagram(diagram, sourceOpts)
22+
}
23+
}
24+
25+
return Object.assign(_sources, obj);
26+
}, {})
27+
28+
const _sinks = Object.keys(sinks)
29+
.reduce((_sinks, sinkKey) => {
30+
const sinkObj = sinks[sinkKey];
31+
const diagram = Object.keys(sinkObj)[0];
32+
const sinkOpts = sinkObj[diagram];
33+
34+
return Object.assign(_sinks, { [sinkKey]: Time.diagram(diagram, sinkOpts) });
35+
}, {});
36+
37+
// always pass Time as a source
38+
_sources.Time = Time;
39+
40+
const _main = main(_sources);
41+
42+
Object.keys(sinks)
43+
.map(sinkKey => Time.assertEqual(_main[sinkKey], _sinks[sinkKey]));
44+
45+
Time.run(err => {
46+
expect(err).toBeFalsy();
47+
done();
48+
});
49+
}

example/cycle/test/test.js

Lines changed: 33 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,12 @@
11
import assert from 'assert';
22
import xs from 'xstream';
3-
import fromDiagram from 'xstream/extra/fromDiagram';
4-
import {mockTimeSource} from '@cycle/time';
3+
4+
import { assertSourcesSinks } from './helpers';
55
import * as ActionTypes from '../../ActionTypes';
66
import * as actions from '../../actions';
77

88
import { fetchReposByUser, searchUsers } from '../';
99

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-
6110
describe('Cycles', function() {
6211
describe('fetchReposByUser', function() {
6312
it('should emit HTTP requests given ACTIONs', function(done) {
@@ -125,31 +74,35 @@ describe('Cycles', function() {
12574
});
12675
});
12776

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+
})
155108
});

0 commit comments

Comments
 (0)