Skip to content

Commit a42ecdb

Browse files
Merge pull request #20 from msmith-techempower/test-async-await
Adds compound async action test
2 parents a7fd424 + f46f70b commit a42ecdb

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/examples/counter/Counter.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ export default function Counter() {
2121
/>
2222
<button className="removeState" onClick={() => actions.removeState()} />
2323
<button className="asyncFunc" onClick={actions.asyncFunc} />
24+
<button
25+
className="compoundAsyncFunc"
26+
onClick={async () => {
27+
// Should set `state.count` = 256
28+
await actions.asyncFunc();
29+
// Should set `state.count` = 257
30+
actions.increment();
31+
}}
32+
/>
2433
<input className="googleStatus" value={state.status} />
2534
<button className="fetchGoogle" onClick={actions.fetchGoogle} />
2635
</div>

src/tests/counter/Counter.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,20 @@ it("can asyncFunc", async () => {
158158
});
159159
});
160160

161+
it("can do compound async actions", async () => {
162+
const counter = TestRenderer.create(<Counter />);
163+
const val = counter.root.findByProps({ className: "val" });
164+
const button = counter.root.findByProps({ className: "compoundAsyncFunc" });
165+
166+
await act(async () => {
167+
expect(val.props.value).toBe(0);
168+
169+
await button.props.onClick();
170+
171+
expect(val.props.value).toBe(257);
172+
});
173+
});
174+
161175
it("can fetchGoogle", async () => {
162176
const counter = TestRenderer.create(<Counter />);
163177
const val = counter.root.findByProps({ className: "googleStatus" });

0 commit comments

Comments
 (0)