-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test for null on first render by copying react-hooks-testing-library's renderHooks method
- Loading branch information
1 parent
37cf5cb
commit b007552
Showing
9 changed files
with
159 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// @flow strict | ||
import React from 'react'; | ||
|
||
const Example = () => <div> Some Component </div>; | ||
|
||
export default Example; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// @flow strict | ||
|
||
// eslint-disable-next-line import/prefer-default-export | ||
export { default as useLazy } from './useLazy'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// @flow strict | ||
import { useState, useEffect } from 'react'; | ||
|
||
const useLazy = ( | ||
getModule: () => Promise<*>, | ||
cond?: boolean = false, | ||
) => { | ||
const [AsyncModule, setAsyncModule] = useState(null); | ||
useEffect(() => { | ||
(async () => { | ||
try { | ||
if (!cond) { | ||
return; | ||
} | ||
const module = await getModule(); | ||
// eslint-disable-next-line no-console | ||
console.log('module', module); | ||
setAsyncModule(() => module.default); | ||
} catch (err) { | ||
throw new Error(`LazyComponent error: ${err}`); | ||
} | ||
})(); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [cond]); | ||
|
||
return AsyncModule; | ||
}; | ||
|
||
export default useLazy; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// @flow strict | ||
// $FlowFixMe | ||
import { renderHook } from '@/utils/testing/renderHook'; | ||
|
||
import useLazy from './useLazy'; | ||
|
||
const getModule = () => import('./Example'); | ||
|
||
describe('LazyComponent', () => { | ||
it('should render "null" at first', () => { | ||
const { result } = renderHook(() => useLazy(getModule, false)); | ||
expect(result.current).toEqual(null); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// this is taken from | ||
// @link https://raw.githubusercontent.com/testing-library/react-hooks-testing-library/master/src/index.js | ||
|
||
import React, { Suspense } from 'react'; | ||
import { act, create } from 'react-test-renderer'; | ||
|
||
function TestHook({ | ||
callback, hookProps, onError, children, | ||
}) { | ||
try { | ||
children(callback(hookProps)); | ||
} catch (err) { | ||
if (err.then) { | ||
throw err; | ||
} else { | ||
onError(err); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
function Fallback() { | ||
return null; | ||
} | ||
|
||
function resultContainer() { | ||
let value = null; | ||
let error = null; | ||
const resolvers = []; | ||
|
||
const result = { | ||
get current() { | ||
if (error) { | ||
throw error; | ||
} | ||
return value; | ||
}, | ||
get error() { | ||
return error; | ||
}, | ||
}; | ||
|
||
const updateResult = (val, err) => { | ||
value = val; | ||
error = err; | ||
resolvers.splice(0, resolvers.length).forEach(resolve => resolve()); | ||
}; | ||
|
||
return { | ||
result, | ||
addResolver: (resolver) => { | ||
resolvers.push(resolver); | ||
}, | ||
setValue: val => updateResult(val), | ||
setError: err => updateResult(undefined, err), | ||
}; | ||
} | ||
|
||
function renderHook(callback, { initialProps, wrapper } = {}) { | ||
const { | ||
result, setValue, setError, addResolver, | ||
} = resultContainer(); | ||
const hookProps = { current: initialProps }; | ||
|
||
const wrapUiIfNeeded = innerElement => ( | ||
wrapper | ||
? React.createElement(wrapper, null, innerElement) | ||
: innerElement | ||
); | ||
|
||
const toRender = () => wrapUiIfNeeded( | ||
<Suspense fallback={<Fallback />}> | ||
<TestHook callback={callback} hookProps={hookProps.current} onError={setError}> | ||
{setValue} | ||
</TestHook> | ||
</Suspense>, | ||
); | ||
|
||
let testRenderer; | ||
act(() => { | ||
testRenderer = create(toRender()); | ||
}); | ||
|
||
const { unmount, update } = testRenderer; | ||
|
||
return { | ||
result, | ||
waitForNextUpdate: () => new Promise(resolve => addResolver(resolve)), | ||
rerender: (newProps = hookProps.current) => { | ||
hookProps.current = newProps; | ||
act(() => { | ||
update(toRender()); | ||
}); | ||
}, | ||
unmount: () => { | ||
act(() => { | ||
unmount(); | ||
}); | ||
}, | ||
}; | ||
} | ||
|
||
export { renderHook, act }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters