forked from almin/almin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalmin-react-container-test.tsx
160 lines (141 loc) · 5.54 KB
/
almin-react-container-test.tsx
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
import * as assert from "assert";
import { Context, Dispatcher, Store, StoreGroup } from "almin";
import AlminReactContainer from "../src/almin-react-container";
import * as React from "react";
import * as TestUtils from "react-dom/test-utils";
const createTestStore = <T extends {}>(initialState: T) => {
class TestStore extends Store<T> {
state: T;
constructor() {
super();
this.state = initialState;
}
updateState(newState: T) {
if (this.state !== newState) {
this.state = newState;
this.emitChange();
}
}
getState() {
return this.state;
}
}
return new TestStore();
};
describe("almin-react-container", () => {
context("when update with new state", () => {
it("should update WrapperComponent with new props", () => {
let updatedCount = 0;
class Passthrough extends React.Component {
componentWillUpdate() {
updatedCount++;
}
render() {
return <div />;
}
}
// initial state
const initialState = {
testKey: "initial value"
};
const testStore = createTestStore(initialState);
const context = new Context({
dispatcher: new Dispatcher(),
store: testStore
});
const Container = AlminReactContainer.create(Passthrough, context);
const tree = TestUtils.renderIntoDocument<React.Component>(<Container />) as React.Component;
const container = TestUtils.findRenderedComponentWithType(tree, Container);
const stub = TestUtils.findRenderedComponentWithType(tree, Passthrough);
// Initial state
assert.strictEqual(updatedCount, 0);
assert.deepStrictEqual(container.state, initialState);
assert.deepStrictEqual(stub.props, initialState);
// Update state
const newState = {
testKey: "new value"
};
testStore.updateState(newState);
assert.strictEqual(updatedCount, 1);
assert.deepStrictEqual(container.state, newState, "should update state");
assert.deepStrictEqual(stub.props, newState, "should update props");
});
});
context("when update with same state", () => {
it("should not update WrapperComponent", () => {
let updatedCount = 0;
class Passthrough extends React.Component {
componentWillUpdate() {
updatedCount++;
}
render() {
return <div />;
}
}
// initial state
const initialState = {
testKey: "initial value"
};
const testStore = createTestStore(initialState);
const context = new Context({
dispatcher: new Dispatcher(),
store: testStore
});
const Container = AlminReactContainer.create(Passthrough, context);
const tree = TestUtils.renderIntoDocument(<Container />);
const container = TestUtils.findRenderedComponentWithType(tree as any, Container);
const stub = TestUtils.findRenderedComponentWithType(tree as any, Passthrough);
// Initial state
assert.strictEqual(updatedCount, 0);
assert.deepEqual(container.state, initialState);
assert.deepEqual(stub.props, initialState);
// Update same state
const newState = initialState;
testStore.updateState(newState);
assert.strictEqual(updatedCount, 0);
assert.deepEqual(container.state, newState, "should update state");
assert.deepEqual(stub.props, newState, "should not update props");
});
});
context("with custom props", () => {
it("should pass props to Container", () => {
// Store
class MyState {
value: string;
constructor({ value }: { value: string }) {
this.value = value;
}
}
class MyStore extends Store<MyState> {
state: MyState;
constructor() {
super();
this.state = new MyState({ value: "initial" });
}
getState() {
return this.state;
}
}
const storeGroup = new StoreGroup({
myState: new MyStore()
});
const context = new Context({
dispatcher: new Dispatcher(),
store: storeGroup
});
type PassthroughProps = { custom: string } & typeof storeGroup.state;
class Passthrough extends React.Component<PassthroughProps> {
render() {
return <div>{this.props.custom}</div>;
}
}
const Container = AlminReactContainer.create(Passthrough, context);
const tree = TestUtils.renderIntoDocument(<Container custom={"value"} />);
const stub = TestUtils.findRenderedComponentWithType(
(tree as any) as React.Component<typeof Container>,
Passthrough as any
);
assert.equal((stub.props as any).custom, "value", "should have custom value");
});
});
});