Skip to content

Commit

Permalink
test: chain page testing
Browse files Browse the repository at this point in the history
  • Loading branch information
0xtiti committed Sep 3, 2024
1 parent 619f701 commit e65f6b0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 43 deletions.
14 changes: 5 additions & 9 deletions cypress/e2e/navigation.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ describe('Navigation tests', () => {
cy.getByTestId('chain-row').should('be.visible').click();
cy.url().should('include', '/324');

// Wait for the mock data to be loaded
// cy.wait('@getChainData');
// cy.getByTestId('chain-id').should('be.visible').and('contain', '324');
cy.getByTestId('chain-id').should('be.visible').and('contain', '324');

// cy.getByTestId('home-breadcrumb').click();
// cy.url().should('eq', 'http://localhost:5173/');
// cy.getByTestId('search-bar').find('input').should('have.value', '');
cy.getByTestId('home-breadcrumb').click();
cy.url().should('eq', 'http://localhost:5173/');
cy.getByTestId('search-bar').find('input').should('have.value', '');
});

it('should navigate to tokens page, on all tokens button click', () => {
Expand All @@ -46,8 +44,6 @@ describe('Navigation tests', () => {
cy.getByTestId('chain-row').first().click();
cy.url().should('include', '/324');

// Wait for the mock data to be loaded
// cy.wait('@getChainData');
// cy.getByTestId('chain-id').should('be.visible').and('contain', '324');
cy.getByTestId('chain-id').should('be.visible').and('contain', '324');
});
});
51 changes: 17 additions & 34 deletions src/pages/[chain]/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useEffect } from 'react';
import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
import { GetStaticProps, GetStaticPaths, GetStaticPropsContext, InferGetStaticPropsType } from 'next';
import { useTranslation } from 'next-i18next';

import { EcosystemChainData } from '~/types';
import { CustomHead } from '~/components';
Expand All @@ -13,23 +12,18 @@ import { getConfig } from '~/config';
const { DEFAULT_LANG, SUPPORTED_LANGUAGES } = getConfig();

interface ChainProps {
chain: EcosystemChainData | null;
chain: EcosystemChainData;
}

const Chain = ({ chain }: InferGetStaticPropsType<typeof getStaticProps>) => {
const { setSelectedChainId, refetchChainData } = useData();
const { t } = useTranslation();

useEffect(() => {
if (chain?.chainId) {
if (chain.chainId) {
setSelectedChainId(chain.chainId);
}
}, [chain?.chainId, setSelectedChainId, refetchChainData]);

if (!chain) {
return <div>{t('ERROR.errorFetchingData')}</div>;
}

return (
<>
<CustomHead title={chain?.chainId} />
Expand All @@ -39,38 +33,27 @@ const Chain = ({ chain }: InferGetStaticPropsType<typeof getStaticProps>) => {
};

export const getStaticPaths: GetStaticPaths = async () => {
let paths = [];

try {
const ecosystemData = await fetchEcosystemData();
const chains = ecosystemData.zkChains;

paths = SUPPORTED_LANGUAGES.flatMap((locale) =>
chains.map((chain: EcosystemChainData) => ({
params: { chain: chain.chainId.toString() },
locale,
})),
);
} catch (error) {
console.error('Failed to fetch ecosystem data:', error);
}
const ecosystemData = await fetchEcosystemData();
const chains = ecosystemData.zkChains;

const paths = SUPPORTED_LANGUAGES.flatMap((locale) =>
chains.map((chain: EcosystemChainData) => ({
params: { chain: chain.chainId.toString() },
locale,
})),
);

return { paths, fallback: true };
};

export const getStaticProps: GetStaticProps<ChainProps> = async ({ params, locale }: GetStaticPropsContext) => {
const chain: EcosystemChainData | null = null;
const ecosystemData = await fetchEcosystemData();
const chains = ecosystemData.zkChains;
const chainId = params?.chain;
const chain = chains.find((chain: EcosystemChainData) => chain.chainId === chainId);

try {
const ecosystemData = await fetchEcosystemData();
const chains = ecosystemData.zkChains;
const chainId = params?.chain;
const chain = chains.find((chain: EcosystemChainData) => chain.chainId === chainId);
if (!chain) {
return { notFound: true };
}
} catch (error) {
console.error('Failed to fetch ecosystem data:', error);
if (!chain) {
return { notFound: true };
}

const i18Config = await serverSideTranslations(locale || DEFAULT_LANG, ['common'], null, SUPPORTED_LANGUAGES);
Expand Down

0 comments on commit e65f6b0

Please sign in to comment.