Skip to content

Commit dfc9d0e

Browse files
fix(@interchain-kit/react): fix warning when test
1 parent 8fba1f2 commit dfc9d0e

File tree

4 files changed

+82
-51
lines changed

4 files changed

+82
-51
lines changed

packages/react/__tests__/hooks/useAsync.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,19 @@ describe('useAsync', () => {
88
});
99
afterEach(cleanup)
1010

11-
it('should return initial state', () => {
11+
it('should return initial state', async () => {
1212
const { result } = renderHook(() =>
1313
useAsync({
1414
queryKey: 'test1',
1515
queryFn: async () => 'data',
1616
})
1717
);
18+
await waitFor(() => {
19+
expect(result.current.data).toBeNull();
20+
expect(result.current.isLoading).toBe(true);
21+
expect(result.current.error).toBeNull();
22+
})
1823

19-
expect(result.current.data).toBeNull();
20-
expect(result.current.isLoading).toBe(true);
21-
expect(result.current.error).toBeNull();
2224
});
2325

2426
it('should fetch data and update state', async () => {

packages/react/__tests__/hooks/useChain.test.ts

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @jest-environment jsdom
33
*/
44

5-
import { renderHook, act } from '@testing-library/react';
5+
import { renderHook, act, waitFor } from '@testing-library/react';
66
import { useChain } from '../../src/hooks/useChain';
77
import { useWalletManager } from '../../src/hooks/useWalletManager';
88
import { useWalletModal } from '../../src/modal';
@@ -75,7 +75,7 @@ describe('useChain', () => {
7575
}).toThrow(new ChainNameNotExist('non-existent-chain'));
7676
});
7777

78-
it('should return correct values for an existing chain', () => {
78+
it('should return correct values for an existing chain', async () => {
7979

8080
const mockChainWalletState: ChainWalletState = {
8181
walletState: WalletState.Connected,
@@ -92,25 +92,31 @@ describe('useChain', () => {
9292

9393
const { result } = renderHook(() => useChain('test-chain'));
9494

95-
expect(result.current.chain).toEqual(mockChain);
96-
expect(result.current.status).toBe(WalletState.Connected);
97-
expect(result.current.username).toBe('test-user');
98-
expect(result.current.address).toBe('test-address');
99-
expect(result.current.rpcEndpoint).toBe('http://localhost:26657');
95+
await waitFor(() => {
96+
expect(result.current.chain).toEqual(mockChain);
97+
expect(result.current.status).toBe(WalletState.Connected);
98+
expect(result.current.username).toBe('test-user');
99+
expect(result.current.address).toBe('test-address');
100+
expect(result.current.rpcEndpoint).toBe('http://localhost:26657');
101+
})
102+
103+
100104
});
101105

102-
it('should call connect and open modal when connect is invoked', () => {
106+
it('should call connect and open modal when connect is invoked', async () => {
103107

104108
mockWalletManager.getChainByName.mockReturnValue(mockChain);
105109

106110
const { result } = renderHook(() => useChain('test-chain'));
107111

108-
act(() => {
112+
await act(() => {
109113
result.current.connect();
110114
});
111115

112-
expect(mockWalletManager.setCurrentChainName).toHaveBeenCalledWith('test-chain');
113-
expect(mockWalletModal.open).toHaveBeenCalled();
116+
await waitFor(() => {
117+
expect(mockWalletManager.setCurrentChainName).toHaveBeenCalledWith('test-chain');
118+
expect(mockWalletModal.open).toHaveBeenCalled();
119+
})
114120
});
115121

116122
it('should call disconnect when disconnect is invoked', async () => {
@@ -123,33 +129,42 @@ describe('useChain', () => {
123129
await result.current.disconnect();
124130
});
125131

126-
expect(mockWalletManager.disconnect).toHaveBeenCalledWith('test-wallet', 'test-chain');
132+
await waitFor(() => {
133+
134+
expect(mockWalletManager.disconnect).toHaveBeenCalledWith('test-wallet', 'test-chain');
135+
})
136+
127137
});
128138

129-
it('should call open modal when openView is invoked', () => {
139+
it('should call open modal when openView is invoked', async () => {
130140

131141
mockWalletManager.getChainByName.mockReturnValue(mockChain);
132142

133143
const { result } = renderHook(() => useChain('test-chain'));
134144

135-
act(() => {
145+
await act(() => {
136146
result.current.openView();
137147
});
138148

139-
expect(mockWalletManager.setCurrentChainName).toHaveBeenCalledWith('test-chain');
140-
expect(mockWalletModal.open).toHaveBeenCalled();
149+
await waitFor(() => {
150+
expect(mockWalletManager.setCurrentChainName).toHaveBeenCalledWith('test-chain');
151+
expect(mockWalletModal.open).toHaveBeenCalled();
152+
})
153+
141154
});
142155

143-
it('should call close modal when closeView is invoked', () => {
156+
it('should call close modal when closeView is invoked', async () => {
144157

145158
mockWalletManager.getChainByName.mockReturnValue(mockChain);
146159

147160
const { result } = renderHook(() => useChain('test-chain'));
148161

149-
act(() => {
162+
await act(() => {
150163
result.current.closeView();
151164
});
152165

153-
expect(mockWalletModal.close).toHaveBeenCalled();
166+
await waitFor(() => {
167+
expect(mockWalletModal.close).toHaveBeenCalled();
168+
})
154169
});
155170
});

packages/react/__tests__/hooks/useChainWallet.test.ts

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @jest-environment jsdom
33
*/
44

5-
import { renderHook } from "@testing-library/react"
5+
import { act, renderHook, waitFor } from "@testing-library/react"
66
import { useChainWallet } from "../../src/hooks/useChainWallet"
77
import { useWalletManager } from "../../src/hooks/useWalletManager"
88
import { ChainWallet } from "../../src/store/chain-wallet"
@@ -61,7 +61,7 @@ describe("useChainWallet", () => {
6161
; (useWalletManager as jest.Mock).mockReturnValue(mockWalletManager)
6262
})
6363

64-
it("should return the correct chain and wallet data", () => {
64+
it("should return the correct chain and wallet data", async () => {
6565
const chainName = "test-chain"
6666
const walletName = "test-wallet"
6767

@@ -83,26 +83,32 @@ describe("useChainWallet", () => {
8383

8484
const { result } = renderHook(() => useChainWallet(chainName, walletName))
8585

86-
expect(result.current.chain).toEqual(mockChain)
87-
expect(result.current.wallet).toBeInstanceOf(ChainWallet)
88-
expect(result.current.assetList).toEqual(mockWalletManager.assetLists[0])
89-
expect(result.current.status).toBe(WalletState.Connected)
90-
expect(result.current.username).toBe("test-user")
91-
expect(result.current.address).toBe("test-address")
92-
expect(result.current.message).toBe("")
93-
expect(result.current.rpcEndpoint).toBe("http://localhost:26657")
94-
expect(result.current.logoUrl).toBe("http://logo.url")
86+
await waitFor(() => {
87+
expect(result.current.chain).toEqual(mockChain)
88+
expect(result.current.wallet).toBeInstanceOf(ChainWallet)
89+
expect(result.current.assetList).toEqual(mockWalletManager.assetLists[0])
90+
expect(result.current.status).toBe(WalletState.Connected)
91+
expect(result.current.username).toBe("test-user")
92+
expect(result.current.address).toBe("test-address")
93+
expect(result.current.message).toBe("")
94+
expect(result.current.rpcEndpoint).toBe("http://localhost:26657")
95+
expect(result.current.logoUrl).toBe("http://logo.url")
96+
})
9597
})
9698

97-
it("should call disconnect when disconnect is invoked", () => {
99+
it("should call disconnect when disconnect is invoked", async () => {
98100
const chainName = "test-chain"
99101
const walletName = "test-wallet"
100102

101103
const { result } = renderHook(() => useChainWallet(chainName, walletName))
102104

103105
result.current.disconnect()
104106

105-
expect(mockWalletManager.disconnect).toHaveBeenCalledWith(walletName, chainName)
107+
await waitFor(() => {
108+
expect(mockWalletManager.disconnect).toHaveBeenCalledWith(walletName, chainName)
109+
})
110+
111+
106112
})
107113

108114
it("should return the correct RPC endpoint", async () => {
@@ -115,10 +121,12 @@ describe("useChainWallet", () => {
115121

116122
const endpointResult = await result.current.getRpcEndpoint()
117123

118-
expect(endpointResult).toBe("http://localhost:26657")
124+
await waitFor(() => {
125+
expect(endpointResult).toBe("http://localhost:26657")
126+
})
119127
})
120128

121-
it("should return the signing client", () => {
129+
it("should return the signing client", async () => {
122130
const chainName = "test-chain"
123131
const walletName = "test-wallet"
124132

@@ -129,6 +137,8 @@ describe("useChainWallet", () => {
129137

130138
const { result } = renderHook(() => useChainWallet(chainName, walletName))
131139

132-
expect(result.current.getSigningClient()).toBe(mockSigningClient)
140+
await waitFor(() => {
141+
expect(result.current.getSigningClient()).toBe(mockSigningClient)
142+
})
133143
})
134144
})

packages/react/__tests__/modal/provider.test.tsx

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import React from "react";
2-
import { render, screen, fireEvent } from "@testing-library/react";
1+
import React, { act } from "react";
2+
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
33
import { WalletModalProvider, useWalletModal } from "../../src/modal/provider";
44
import "@testing-library/jest-dom";
55
import { ChainProvider } from "../../src/provider";
@@ -18,7 +18,7 @@ const TestComponent = () => {
1818
};
1919

2020
describe("WalletModalProvider", () => {
21-
it("should render children correctly", () => {
21+
it("should render children correctly", async () => {
2222
render(
2323
<ChainProvider chains={[chain]} assetLists={[assetList]} wallets={[]}>
2424
<WalletModalProvider>
@@ -27,17 +27,21 @@ describe("WalletModalProvider", () => {
2727
</ChainProvider>
2828
);
2929

30-
expect(screen.getByText("Test Child")).toBeInTheDocument();
30+
await waitFor(() => {
31+
expect(screen.getByText("Test Child")).toBeInTheDocument();
32+
});
3133
});
3234

33-
it("should open and close the modal", () => {
34-
render(
35-
<ChainProvider chains={[chain]} assetLists={[assetList]} wallets={[]}>
36-
<WalletModalProvider>
37-
<TestComponent />
38-
</WalletModalProvider>
39-
</ChainProvider>
40-
);
35+
it("should open and close the modal", async () => {
36+
await act(async () => {
37+
render(
38+
<ChainProvider chains={[chain]} assetLists={[assetList]} wallets={[]}>
39+
<WalletModalProvider>
40+
<TestComponent />
41+
</WalletModalProvider>
42+
</ChainProvider>
43+
);
44+
});
4145

4246
const openButton = screen.getByText("Open Modal");
4347
const closeButton = screen.getByText("Close Modal");

0 commit comments

Comments
 (0)