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
31 changes: 31 additions & 0 deletions src/hooks/useLoader.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {act, renderHook, waitFor} from '@testing-library/react';
import {StrictMode} from 'react';
import {useLoader} from './useLoader';

describe('useLoader', () => {
Expand Down Expand Up @@ -404,4 +405,34 @@ describe('useLoader', () => {

await waitFor(() => expect(secondTime.current).toBe('foo'));
});

it('should update the content in StrictMode', async () => {
jest.useFakeTimers();

const delay = 10;
const loader = jest.fn(
() => new Promise(resolve => {
setTimeout(() => resolve('foo'), delay);
}),
);

const {result} = renderHook(
() => useLoader({
cacheKey: cacheKey.current(),
loader: loader,
initial: 'bar',
}),
{
wrapper: StrictMode,
},
);

// Let the loader resolve
await act(async () => {
jest.advanceTimersByTime(delay);
await flushPromises();
});

await waitFor(() => expect(result.current).toBe('foo'));
});
});
2 changes: 2 additions & 0 deletions src/hooks/useLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export function useLoader<R>({initial, ...currentOptions}: CacheOptions<R>): R {

useEffect(
() => {
mountedRef.current = true;

if (initial !== undefined) {
load(currentOptions);
}
Expand Down