Skip to content

Commit

Permalink
main 🧊 add test for use query, add retry delay
Browse files Browse the repository at this point in the history
  • Loading branch information
debabin committed Dec 21, 2024
1 parent ab789b9 commit 4120b4d
Show file tree
Hide file tree
Showing 10 changed files with 280 additions and 152 deletions.
2 changes: 1 addition & 1 deletion src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export * from './useMap/useMap';
export * from './useMeasure/useMeasure';
export * from './useMediaQuery/useMediaQuery';
export * from './useMemory/useMemory';
export * from './useMessage/usePostMessage';
export * from './useMount/useMount';
export * from './useMouse/useMouse';
export * from './useMutation/useMutation';
Expand All @@ -70,6 +69,7 @@ export * from './usePaint/usePaint';
export * from './useParallax/useParallax';
export * from './usePermission/usePermission';
export * from './usePointerLock/usePointerLock';
export * from './usePostMessage/usePostMessage';
export * from './usePreferredColorScheme/usePreferredColorScheme';
export * from './usePreferredContrast/usePreferredContrast';
export * from './usePreferredDark/usePreferredDark';
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useClipboard/useClipboard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ it('Should use copy to clipboard', () => {
const { result } = renderHook(useClipboard);

expect(result.current.value).toBeNull();
expect(result.current.supported).toBe(true);
expect(result.current.supported).toBeTruthy();
expect(typeof result.current.copy).toBe('function');
});

Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useDeviceOrientation/useDeviceOrientation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ beforeAll(() => {
it('Should use on device orientation', () => {
const { result } = renderHook(useDeviceOrientation);

expect(result.current.supported).toBe(true);
expect(result.current.supported).toBeTruthy();
expect(result.current.value.alpha).toBeNull();
expect(result.current.value.beta).toBeNull();
expect(result.current.value.gamma).toBeNull();
Expand All @@ -38,5 +38,5 @@ it('Should set new values when device orientation change', () => {
expect(result.current.value.alpha).toBe(30);
expect(result.current.value.beta).toBe(60);
expect(result.current.value.gamma).toBe(90);
expect(result.current.value.absolute).toBe(true);
expect(result.current.value.absolute).toBeTruthy();
});
12 changes: 6 additions & 6 deletions src/hooks/useInterval/useInterval.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ beforeEach(() => {

it('Should use interval', () => {
const { result } = renderHook(() => useInterval(vi.fn, 1000));
expect(result.current.active).toBe(true);
expect(result.current.active).toBeTruthy();
expect(typeof result.current.pause).toBe('function');
expect(typeof result.current.resume).toBe('function');
});
Expand All @@ -17,24 +17,24 @@ it('Should pause and resume properly', () => {
const { result } = renderHook(() => useInterval(() => {}, 1000));
const { pause, resume } = result.current;

expect(result.current.active).toBe(true);
expect(result.current.active).toBeTruthy();
act(pause);
expect(result.current.active).toBe(false);
expect(result.current.active).toBeFalsy();
act(resume);
expect(result.current.active).toBe(true);
expect(result.current.active).toBeTruthy();
});

it('Should not be active when disabled', () => {
const { result } = renderHook(() => useInterval(() => {}, 1000, { enabled: false }));

expect(result.current.active).toBe(false);
expect(result.current.active).toBeFalsy();
});

it('Should call callback on interval', () => {
const callback = vi.fn();
const { result } = renderHook(() => useInterval(callback, 1000));

expect(result.current.active).toBe(true);
expect(result.current.active).toBeTruthy();
act(() => vi.advanceTimersByTime(1000));

expect(callback).toBeCalledTimes(1);
Expand Down
8 changes: 4 additions & 4 deletions src/hooks/usePageLeave/usePageLeave.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ it('Should use page leave', () => {
it('Should call the callback on page leave', () => {
const callback = vi.fn();
const { result } = renderHook(() => usePageLeave(callback));
expect(result.current).toBe(false);
expect(result.current).toBeFalsy();

act(() => document.dispatchEvent(new Event('mouseleave')));
expect(callback).toBeCalledTimes(1);
expect(result.current).toBe(true);
expect(result.current).toBeTruthy();

act(() => document.dispatchEvent(new Event('mouseenter')));
expect(result.current).toBe(false);
expect(result.current).toBeFalsy();

act(() => document.dispatchEvent(new Event('mouseleave')));
expect(callback).toBeCalledTimes(2);
expect(result.current).toBe(true);
expect(result.current).toBeTruthy();
});
31 changes: 19 additions & 12 deletions src/hooks/usePostMessage/usePostMessage.demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ import { usePostMessage } from './usePostMessage';
const Demo = () => {
const [messages, setMessages] = useState<string[]>([]);

const postMessage = usePostMessage<{ type: 'delete' } | { type: 'send'; value: string }>('*', (message) => {
console.log('Message received', message);

if (message.type === 'send') {
setMessages((prevMessages) => [...prevMessages, message.value]);
}

if (message.type === 'delete') {
setMessages((prevMessages) => prevMessages.slice(0, prevMessages.length - 1));
const postMessage = usePostMessage<{ type: 'delete' } | { type: 'send'; value: string }>(
'*',
(message) => {
console.log('Message received', message);

if (message.type === 'send') {
setMessages((prevMessages) => [...prevMessages, message.value]);
}

if (message.type === 'delete') {
setMessages((prevMessages) => prevMessages.slice(0, prevMessages.length - 1));
}
}
});
);

const onSendClick = () =>
postMessage({ type: 'send', value: (Math.random() + 1).toString(36).substring(3) });
Expand All @@ -35,8 +38,12 @@ const Demo = () => {
</ul>
)}
</div>
<button type='button' onClick={onSendClick}>Send message</button>
<button type='button' onClick={onDeleteClick}>Delete message</button>
<button type='button' onClick={onSendClick}>
Send message
</button>
<button type='button' onClick={onDeleteClick}>
Delete message
</button>
</>
);
};
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useQuery/useQuery.demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface Pokemon {
}

const getPokemon = (id: number) =>
fetch(`https://pokeapi.co/api/v2/pokemon/${id}`).then((res) => res.json()) as Promise<Pokemon>;
fetch(`https://pokeapi.co/api/v2/pokemon2/${id}`).then((res) => res.json()) as Promise<Pokemon>;

const Demo = () => {
const counter = useCounter(1);
Expand Down
Loading

0 comments on commit 4120b4d

Please sign in to comment.