Skip to content

Commit

Permalink
feat: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
OlhaD committed Dec 31, 2023
1 parent 1e19080 commit 7636ffa
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 250 deletions.
4 changes: 2 additions & 2 deletions src/components/Routes/ClientRoutes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ describe('ClientRoutes component', () => {
// screen.getByRole('');

//Logo, Home, Send Tokens, and My Trasnfers for now
expect(await screen.findAllByRole('link')).toHaveLength(4);
expect(screen.getAllByRole('button')).toHaveLength(4);
expect(await screen.findAllByRole('link')).toHaveLength(5);
expect(screen.getAllByRole('button')).toHaveLength(5);

expect(screen.getByText(/Home/)).toBeInTheDocument();
expect(screen.getByText(/Send Tokens/)).toBeInTheDocument();
Expand Down
14 changes: 7 additions & 7 deletions src/components/UI/components/Table/Table.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Loader } from '../Loader/Loader';
* @name Header
* @description Renders the table header (title, filters) for the table
* @param {string} tableTitle Name of the table to be displayed
* @param {function} getStatusColor returns color corresponding to wallets state value
* @param {function} getStatusColor returns color corresponding to state value
*
* @returns {JSX.Element} - Table header component
*/
Expand All @@ -35,7 +35,7 @@ const Header = ({ tableTitle }) => {

/**@function
* @name Body
* @description Renders the table body (table rows) for the wallets table
* @description Renders the table body (table rows) for the table
* @param tableColumns
* @param tableRows
* @return {JSX.Element} - Table body component
Expand Down Expand Up @@ -130,13 +130,13 @@ const OverflownCell = ({ cellValue, cellColor, children }) => {
};

/**@function
* @name WalletsTable
* @description Renders the wallets table
* @name Table
* @description Renders the table
* @param {string} tableTitle Name of the table to be displayed
* @param {object} tableColumns Array of table column objects to be displayed
* @param {object} tableRows Array of table row objects to be displayed
*
* @returns {JSX.Element} - wallets table component
* @returns {JSX.Element} - table component
*/
const Table = ({
tableTitle,
Expand Down Expand Up @@ -175,8 +175,8 @@ const Table = ({
<MuiTable
stickyHeader
sx={{ minWidth: 650 }}
aria-label="wallets table"
data-testid="wallets-table"
aria-label="table"
data-testid="table"
>
<TableHead>
<TableRow>
Expand Down
75 changes: 75 additions & 0 deletions src/components/UI/components/Table/Table.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import Table from './Table';

// Mock the necessary props and data
const tableTitle = 'Test Table';
const tableColumns = [
{ description: 'Column 1', name: 'column1' },
{ description: 'Column 2', name: 'column2' },
];
const tableRows = [
{ column1: 'Row 1 Data 1', column2: 'Row 1 Data 2' },
{ column1: 'Row 2 Data 1', column2: 'Row 2 Data 2' },
];
const totalRowCount = 2;
const pagination = { limit: 10, offset: 0 };
const isLoading = false;

describe('Wallets List page', function () {
it('renders the table component with correct title', () => {
render(
<Table
tableTitle={tableTitle}
tableColumns={tableColumns}
tableRows={tableRows}
totalRowCount={totalRowCount}
pagination={pagination}
setPagination={() => {}}
isLoading={isLoading}
/>
);

// Check if the table title is present in the rendered component
expect(screen.getByText(tableTitle)).toBeInTheDocument();
});

it('renders the table headers correctly', () => {
render(
<Table
tableTitle={tableTitle}
tableColumns={tableColumns}
tableRows={tableRows}
totalRowCount={totalRowCount}
pagination={pagination}
setPagination={() => {}}
isLoading={isLoading}
/>
);

// Check if all table column headers are present
tableColumns.forEach((column) => {
expect(screen.getByText(column.description)).toBeInTheDocument();
});
});

it('renders table rows correctly', () => {
render(
<Table
tableTitle={tableTitle}
tableColumns={tableColumns}
tableRows={tableRows}
totalRowCount={totalRowCount}
pagination={pagination}
setPagination={() => {}}
isLoading={isLoading}
/>
);

// Check if all table data rows are present
tableRows.forEach((row) => {
expect(screen.getByText(row.column1)).toBeInTheDocument();
expect(screen.getByText(row.column2)).toBeInTheDocument();
});
});
});
15 changes: 3 additions & 12 deletions src/pages/ListWallets/ListWallets.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React, { useContext, useEffect, useState } from 'react';
import { Grid } from '@mui/material';
// import WalletsTable from './WalletsTable';
import Message from '../../components/UI/components/Message/Message';
import { getWallets } from '../../api/wallets';
import AuthContext from '../../store/auth-context';
import { useWalletsContext } from '../../store/WalletsContext';
import Table from '../../components/UI/components/Table/Table';
import { Container } from './ListWallets.style';

/**@function
* @name ListWallets
Expand Down Expand Up @@ -54,16 +54,7 @@ const ListWallets = () => {
}, [pagination]);

return (
<div
style={{
marginTop: '5rem',
marginLeft: '1rem',
display: 'flex',
flexDirection: 'column',
marginRight: '1rem',
width: '100%',
}}
>
<Container>
{message && <Message message={message} onClose={() => setMessage('')} />}
<Grid container direction="column" sx={{ flexGrow: '1' }}>
<Table
Expand All @@ -76,7 +67,7 @@ const ListWallets = () => {
isLoading={isLoading}
/>
</Grid>
</div>
</Container>
);
};

Expand Down
10 changes: 10 additions & 0 deletions src/pages/ListWallets/ListWallets.style.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import styled from '@emotion/styled';

export const Container = styled('div')({
marginTop: '5rem',
marginLeft: '1rem',
display: 'flex',
flexDirection: 'column',
marginRight: '1rem',
width: '100%',
});
171 changes: 171 additions & 0 deletions src/pages/ListWallets/ListWallets.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import { render, screen, waitFor } from '@testing-library/react';
import { ThemeProvider } from '@mui/material';
import theme from '../../components/UI/theme';
import AuthProvider from '../../store/AuthProvider';
import { BrowserRouter as Router } from 'react-router-dom';
import { WalletsProvider } from '../../store/WalletsContext';
import ListWallets from './ListWallets';
import { getWallets } from '../../api/wallets';

jest.mock('../../api/wallets', () => ({
getWallets: jest.fn(),
}));

jest.mock('react-secure-storage', () => {
return {
getItem: jest.fn(),
setItem: jest.fn(),
};
});

const mockWalletsData = {
total: 12,
query: {
limit: 1000,
offset: 0,
sort_by: 'created_at',
order: 'desc',
},
wallets: [
{
id: 'dc937897-f5a0-411d-8fce-e6c570870865',
name: 'wallet12',
about: null,
logo_url: null,
created_at: '2023-12-12T12:55:43.364Z',
tokens_in_wallet: 0,
},
{
id: '8b80c4cd-4609-4882-ad15-862000a5786e',
name: 'wallet11',
about: null,
logo_url: null,
created_at: '2023-12-12T12:55:40.836Z',
tokens_in_wallet: 0,
},
{
id: 'eee50580-8460-483f-856e-bcbce8c8fb31',
name: 'wallet10',
about: null,
logo_url: null,
created_at: '2023-12-12T12:55:38.607Z',
tokens_in_wallet: 0,
},
{
id: '9c6476f9-bfdd-454a-9fee-e159dbd4bc22',
name: 'wallet9',
about: null,
logo_url: null,
created_at: '2023-12-12T12:55:34.850Z',
tokens_in_wallet: 0,
},
{
id: '5758358e-eea3-480e-bbe5-ea329130068a',
name: 'wallet8',
about: null,
logo_url: null,
created_at: '2023-12-12T12:55:31.582Z',
tokens_in_wallet: 0,
},
{
id: '84872f38-6cc3-4f60-ad91-2b616337893c',
name: 'wallet7',
about: null,
logo_url: null,
created_at: '2023-12-12T12:55:28.094Z',
tokens_in_wallet: 0,
},
{
id: '89c5310c-5baf-49c1-94d0-ae57a217801a',
name: 'wallet6',
about: null,
logo_url: null,
created_at: '2023-12-12T12:55:24.412Z',
tokens_in_wallet: 0,
},
{
id: '7aeca165-f999-440d-9821-d39bf3078ec2',
name: 'wallet5',
about: null,
logo_url: null,
created_at: '2023-12-12T12:55:21.375Z',
tokens_in_wallet: 0,
},
{
id: 'e0d8666d-511b-46bc-a1a9-ccca3e216a3b',
name: 'wallet4',
about: null,
logo_url: null,
created_at: '2023-12-12T12:55:18.919Z',
tokens_in_wallet: 0,
},
{
id: '86cb813d-8107-4f3d-86c7-22570cef17fc',
name: 'wallet3',
about: null,
logo_url: null,
created_at: '2023-12-12T12:55:05.789Z',
tokens_in_wallet: 0,
},
{
id: '30d52732-ea3d-4630-b0a6-7b2eed0d354a',
name: 'wallet2',
about: null,
logo_url: null,
created_at: '2023-12-12T12:55:02.851Z',
tokens_in_wallet: 0,
},
{
id: '5bc12fbe-244e-45f2-a521-0b3a8efcaa08',
name: 'wallet1',
about: null,
logo_url: null,
created_at: '2023-12-12T12:54:56.855Z',
tokens_in_wallet: 0,
},
],
};

const TestWrapper = (props) => {
return (
<ThemeProvider theme={theme}>
<Router>
<AuthProvider>
<WalletsProvider>{props.children}</WalletsProvider>
</AuthProvider>
</Router>
</ThemeProvider>
);
};

describe('Wallets List page', function () {
beforeEach(() => {
localStorage.setItem(
'wallet',
JSON.stringify({
id: '9d6c674f-ae62-4fab-8d14-ae5de9f14ab8',
wallet: 'test wallet',
})
);
});

afterEach(() => {
localStorage.removeItem('wallet');
});

it('renders table pagination correctly', async () => {
getWallets.mockResolvedValueOnce(mockWalletsData);
render(
<TestWrapper>
<ListWallets />
</TestWrapper>
);
expect(await screen.findByTestId('table-pagination')).toBeInTheDocument();
expect(await screen.findByTestId('table')).toBeInTheDocument();
await waitFor(() => {
expect(screen.getAllByRole('row')).toHaveLength(
mockWalletsData.total + 1
);
});
});
});
Loading

0 comments on commit 7636ffa

Please sign in to comment.