@0xsequence/marketplace-sdk 0.5.2 → 0.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (165) hide show
  1. package/dist/builder-types-wOwfTJpd.d.ts +73 -0
  2. package/dist/{chunk-FCF57DZI.js → chunk-7FN62HOP.js} +5 -9
  3. package/dist/chunk-7FN62HOP.js.map +1 -0
  4. package/dist/{chunk-MSTTVFVQ.js → chunk-BVXIRVEC.js} +251 -241
  5. package/dist/chunk-BVXIRVEC.js.map +1 -0
  6. package/dist/{chunk-XP3WY5AX.js → chunk-BZD2LDJJ.js} +2 -2
  7. package/dist/{chunk-XP3WY5AX.js.map → chunk-BZD2LDJJ.js.map} +1 -1
  8. package/dist/chunk-DZKPDV63.js +27 -0
  9. package/dist/chunk-DZKPDV63.js.map +1 -0
  10. package/dist/{chunk-I37CRQ4S.js → chunk-H5YWG6WN.js} +128 -88
  11. package/dist/chunk-H5YWG6WN.js.map +1 -0
  12. package/dist/{chunk-ZUEQGPLO.js → chunk-J6F5QOW5.js} +2 -2
  13. package/dist/{chunk-ZUEQGPLO.js.map → chunk-J6F5QOW5.js.map} +1 -1
  14. package/dist/{chunk-LJAB3S6U.js → chunk-TFRAOS7F.js} +22 -13
  15. package/dist/chunk-TFRAOS7F.js.map +1 -0
  16. package/dist/{chunk-5NORRVPM.js → chunk-UZIAX32Y.js} +1 -1
  17. package/dist/{chunk-5NORRVPM.js.map → chunk-UZIAX32Y.js.map} +1 -1
  18. package/dist/{chunk-MKGSGTQC.js → chunk-Y7YTLAO2.js} +3 -3
  19. package/dist/{create-config-BXvwUh55.d.ts → create-config-Bltg8Enl.d.ts} +1 -1
  20. package/dist/index.css +1 -1
  21. package/dist/index.d.ts +3 -3
  22. package/dist/index.js +10 -6
  23. package/dist/react/_internal/index.d.ts +3 -3
  24. package/dist/react/_internal/index.js +6 -6
  25. package/dist/react/_internal/wagmi/index.d.ts +2 -3
  26. package/dist/react/_internal/wagmi/index.js +1 -1
  27. package/dist/react/hooks/index.d.ts +72 -20
  28. package/dist/react/hooks/index.js +5 -7
  29. package/dist/react/index.css +1 -1
  30. package/dist/react/index.css.map +1 -1
  31. package/dist/react/index.d.ts +4 -4
  32. package/dist/react/index.js +10 -12
  33. package/dist/react/ssr/index.d.ts +54 -41
  34. package/dist/react/ssr/index.js +5 -9
  35. package/dist/react/ssr/index.js.map +1 -1
  36. package/dist/react/ui/components/collectible-card/index.css +1 -1
  37. package/dist/react/ui/components/collectible-card/index.css.map +1 -1
  38. package/dist/react/ui/components/collectible-card/index.d.ts +2 -2
  39. package/dist/react/ui/components/collectible-card/index.js +8 -8
  40. package/dist/react/ui/index.css +1 -1
  41. package/dist/react/ui/index.css.map +1 -1
  42. package/dist/react/ui/index.d.ts +2 -2
  43. package/dist/react/ui/index.js +8 -8
  44. package/dist/react/ui/modals/_internal/components/actionModal/index.d.ts +2 -2
  45. package/dist/react/ui/modals/_internal/components/actionModal/index.js +6 -6
  46. package/dist/react/ui/styles/index.d.ts +1 -1
  47. package/dist/styles/index.css +1 -1
  48. package/dist/styles/index.css.map +1 -1
  49. package/dist/styles/index.d.ts +1 -1
  50. package/dist/styles/index.js +1 -1
  51. package/dist/types/index.d.ts +1 -3
  52. package/dist/types/index.js +9 -5
  53. package/dist/{types-Yto6KrTN.d.ts → types-BY3husBh.d.ts} +1 -1
  54. package/dist/utils/index.d.ts +2 -2
  55. package/dist/utils/index.js +4 -4
  56. package/package.json +11 -10
  57. package/src/react/_internal/api/__mocks__/indexer.msw.ts +197 -0
  58. package/src/react/_internal/api/__mocks__/marketplace.msw.ts +140 -1
  59. package/src/react/_internal/api/__mocks__/metadata.msw.ts +162 -0
  60. package/src/react/_internal/test/mocks/publicClient.ts +39 -0
  61. package/src/react/_internal/test/mocks/wagmi.ts +61 -0
  62. package/src/react/_internal/test/mocks/wallet.ts +61 -0
  63. package/src/react/_internal/test/setup.ts +28 -0
  64. package/src/react/_internal/test-utils.tsx +31 -2
  65. package/src/react/_internal/wagmi/__tests__/create-config.test.ts +53 -20
  66. package/src/react/_internal/wagmi/create-config.ts +3 -4
  67. package/src/react/_internal/wagmi/embedded.ts +1 -4
  68. package/src/react/_internal/wagmi/universal.ts +1 -4
  69. package/src/react/_internal/wallet/wallet.ts +1 -0
  70. package/src/react/hooks/__tests__/useAutoSelectFeeOption.test.tsx +314 -0
  71. package/src/react/hooks/__tests__/useBalanceOfCollectible.test.tsx +148 -0
  72. package/src/react/hooks/__tests__/useCancelOrder.test.tsx +410 -0
  73. package/src/react/hooks/__tests__/useCancelTransactionSteps.test.tsx +269 -0
  74. package/src/react/hooks/__tests__/useCollectible.test.tsx +120 -0
  75. package/src/react/hooks/__tests__/useCollection.test.tsx +101 -0
  76. package/src/react/hooks/__tests__/useCollectionBalanceDetails.test.tsx +175 -0
  77. package/src/react/hooks/__tests__/useCollectionDetails.test.tsx +82 -0
  78. package/src/react/hooks/__tests__/useCollectionDetailsPolling.test.tsx +133 -0
  79. package/src/react/hooks/__tests__/useCountListingsForCollectible.test.tsx +108 -0
  80. package/src/react/hooks/__tests__/useCountOfCollectables.test.tsx +129 -0
  81. package/src/react/hooks/__tests__/useCountOffersForCollectible.test.tsx +108 -0
  82. package/src/react/hooks/__tests__/useCurrencies.test.tsx +176 -0
  83. package/src/react/hooks/__tests__/useCurrency.test.tsx +153 -0
  84. package/src/react/hooks/__tests__/useCurrencyBalance.test.tsx +111 -0
  85. package/src/react/hooks/__tests__/useFilters.test.tsx +127 -0
  86. package/src/react/hooks/__tests__/useFloorOrder.test.tsx +101 -0
  87. package/src/react/hooks/__tests__/useGenerateBuyTransaction.test.tsx +173 -0
  88. package/src/react/hooks/__tests__/useGenerateCancelTransaction.test.tsx +207 -0
  89. package/src/react/hooks/__tests__/useGenerateListingTransaction.test.tsx +207 -0
  90. package/src/react/hooks/__tests__/useGenerateOfferTransaction.test.tsx +205 -0
  91. package/src/react/hooks/__tests__/useGenerateSellTransaction.test.tsx +181 -0
  92. package/src/react/hooks/__tests__/useHighestOffer.test.tsx +118 -0
  93. package/src/react/hooks/__tests__/useListBalances.test.tsx +136 -0
  94. package/src/react/hooks/__tests__/useListCollectibleActivities.test.tsx +200 -0
  95. package/src/react/hooks/__tests__/useListCollectibles.test.tsx +232 -0
  96. package/src/react/hooks/__tests__/useListCollectionActivities.test.tsx +235 -0
  97. package/src/react/hooks/__tests__/useListCollections.test.tsx +296 -0
  98. package/src/react/hooks/__tests__/useListListingsForCollectible.test.tsx +140 -0
  99. package/src/react/hooks/__tests__/useListOffersForCollectible.test.tsx +140 -0
  100. package/src/react/hooks/__tests__/useLowestListing.test.tsx +148 -0
  101. package/src/react/hooks/__tests__/useMarketplaceConfig.test.tsx +106 -0
  102. package/src/react/hooks/__tests__/useRoyaltyPercentage.test.tsx +129 -0
  103. package/src/react/hooks/index.ts +0 -1
  104. package/src/react/hooks/options/__mocks__/marketplaceConfig.msw.ts +66 -10
  105. package/src/react/hooks/options/__tests__/marketplaceConfigOptions.test.tsx +2 -11
  106. package/src/react/hooks/options/marketplaceConfigOptions.ts +8 -3
  107. package/src/react/hooks/useAutoSelectFeeOption.tsx +4 -3
  108. package/src/react/hooks/useCancelTransactionSteps.tsx +17 -9
  109. package/src/react/hooks/useCollectionDetailsPolling.tsx +1 -1
  110. package/src/react/hooks/useCurrencies.tsx +29 -28
  111. package/src/react/hooks/useFilters.tsx +69 -2
  112. package/src/react/hooks/useGenerateBuyTransaction.tsx +13 -5
  113. package/src/react/hooks/useListCollectibleActivities.tsx +1 -0
  114. package/src/react/hooks/useListCollectibles.tsx +1 -0
  115. package/src/react/hooks/useListCollectionActivities.tsx +1 -0
  116. package/src/react/hooks/useListCollections.tsx +2 -2
  117. package/src/react/ui/components/_internals/custom-select/__tests__/CustomSelect.test.tsx +6 -2
  118. package/src/react/ui/components/collectible-card/CollectibleCard.tsx +1 -1
  119. package/src/react/ui/components/collectible-card/Footer.tsx +9 -5
  120. package/src/react/ui/modals/BuyModal/Modal.tsx +9 -4
  121. package/src/react/ui/modals/BuyModal/__tests__/Modal.test.tsx +0 -1
  122. package/src/react/ui/modals/BuyModal/__tests__/store.test.ts +4 -2
  123. package/src/react/ui/modals/BuyModal/hooks/__tests__/useBuyCollectable.test.tsx +1 -24
  124. package/src/react/ui/modals/BuyModal/hooks/__tests__/useCheckoutOptions.test.tsx +152 -210
  125. package/src/react/ui/modals/BuyModal/hooks/__tests__/useFees.test.tsx +19 -49
  126. package/src/react/ui/modals/BuyModal/hooks/useFees.ts +6 -6
  127. package/src/react/ui/modals/BuyModal/modals/Modal1155.tsx +4 -2
  128. package/src/react/ui/modals/BuyModal/modals/__tests__/Modal1155.test.tsx +161 -52
  129. package/src/react/ui/modals/BuyModal/store.ts +7 -0
  130. package/src/react/ui/modals/CreateListingModal/Modal.tsx +1 -3
  131. package/src/react/ui/modals/CreateListingModal/__tests__/Modal.test.tsx +59 -227
  132. package/src/react/ui/modals/CreateListingModal/hooks/useCreateListing.tsx +2 -1
  133. package/src/react/ui/modals/CreateListingModal/hooks/useTransactionSteps.tsx +9 -5
  134. package/src/react/ui/modals/MakeOfferModal/Modal.tsx +1 -8
  135. package/src/react/ui/modals/MakeOfferModal/__tests__/Modal.test.tsx +41 -118
  136. package/src/react/ui/modals/MakeOfferModal/hooks/useMakeOffer.tsx +2 -1
  137. package/src/react/ui/modals/MakeOfferModal/hooks/useTransactionSteps.tsx +9 -5
  138. package/src/react/ui/modals/SellModal/__tests__/Modal.test.tsx +4 -3
  139. package/src/react/ui/modals/SellModal/hooks/useGetTokenApproval.tsx +33 -31
  140. package/src/react/ui/modals/SellModal/hooks/useSell.tsx +1 -0
  141. package/src/react/ui/modals/SellModal/hooks/useTransactionSteps.tsx +9 -5
  142. package/src/react/ui/modals/SuccessfulPurchaseModal/__tests__/Modal.test.tsx +0 -1
  143. package/src/react/ui/modals/_internal/components/actionModal/ErrorModal.tsx +4 -2
  144. package/src/react/ui/modals/_internal/components/currencyOptionsSelect/__tests__/index.test.tsx +129 -57
  145. package/src/react/ui/modals/_internal/components/currencyOptionsSelect/index.tsx +1 -3
  146. package/src/react/ui/modals/_internal/components/priceInput/__tests__/index.test.tsx +1 -3
  147. package/src/react/ui/modals/_internal/components/transactionDetails/index.tsx +2 -2
  148. package/src/react/ui/modals/_internal/components/transactionStatusModal/__tests__/TransactionStatusModal.test.tsx +8 -8
  149. package/src/react/ui/modals/_internal/components/transactionStatusModal/hooks/useTransactionStatus.ts +1 -0
  150. package/src/types/builder-types.ts +79 -0
  151. package/src/types/index.ts +1 -1
  152. package/src/utils/__tests__/get-public-rpc-client.test.ts +2 -0
  153. package/src/utils/getMarketplaceDetails.ts +2 -2
  154. package/tsconfig.tsbuildinfo +1 -1
  155. package/vitest.config.js +2 -1
  156. package/dist/chunk-FCF57DZI.js.map +0 -1
  157. package/dist/chunk-I37CRQ4S.js.map +0 -1
  158. package/dist/chunk-LJAB3S6U.js.map +0 -1
  159. package/dist/chunk-MSTTVFVQ.js.map +0 -1
  160. package/dist/chunk-RK6KYMZM.js +0 -18
  161. package/dist/chunk-RK6KYMZM.js.map +0 -1
  162. package/dist/marketplace-config-znEu4L0K.d.ts +0 -60
  163. package/src/react/hooks/useCurrencyOptions.tsx +0 -16
  164. package/src/types/marketplace-config.ts +0 -67
  165. /package/dist/{chunk-MKGSGTQC.js.map → chunk-Y7YTLAO2.js.map} +0 -0
@@ -0,0 +1,181 @@
1
+ import { describe, expect, it, vi, beforeEach } from 'vitest';
2
+ import { useGenerateSellTransaction } from '../useGenerateSellTransaction';
3
+ import { renderHook, waitFor } from '../../_internal/test-utils';
4
+ import { zeroAddress } from 'viem';
5
+ import { http, HttpResponse } from 'msw';
6
+ import {
7
+ mockSteps,
8
+ mockMarketplaceEndpoint,
9
+ } from '../../_internal/api/__mocks__/marketplace.msw';
10
+ import { server } from '../../_internal/test/setup';
11
+ import {
12
+ ContractType,
13
+ OrderbookKind,
14
+ MarketplaceKind,
15
+ } from '../../_internal/api/marketplace.gen';
16
+ import { useConfig } from '../useConfig';
17
+ import type { SdkConfig } from '../../../types';
18
+
19
+ // Mock useConfig hook
20
+ vi.mock('../useConfig');
21
+
22
+ describe('useGenerateSellTransaction', () => {
23
+ const mockOnSuccess = vi.fn();
24
+
25
+ const mockConfig: SdkConfig = {
26
+ projectId: 'test-project',
27
+ projectAccessKey: 'test-access-key',
28
+ };
29
+
30
+ const mockOrderData = {
31
+ orderId: '1',
32
+ quantity: '1',
33
+ };
34
+
35
+ const mockTransactionProps = {
36
+ collectionAddress: zeroAddress,
37
+ seller: zeroAddress,
38
+ contractType: ContractType.ERC721,
39
+ orderbook: OrderbookKind.sequence_marketplace_v2,
40
+ marketplace: MarketplaceKind.sequence_marketplace_v2,
41
+ ordersData: [mockOrderData],
42
+ additionalFees: [],
43
+ };
44
+
45
+ const defaultArgs = {
46
+ chainId: '1' as const,
47
+ onSuccess: mockOnSuccess,
48
+ };
49
+
50
+ beforeEach(() => {
51
+ vi.clearAllMocks();
52
+
53
+ // Set up the mock implementation for useConfig
54
+ vi.mocked(useConfig).mockReturnValue(mockConfig);
55
+
56
+ // Mock default steps response
57
+ server.use(
58
+ http.post(mockMarketplaceEndpoint('GenerateSellTransaction'), () => {
59
+ return HttpResponse.json({
60
+ steps: mockSteps,
61
+ });
62
+ }),
63
+ );
64
+ });
65
+
66
+ it('should generate sell transaction successfully', async () => {
67
+ const { result } = renderHook(() =>
68
+ useGenerateSellTransaction(defaultArgs),
69
+ );
70
+
71
+ await result.current.generateSellTransactionAsync(mockTransactionProps);
72
+
73
+ expect(mockOnSuccess).toHaveBeenCalledWith(
74
+ mockSteps,
75
+ mockTransactionProps,
76
+ undefined,
77
+ );
78
+ });
79
+
80
+ it('should handle async generation with await', async () => {
81
+ const { result } = renderHook(() =>
82
+ useGenerateSellTransaction(defaultArgs),
83
+ );
84
+
85
+ const steps =
86
+ await result.current.generateSellTransactionAsync(mockTransactionProps);
87
+
88
+ expect(steps).toEqual(mockSteps);
89
+ });
90
+
91
+ it('should handle non-async generation with callback', async () => {
92
+ const { result } = renderHook(() =>
93
+ useGenerateSellTransaction(defaultArgs),
94
+ );
95
+
96
+ result.current.generateSellTransaction(mockTransactionProps);
97
+
98
+ await waitFor(() => {
99
+ expect(mockOnSuccess).toHaveBeenCalledWith(
100
+ mockSteps,
101
+ mockTransactionProps,
102
+ undefined,
103
+ );
104
+ });
105
+ });
106
+
107
+ it('should handle API errors', async () => {
108
+ // Mock error response
109
+ server.use(
110
+ http.post(mockMarketplaceEndpoint('GenerateSellTransaction'), () => {
111
+ return new HttpResponse(null, { status: 500 });
112
+ }),
113
+ );
114
+
115
+ const { result } = renderHook(() =>
116
+ useGenerateSellTransaction(defaultArgs),
117
+ );
118
+
119
+ await expect(
120
+ result.current.generateSellTransactionAsync(mockTransactionProps),
121
+ ).rejects.toThrow();
122
+
123
+ expect(mockOnSuccess).not.toHaveBeenCalled();
124
+ });
125
+
126
+ it('should use provided config from useConfig hook', async () => {
127
+ const customConfig: SdkConfig = {
128
+ projectId: 'custom-project',
129
+ projectAccessKey: 'custom-access-key',
130
+ };
131
+
132
+ let requestHeaders: Headers | undefined;
133
+
134
+ server.use(
135
+ http.post(
136
+ mockMarketplaceEndpoint('GenerateSellTransaction'),
137
+ async ({ request }) => {
138
+ requestHeaders = request.headers;
139
+ return HttpResponse.json({ steps: mockSteps });
140
+ },
141
+ ),
142
+ );
143
+
144
+ vi.mocked(useConfig).mockReturnValue(customConfig);
145
+
146
+ const { result } = renderHook(() =>
147
+ useGenerateSellTransaction(defaultArgs),
148
+ );
149
+ await result.current.generateSellTransactionAsync(mockTransactionProps);
150
+
151
+ expect(requestHeaders?.get('x-access-key')).toBe(
152
+ customConfig.projectAccessKey,
153
+ );
154
+ });
155
+
156
+ it('should handle invalid sell data', async () => {
157
+ const invalidOrderData = {
158
+ orderId: '1',
159
+ quantity: 'invalid-quantity',
160
+ };
161
+
162
+ server.use(
163
+ http.post(mockMarketplaceEndpoint('GenerateSellTransaction'), () => {
164
+ return new HttpResponse(null, { status: 400 });
165
+ }),
166
+ );
167
+
168
+ const { result } = renderHook(() =>
169
+ useGenerateSellTransaction(defaultArgs),
170
+ );
171
+
172
+ await expect(
173
+ result.current.generateSellTransactionAsync({
174
+ ...mockTransactionProps,
175
+ ordersData: [invalidOrderData],
176
+ }),
177
+ ).rejects.toThrow();
178
+
179
+ expect(mockOnSuccess).not.toHaveBeenCalled();
180
+ });
181
+ });
@@ -0,0 +1,118 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { useHighestOffer } from '../useHighestOffer';
3
+ import { renderHook, waitFor } from '../../_internal/test-utils';
4
+ import { zeroAddress } from 'viem';
5
+ import type { UseHighestOfferArgs } from '../useHighestOffer';
6
+ import { http, HttpResponse } from 'msw';
7
+ import { server } from '../../_internal/test/setup';
8
+ import {
9
+ mockOrder,
10
+ mockMarketplaceEndpoint,
11
+ } from '../../_internal/api/__mocks__/marketplace.msw';
12
+
13
+ describe('useHighestOffer', () => {
14
+ const defaultArgs: UseHighestOfferArgs = {
15
+ chainId: '1',
16
+ collectionAddress: zeroAddress,
17
+ tokenId: '1',
18
+ query: {},
19
+ };
20
+
21
+ it('should fetch highest offer data successfully', async () => {
22
+ const { result } = renderHook(() => useHighestOffer(defaultArgs));
23
+
24
+ // Initially loading
25
+ expect(result.current.isLoading).toBe(true);
26
+ expect(result.current.data).toBeUndefined();
27
+
28
+ // Wait for data to be loaded
29
+ await waitFor(() => {
30
+ expect(result.current.isLoading).toBe(false);
31
+ });
32
+
33
+ // Verify the data matches our mock
34
+ expect(result.current.data).toEqual({ order: mockOrder });
35
+ expect(result.current.error).toBeNull();
36
+ });
37
+
38
+ it('should handle error states', async () => {
39
+ // Override the handler for this test to return an error
40
+ server.use(
41
+ http.post(mockMarketplaceEndpoint('GetCollectibleHighestOffer'), () => {
42
+ return HttpResponse.json(
43
+ { error: { message: 'Failed to fetch highest offer' } },
44
+ { status: 500 },
45
+ );
46
+ }),
47
+ );
48
+
49
+ const { result } = renderHook(() => useHighestOffer(defaultArgs));
50
+
51
+ await waitFor(() => {
52
+ expect(result.current.isError).toBe(true);
53
+ });
54
+
55
+ expect(result.current.error).toBeDefined();
56
+ expect(result.current.data).toBeUndefined();
57
+ });
58
+
59
+ it('should refetch when args change', async () => {
60
+ const { result, rerender } = renderHook(() => useHighestOffer(defaultArgs));
61
+
62
+ // Wait for initial data
63
+ await waitFor(() => {
64
+ expect(result.current.isLoading).toBe(false);
65
+ });
66
+
67
+ // Change args and rerender
68
+ const newArgs = {
69
+ ...defaultArgs,
70
+ tokenId: '2',
71
+ collectionAddress:
72
+ '0x1234567890123456789012345678901234567890' as `0x${string}`,
73
+ };
74
+
75
+ rerender(() => useHighestOffer(newArgs));
76
+
77
+ // Wait for new data
78
+ await waitFor(() => {
79
+ expect(result.current.data).toBeDefined();
80
+ });
81
+
82
+ // Verify that the query was refetched with new args
83
+ expect(result.current.data).toEqual({ order: mockOrder });
84
+ expect(result.current.isSuccess).toBe(true);
85
+ });
86
+
87
+ it('should handle undefined query params', async () => {
88
+ const argsWithoutQuery: UseHighestOfferArgs = {
89
+ chainId: '1',
90
+ collectionAddress: zeroAddress,
91
+ tokenId: '1',
92
+ };
93
+
94
+ const { result } = renderHook(() => useHighestOffer(argsWithoutQuery));
95
+
96
+ await waitFor(() => {
97
+ expect(result.current.isLoading).toBe(false);
98
+ });
99
+
100
+ expect(result.current.data).toBeDefined();
101
+ expect(result.current.error).toBeNull();
102
+ });
103
+
104
+ it('should validate input parameters', async () => {
105
+ const invalidArgs = {
106
+ ...defaultArgs,
107
+ chainId: 'invalid-chain' as string, // Properly typed as string
108
+ };
109
+
110
+ const { result } = renderHook(() => useHighestOffer(invalidArgs));
111
+
112
+ await waitFor(() => {
113
+ expect(result.current.isError).toBe(true);
114
+ });
115
+
116
+ expect(result.current.error).toBeDefined();
117
+ });
118
+ });
@@ -0,0 +1,136 @@
1
+ import { waitFor } from '@testing-library/react';
2
+ import { describe, expect, it, beforeEach } from 'vitest';
3
+ import { useListBalances } from '../useListBalances';
4
+ import { mockTokenBalance } from '../../_internal/api/__mocks__/indexer.msw';
5
+ import { zeroAddress } from 'viem';
6
+ import { server } from '../../_internal/test/setup';
7
+ import { renderHook } from '../../_internal/test-utils';
8
+
9
+ describe('useListBalances', () => {
10
+ beforeEach(() => {
11
+ server.resetHandlers();
12
+ });
13
+
14
+ it('should fetch token balances successfully', async () => {
15
+ const { result } = renderHook(() =>
16
+ useListBalances({
17
+ chainId: 1,
18
+ accountAddress: zeroAddress,
19
+ query: {
20
+ enabled: true,
21
+ },
22
+ }),
23
+ );
24
+
25
+ await waitFor(() => {
26
+ expect(result.current.isSuccess).toBe(true);
27
+ });
28
+
29
+ expect(result.current.data?.pages[0].balances).toEqual([mockTokenBalance]);
30
+ });
31
+
32
+ it('should handle pagination correctly', async () => {
33
+ const { result } = renderHook(() =>
34
+ useListBalances({
35
+ chainId: 1,
36
+ accountAddress: zeroAddress,
37
+ query: {
38
+ enabled: true,
39
+ },
40
+ page: {
41
+ pageSize: 10,
42
+ },
43
+ }),
44
+ );
45
+
46
+ await waitFor(() => {
47
+ expect(result.current.isSuccess).toBe(true);
48
+ });
49
+
50
+ expect(result.current.data?.pages[0].page).toEqual({
51
+ page: 1,
52
+ pageSize: 10,
53
+ more: false,
54
+ });
55
+ });
56
+
57
+ it('should handle optional parameters', async () => {
58
+ const { result } = renderHook(() =>
59
+ useListBalances({
60
+ chainId: 1,
61
+ accountAddress: zeroAddress,
62
+ contractAddress: zeroAddress,
63
+ tokenId: '1',
64
+ includeMetadata: true,
65
+ includeCollectionTokens: true,
66
+ query: {
67
+ enabled: true,
68
+ },
69
+ }),
70
+ );
71
+
72
+ await waitFor(() => {
73
+ expect(result.current.isSuccess).toBe(true);
74
+ });
75
+
76
+ const balance = result.current.data?.pages[0].balances[0];
77
+ expect(balance?.contractInfo).toBeDefined();
78
+ expect(balance?.tokenMetadata).toBeDefined();
79
+ });
80
+
81
+ it('should handle disabled queries', async () => {
82
+ const { result } = renderHook(() =>
83
+ useListBalances({
84
+ chainId: 1,
85
+ accountAddress: zeroAddress,
86
+ query: {
87
+ enabled: false,
88
+ },
89
+ }),
90
+ );
91
+
92
+ expect(result.current.isLoading).toBe(false);
93
+ expect(result.current.data).toBeUndefined();
94
+ });
95
+
96
+ it('should handle metadata options', async () => {
97
+ const { result } = renderHook(() =>
98
+ useListBalances({
99
+ chainId: 1,
100
+ accountAddress: zeroAddress,
101
+ metadataOptions: {
102
+ verifiedOnly: true,
103
+ includeContracts: [zeroAddress],
104
+ },
105
+ query: {
106
+ enabled: true,
107
+ },
108
+ }),
109
+ );
110
+
111
+ await waitFor(() => {
112
+ expect(result.current.isSuccess).toBe(true);
113
+ });
114
+
115
+ const balance = result.current.data?.pages[0].balances[0];
116
+ expect(balance?.contractInfo?.extensions.verified).toBe(true);
117
+ });
118
+
119
+ it('should validate input parameters', async () => {
120
+ const invalidChainId = 'invalid' as number | string;
121
+
122
+ const { result } = renderHook(() =>
123
+ useListBalances({
124
+ chainId: invalidChainId,
125
+ accountAddress: zeroAddress,
126
+ query: {
127
+ enabled: true,
128
+ },
129
+ }),
130
+ );
131
+
132
+ await waitFor(() => {
133
+ expect(result.current.isError).toBe(true);
134
+ });
135
+ });
136
+ });
@@ -0,0 +1,200 @@
1
+ import { describe, expect, it, beforeEach } from 'vitest';
2
+ import { useListCollectibleActivities } from '../useListCollectibleActivities';
3
+ import { renderHook, waitFor } from '../../_internal/test-utils';
4
+ import { zeroAddress } from 'viem';
5
+ import type { UseListCollectibleActivitiesArgs } from '../useListCollectibleActivities';
6
+ import { http, HttpResponse } from 'msw';
7
+ import {
8
+ mockActivity,
9
+ mockMarketplaceEndpoint,
10
+ } from '../../_internal/api/__mocks__/marketplace.msw';
11
+ import { server } from '../../_internal/test/setup';
12
+
13
+ describe('useListCollectibleActivities', () => {
14
+ const defaultArgs: UseListCollectibleActivitiesArgs = {
15
+ chainId: '1',
16
+ collectionAddress: zeroAddress,
17
+ tokenId: '1',
18
+ query: {
19
+ enabled: true,
20
+ page: 1,
21
+ pageSize: 10,
22
+ },
23
+ };
24
+
25
+ // Reset handlers before each test
26
+ beforeEach(() => {
27
+ server.resetHandlers();
28
+ });
29
+
30
+ it('should fetch collectible activities successfully', async () => {
31
+ // Set up the default success handler
32
+ server.use(
33
+ http.post(mockMarketplaceEndpoint('ListCollectibleActivities'), () => {
34
+ return HttpResponse.json({
35
+ activities: [mockActivity],
36
+ page: { page: 1, pageSize: 10, more: false },
37
+ });
38
+ }),
39
+ );
40
+
41
+ const { result } = renderHook(() =>
42
+ useListCollectibleActivities(defaultArgs),
43
+ );
44
+
45
+ // Initially loading
46
+ expect(result.current.isLoading).toBe(true);
47
+ expect(result.current.data).toBeUndefined();
48
+
49
+ // Wait for data to be loaded
50
+ await waitFor(() => {
51
+ expect(result.current.isLoading).toBe(false);
52
+ });
53
+
54
+ // Verify the data matches our mock
55
+ expect(result.current.data).toEqual({
56
+ activities: [mockActivity],
57
+ page: { page: 1, pageSize: 10, more: false },
58
+ });
59
+ expect(result.current.error).toBeNull();
60
+ });
61
+
62
+ it('should handle error states', async () => {
63
+ // Override the handler for this test to return an error
64
+ server.use(
65
+ http.post(mockMarketplaceEndpoint('ListCollectibleActivities'), () => {
66
+ return HttpResponse.json(
67
+ { error: { message: 'Failed to fetch activities' } },
68
+ { status: 500 },
69
+ );
70
+ }),
71
+ );
72
+
73
+ const { result } = renderHook(() =>
74
+ useListCollectibleActivities(defaultArgs),
75
+ );
76
+
77
+ await waitFor(() => {
78
+ expect(result.current.isError).toBe(true);
79
+ });
80
+
81
+ expect(result.current.error).toBeDefined();
82
+ expect(result.current.data).toBeUndefined();
83
+ });
84
+
85
+ it('should refetch when args change', async () => {
86
+ // Set up the success handler
87
+ server.use(
88
+ http.post(mockMarketplaceEndpoint('ListCollectibleActivities'), () => {
89
+ return HttpResponse.json({
90
+ activities: [mockActivity],
91
+ page: { page: 1, pageSize: 10, more: false },
92
+ });
93
+ }),
94
+ );
95
+
96
+ let currentArgs = defaultArgs;
97
+ const { result, rerender } = renderHook(() =>
98
+ useListCollectibleActivities(currentArgs),
99
+ );
100
+
101
+ // Wait for initial data
102
+ await waitFor(() => {
103
+ expect(result.current.isLoading).toBe(false);
104
+ });
105
+
106
+ // Change args and rerender
107
+ currentArgs = {
108
+ chainId: '1',
109
+ tokenId: '2',
110
+ collectionAddress:
111
+ '0x1234567890123456789012345678901234567890' as `0x${string}`,
112
+ query: {
113
+ enabled: true,
114
+ page: 1,
115
+ pageSize: 10,
116
+ },
117
+ };
118
+
119
+ rerender();
120
+
121
+ // Wait for new data
122
+ await waitFor(() => {
123
+ expect(result.current.data).toBeDefined();
124
+ });
125
+
126
+ // Verify that the query was refetched with new args
127
+ expect(result.current.data).toBeDefined();
128
+ expect(result.current.isSuccess).toBe(true);
129
+ });
130
+
131
+ it('should handle disabled queries', async () => {
132
+ // Set up a mock handler to ensure no requests are made
133
+ let requestMade = false;
134
+ server.use(
135
+ http.post(mockMarketplaceEndpoint('ListCollectibleActivities'), () => {
136
+ requestMade = true;
137
+ return HttpResponse.json({
138
+ activities: [mockActivity],
139
+ page: { page: 1, pageSize: 10, more: false },
140
+ });
141
+ }),
142
+ );
143
+
144
+ // Create a disabled query args
145
+ const disabledArgs: UseListCollectibleActivitiesArgs = {
146
+ ...defaultArgs,
147
+ query: {
148
+ enabled: false,
149
+ page: 1,
150
+ pageSize: 10,
151
+ },
152
+ };
153
+
154
+ // Render hook with disabled query
155
+ const { result } = renderHook(() =>
156
+ useListCollectibleActivities(disabledArgs),
157
+ );
158
+
159
+ // For disabled queries, we expect no loading state and no data
160
+ expect(result.current.isLoading).toBe(false);
161
+ expect(result.current.data).toBeUndefined();
162
+ expect(result.current.error).toBeNull();
163
+ expect(requestMade).toBe(false);
164
+ });
165
+
166
+ it('should handle pagination correctly', async () => {
167
+ // Override the handler for this test to return paginated data
168
+ server.use(
169
+ http.post(mockMarketplaceEndpoint('ListCollectibleActivities'), () => {
170
+ return HttpResponse.json({
171
+ activities: [mockActivity],
172
+ page: { page: 2, pageSize: 20, more: false },
173
+ });
174
+ }),
175
+ );
176
+
177
+ const paginatedArgs: UseListCollectibleActivitiesArgs = {
178
+ ...defaultArgs,
179
+ query: {
180
+ enabled: true,
181
+ page: 2,
182
+ pageSize: 20,
183
+ },
184
+ };
185
+
186
+ const { result } = renderHook(() =>
187
+ useListCollectibleActivities(paginatedArgs),
188
+ );
189
+
190
+ await waitFor(() => {
191
+ expect(result.current.isLoading).toBe(false);
192
+ });
193
+
194
+ expect(result.current.data?.page).toEqual({
195
+ page: 2,
196
+ pageSize: 20,
197
+ more: false,
198
+ });
199
+ });
200
+ });