Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ export const createInstance = (defaultProps = {}) => {
<Consumer>
{state => {
if (state.data === undefined) return null
if (state.isLoading && !persist) return null
if (!persist && state.isLoading) return null
if (!persist && state.error !== undefined) return null
return isFunction(children) ? children(state.data, state) : children || null
}}
</Consumer>
Expand Down
40 changes: 40 additions & 0 deletions src/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,43 @@ test("An unrelated change in props does not update the Context", async () => {
)
expect(one).toBe(two)
})

test("Async.Resolved does not render after deferFn rejection", async () => {
const promiseFn = () => resolveTo("ok")
const deferFn = () => rejectTo("notok")
const { getByText, queryByText } = render(
<Async promiseFn={promiseFn} deferFn={deferFn}>
<Async.Rejected>fail</Async.Rejected>
<Async.Resolved>{(data, { run }) => <button onClick={run}>next</button>}</Async.Resolved>
</Async>
)

expect(queryByText("next")).toBeNull()
await waitForElement(() => getByText("next"))
expect(queryByText("next")).toBeInTheDocument()
expect(queryByText("fail")).toBeNull()
fireEvent.click(getByText("next"))
await waitForElement(() => getByText("fail"))
expect(queryByText("next")).toBeNull()
expect(queryByText("fail")).toBeInTheDocument()
})

test("Async.Resolved renders after deferFn rejection with persist", async () => {
const promiseFn = () => resolveTo("ok")
const deferFn = () => rejectTo("notok")
const { getByText, queryByText } = render(
<Async promiseFn={promiseFn} deferFn={deferFn}>
<Async.Rejected>fail</Async.Rejected>
<Async.Resolved persist>{(data, { run }) => <button onClick={run}>next</button>}</Async.Resolved>
</Async>
)

expect(queryByText("next")).toBeNull()
await waitForElement(() => getByText("next"))
expect(queryByText("next")).toBeInTheDocument()
expect(queryByText("fail")).toBeNull()
fireEvent.click(getByText("next"))
await waitForElement(() => getByText("fail"))
expect(queryByText("next")).toBeInTheDocument()
expect(queryByText("fail")).toBeInTheDocument()
})