-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathisolation.ts
177 lines (154 loc) · 4.59 KB
/
isolation.ts
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
import 'mocha';
import xs, {Stream} from 'xstream';
import {createElement, PureComponent} from 'react';
import isolate from '@cycle/isolate';
import * as renderer from 'react-test-renderer';
import {h, ReactSource, makeCycleReactComponent} from '../src/index';
const assert = require('assert');
class Inspect extends PureComponent<any, any> {
public press() {
if (this.props.onPress) {
this.props.onPress(this.props.name);
}
}
public render() {
return null;
}
}
describe('Isolation', function () {
it('prevents parent from selecting inside the child', (done) => {
function child(sources: {react: ReactSource}) {
sources.react
.select('bar')
.events('press')
.addListener({
next: (name) => {
// This listener exists just to make sure the child's inspect
// has an onPress prop
},
});
const vdom$ = xs.of(
h('div', {sel: 'foo'}, [h(Inspect, {sel: 'bar', name: 'wrong'})])
);
return {
react: vdom$,
};
}
function parent(sources: {react: ReactSource}) {
const childSinks = isolate(child, 'ISOLATION')(sources);
const vdom$ = childSinks.react.map((child) =>
h('div', {sel: 'top-most'}, [
h(Inspect, {sel: 'bar', name: 'correct'}),
child,
])
);
return {
react: vdom$,
};
}
let times = 0;
const RootComponent = makeCycleReactComponent(() => {
const source = new ReactSource();
const sink = parent({react: source}).react;
source
.select('bar')
.events('press')
.addListener({
next: (name) => {
assert.strictEqual(name, 'correct');
assert.strictEqual(times, 0);
times += 1;
},
});
return {source, sink};
});
const r = renderer.create(createElement(RootComponent));
const root = r.root;
setTimeout(() => {
const allInspects = root.findAllByType(Inspect, {deep: true});
const [correct, wrong] = allInspects;
assert.strictEqual(correct.props.name, 'correct');
assert.strictEqual(wrong.props.name, 'wrong');
wrong.instance.press();
setTimeout(() => {
correct.instance.press();
}, 100);
}, 100);
setTimeout(() => {
assert.strictEqual(times, 1);
done();
}, 300);
});
it('prevents component from selecting inside sibling', (done) => {
let times = 0;
function firstborn(sources: {react: ReactSource}) {
sources.react
.select('bar')
.events('press')
.addListener({
next: (name) => {
assert.strictEqual(name, 'correct');
assert.strictEqual(times, 0);
times += 1;
},
});
const vdom$ = xs.of(
h('div', {sel: 'foo'}, [h(Inspect, {sel: 'bar', name: 'correct'})])
);
return {
react: vdom$,
};
}
function secondborn(sources: {react: ReactSource}) {
sources.react
.select('bar')
.events('press')
.addListener({
next: (name) => {
// This listener exists just to make sure the child's inspect
// has an onPress prop
},
});
const vdom$ = xs.of(
h('div', {sel: 'foo'}, [h(Inspect, {sel: 'bar', name: 'wrong'})])
);
return {
react: vdom$,
};
}
function parent(sources: {react: ReactSource}) {
type Sinks = {react: Stream<React.ReactElement<any>>};
const firstSinks: Sinks = isolate(firstborn, 'first')(sources);
const secondSinks: Sinks = isolate(secondborn, 'second')(sources);
const vdom$ = xs
.combine(firstSinks.react, secondSinks.react)
.map(([firstChild, secondChild]) =>
h('div', {sel: 'top-most'}, [firstChild, secondChild])
);
return {
react: vdom$,
};
}
const RootComponent = makeCycleReactComponent(() => {
const source = new ReactSource();
const sink = parent({react: source}).react;
return {source, sink};
});
const r = renderer.create(createElement(RootComponent));
const root = r.root;
setTimeout(() => {
const allInspects = root.findAllByType(Inspect, {deep: true});
const [correct, wrong] = allInspects;
assert.strictEqual(correct.props.name, 'correct');
assert.strictEqual(wrong.props.name, 'wrong');
wrong.instance.press();
setTimeout(() => {
correct.instance.press();
}, 100);
}, 100);
setTimeout(() => {
assert.strictEqual(times, 1);
done();
}, 300);
});
});