Skip to content

Commit

Permalink
replace ref with state to fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
maxaleks committed Mar 12, 2024
1 parent 9a47831 commit 5524c8d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
11 changes: 6 additions & 5 deletions ui/marketplace/useMarketplaceApps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,16 @@ export default function useMarketplaceApps(
const apiFetch = useApiFetch();

// Update favorite apps only when selectedCategoryId changes to avoid sortApps to be called on each favorite app click
const lastFavoriteAppsRef: React.MutableRefObject<Array<string> | undefined> = React.useRef();
const [ snapshotFavoriteApps, setSnapshotFavoriteApps ] = React.useState<Array<string> | undefined>();

React.useEffect(() => {
if (isFavoriteAppsLoaded) {
lastFavoriteAppsRef.current = favoriteApps;
setSnapshotFavoriteApps(favoriteApps);
}
}, [ selectedCategoryId, isFavoriteAppsLoaded ]); // eslint-disable-line react-hooks/exhaustive-deps

const { isPlaceholderData, isError, error, data } = useQuery<unknown, ResourceError<unknown>, Array<MarketplaceAppOverview>>({
queryKey: [ 'marketplace-dapps', lastFavoriteAppsRef.current, favoriteApps ],
queryKey: [ 'marketplace-dapps', snapshotFavoriteApps, favoriteApps ],
queryFn: async() => {
if (!feature.isEnabled) {
return [];
Expand All @@ -75,10 +76,10 @@ export default function useMarketplaceApps(
return apiFetch('marketplace_dapps', { pathParams: { chainId: config.chain.id } });
}
},
select: (data) => sortApps(data as Array<MarketplaceAppOverview>, lastFavoriteAppsRef.current || []),
select: (data) => sortApps(data as Array<MarketplaceAppOverview>, snapshotFavoriteApps || []),
placeholderData: feature.isEnabled ? Array(9).fill(MARKETPLACE_APP) : undefined,
staleTime: Infinity,
enabled: feature.isEnabled && (!favoriteApps || Boolean(lastFavoriteAppsRef.current)),
enabled: feature.isEnabled && (!favoriteApps || Boolean(snapshotFavoriteApps)),
});

const displayedApps = React.useMemo(() => {
Expand Down
2 changes: 1 addition & 1 deletion ui/pages/Marketplace.pw.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const test = base.extend({
]) as any,
});

test('base view +@dark-mode', async({ mount, page }) => {
test('base view +@mobile +@dark-mode', async({ mount, page }) => {
await page.route(MARKETPLACE_CONFIG_URL, (route) => route.fulfill({
status: 200,
body: JSON.stringify(appsMock),
Expand Down

0 comments on commit 5524c8d

Please sign in to comment.