Skip to content

Commit c811b17

Browse files
committed
Fix watchFn with createInstance and defaultProps.
1 parent 46af8a6 commit c811b17

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
5656
}
5757

5858
componentDidUpdate(prevProps) {
59-
const { watch, watchFn, promiseFn } = this.props
59+
const { watch, watchFn = defaultProps.watchFn, promiseFn } = this.props
6060
if (watch !== prevProps.watch) this.load()
61-
if (watchFn && watchFn(this.props, prevProps)) this.load()
61+
if (watchFn && watchFn({ ...defaultProps, ...this.props }, { ...defaultProps, ...prevProps }))
62+
this.load()
6263
if (promiseFn !== prevProps.promiseFn) {
6364
if (promiseFn) this.load()
6465
else this.cancel()

src/spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,18 @@ describe("createInstance", () => {
475475
expect(onResolve).toHaveBeenCalledWith("done")
476476
})
477477

478+
test("accepts watchFn from defaultProps and passes the defaultProps along", async () => {
479+
const promiseFn = () => resolveTo("done")
480+
const watchFn = jest.fn()
481+
const CustomAsync = createInstance({ promiseFn, watchFn })
482+
const { getByText } = render(<CustomAsync>{({ data }) => data || null}</CustomAsync>)
483+
await waitForElement(() => getByText("done"))
484+
expect(watchFn).toHaveBeenCalledWith(
485+
expect.objectContaining({ promiseFn, watchFn }),
486+
expect.objectContaining({ promiseFn, watchFn })
487+
)
488+
})
489+
478490
test("custom instances also have helper components", async () => {
479491
const promiseFn = () => resolveTo("done")
480492
const CustomAsync = createInstance({ promiseFn })

0 commit comments

Comments
 (0)