Skip to content

Commit fc6d813

Browse files
committed
test: add unit-test for testing refresing state
1 parent b65b24b commit fc6d813

File tree

1 file changed

+38
-8
lines changed

1 file changed

+38
-8
lines changed

packages/typink/src/hooks/__tests__/useContractQuery.test.ts

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,14 @@ describe('useContractQuery', () => {
135135
expect(contract.query.testFunction).toHaveBeenCalledTimes(3);
136136
});
137137

138-
it('should handle errors from the contract query', async () => {
139-
const testError = new Error('Test error');
140-
contract.query.testFunction.mockRejectedValue(testError);
138+
it('should update refresh state when refresh function is called', async () => {
139+
contract.query.testFunction.mockImplementation(() => {
140+
return new Promise((resolve) => {
141+
setTimeout(() => {
142+
resolve({ data: 'test result' });
143+
}, 100);
144+
});
145+
});
141146

142147
const { result } = renderHook(() =>
143148
useContractQuery({
@@ -147,14 +152,39 @@ describe('useContractQuery', () => {
147152
}),
148153
);
149154

155+
expect(result.current.isRefreshing).toBe(false);
156+
150157
await act(async () => {
151-
await new Promise((resolve) => setTimeout(resolve, 0));
158+
result.current.refresh();
152159
});
153160

154-
expect(result.current.isLoading).toBe(false);
155-
expect(result.current.error).toBe(testError);
156-
expect(result.current.data).toBeUndefined();
157-
});
161+
expect(result.current.isRefreshing).toBe(true);
162+
163+
await waitFor(() => {
164+
expect(result.current.isRefreshing).toBe(false);
165+
expect(result.current.data).toBe('test result');
166+
});
167+
}),
168+
it('should handle errors from the contract query', async () => {
169+
const testError = new Error('Test error');
170+
contract.query.testFunction.mockRejectedValue(testError);
171+
172+
const { result } = renderHook(() =>
173+
useContractQuery({
174+
contract,
175+
// @ts-ignore
176+
fn: 'testFunction',
177+
}),
178+
);
179+
180+
await act(async () => {
181+
await new Promise((resolve) => setTimeout(resolve, 0));
182+
});
183+
184+
expect(result.current.isLoading).toBe(false);
185+
expect(result.current.error).toBe(testError);
186+
expect(result.current.data).toBeUndefined();
187+
});
158188

159189
it('should reset error state on successful query after an error', async () => {
160190
const testError = new Error('Test error');

0 commit comments

Comments
 (0)