-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.js
More file actions
107 lines (92 loc) · 3.13 KB
/
index.test.js
File metadata and controls
107 lines (92 loc) · 3.13 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
import React from "react";
import { configure, mount } from "enzyme";
import Adapter from "enzyme-adapter-react-16";
import flyd from "flyd";
import { connect } from ".";
configure({ adapter: new Adapter() });
test("stream connection", async () => {
// Connected stream.
const stream = flyd.stream(1);
// Set up mock components.
const Container = () => {
const childProps = {
count: connect(stream)
};
return React.createElement(View, childProps);
};
const View = ({ count }) => React.createElement("p", null, `${count}`);
const elm = mount(React.createElement(Container));
// Assert that the component contains the initial value.
expect(elm.find("p").text()).toBe("1");
// Emit a new value to update the component.
stream(2);
// Assert that the new value is applied to the component.
await new Promise(resolve => {
setTimeout(() => {
expect(elm.find("p").text()).toBe("2");
resolve(true);
}, 100);
});
});
test("selective stream connection", async () => {
// Selective streams.
const streams = {
one: flyd.stream(1),
two: flyd.stream(2)
};
// Set up mock components.
const ContainerWithDefaultConnection = ({ streamKey }) => {
const childProps = {
count: connect(streams[streamKey])
};
return React.createElement(View, childProps);
};
const ContainerWithIdenticalConnection = ({ streamKey }) => {
const childProps = {
count: connect(
streams[streamKey],
[]
)
};
return React.createElement(View, childProps);
};
const ContainerWithAppropriateConnection = ({ streamKey }) => {
const childProps = {
count: connect(
streams[streamKey],
[streamKey]
)
};
return React.createElement(View, childProps);
};
const View = ({ count }) => React.createElement("p", null, `${count}`);
const elmWithDefaultConnection = mount(
React.createElement(ContainerWithDefaultConnection, { streamKey: "one" })
);
const elmWithIdenticalConnection = mount(
React.createElement(ContainerWithIdenticalConnection, { streamKey: "one" })
);
const elmWithAppropriateConnection = mount(
React.createElement(ContainerWithAppropriateConnection, {
streamKey: "one"
})
);
// Assert that the components contains the initial value.
expect(elmWithDefaultConnection.find("p").text()).toBe("1");
expect(elmWithIdenticalConnection.find("p").text()).toBe("1");
expect(elmWithAppropriateConnection.find("p").text()).toBe("1");
// Make the components select the second stream.
elmWithDefaultConnection.setProps({ streamKey: "two" });
elmWithIdenticalConnection.setProps({ streamKey: "two" });
elmWithAppropriateConnection.setProps({ streamKey: "two" });
// Assert that the new value is applied to the component.
await new Promise(resolve => {
setTimeout(() => {
expect(elmWithDefaultConnection.find("p").text()).toBe("2");
// NOTE: Identical connection should not apply the new stream.
expect(elmWithIdenticalConnection.find("p").text()).toBe("1");
expect(elmWithAppropriateConnection.find("p").text()).toBe("2");
resolve(true);
}, 100);
});
});