Skip to content

Commit

Permalink
fix: broken test, minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavkparti committed Apr 23, 2024
1 parent 76bdaf7 commit 4977138
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 111 deletions.
24 changes: 0 additions & 24 deletions src/api/trustRelationships.js

This file was deleted.

40 changes: 31 additions & 9 deletions src/api/trust_relationships.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import apiClient from '../utils/apiClient';
import { makeQueryString } from '../utils/formatting';

export const getTrustRelationships = async (token, {pagination, filter, sorting}) => {


export const getTrustRelationships = async (
token,
{ pagination, filter, sorting }
) => {
const { sort_by, order } = sorting;

try {
Expand All @@ -12,7 +13,7 @@ export const getTrustRelationships = async (token, {pagination, filter, sorting}
...pagination,
...where,
sort_by,
order
order,
};

const queryString = makeQueryString(trustRelationshipsFilter);
Expand All @@ -25,8 +26,30 @@ export const getTrustRelationships = async (token, {pagination, filter, sorting}
}
};

export const requestTrustRelationship = async (
token,
{ requestType, requestingWallet, targetWallet }
) => {
try {
const response = await apiClient
.setAuthHeader(token)
.post('/trust_relationships', {
trust_request_type: requestType,
requester_wallet: requestingWallet,
requestee_wallet: targetWallet,
});

return response.data;
} catch (error) {
console.error(error);
throw Error(
'An error occurred while requesting the trust relationship: ' +
error.response.data.message
);
}
};

export const acceptTrustRelationship = async ({id, token}) => {
export const acceptTrustRelationship = async ({ id, token }) => {
try {
const response = await apiClient
.setAuthHeader(token)
Expand All @@ -35,10 +58,9 @@ export const acceptTrustRelationship = async ({id, token}) => {
} catch (error) {
console.error(error);
}
}

};

export const declineTrustRelationship = async ({id, token}) => {
export const declineTrustRelationship = async ({ id, token }) => {
try {
const response = await apiClient
.setAuthHeader(token)
Expand All @@ -47,4 +69,4 @@ export const declineTrustRelationship = async ({id, token}) => {
} catch (error) {
console.error(error);
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import DialogActions from '@mui/material/DialogActions';
import SelectWallet from '../../SendTokens/SendTokensForm/SelectWallet';
import { ArrowDropDown } from '@mui/icons-material';
import AuthContext from '../../../store/auth-context';
import { requestTrustRelationship } from '../../../api/trustRelationships';
import { requestTrustRelationship } from '../../../api/trust_relationships';
import Message from '../../../components/UI/components/Message/Message';
import { useTrustRelationshipsContext } from '../../../store/TrustRelationshipsContext';

Expand Down
167 changes: 90 additions & 77 deletions src/pages/TrustRelationship/TrustRelationshipTable.test.js
Original file line number Diff line number Diff line change
@@ -1,83 +1,100 @@
import { render, screen, waitFor } from '@testing-library/react';
import { render, screen } from '@testing-library/react';
import TrustRelationshipTable from './TrustRelationshipTable';
import { TrustRelationshipsProvider } from '../../store/TrustRelationshipsContext';

import { ThemeProvider } from '@mui/material';
import theme from '../../components/UI/theme';
import { BrowserRouter as Router } from 'react-router-dom';
import AuthProvider from '../../store/AuthProvider';

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

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

const mockTableRows = [
{
"id": "08f64a56-4470-4774-9b7b-f218bb8c2302",
"actor_wallet_id": "c59a6d29-7256-43d6-ac5d-61673e1d29bb",
"target_wallet_id": "e009c2bf-e6ea-4614-ae68-cbef0d2aaf87",
"type": "send",
"originator_wallet_id": "c59a6d29-7256-43d6-ac5d-61673e1d29bb",
"request_type": "send",
"state": "requested",
"created_at": "2024-03-24T13:06:25.226Z",
"updated_at": "2024-03-24T13:06:25.226Z",
"active": true,
"originating_wallet": "testuser",
"actor_wallet": "testuser",
"target_wallet": "sam-testwallet1"
},
{
"id": "0e14c951-a646-4c27-b409-1ab444825caf",
"actor_wallet_id": "c59a6d29-7256-43d6-ac5d-61673e1d29bb",
"target_wallet_id": "03b4a0ef-da95-4517-b4a0-0b61b5bec6ed",
"type": "send",
"originator_wallet_id": "c59a6d29-7256-43d6-ac5d-61673e1d29bb",
"request_type": "send",
"state": "requested",
"created_at": "2024-03-24T13:11:09.707Z",
"updated_at": "2024-03-24T13:11:09.707Z",
"active": true,
"originating_wallet": "testuser",
"actor_wallet": "testuser",
"target_wallet": "sam-testwallet4"
},
{
"id": "180f1bda-b143-451f-ab6a-65ea6822b86e",
"actor_wallet_id": "c59a6d29-7256-43d6-ac5d-61673e1d29bb",
"target_wallet_id": "86d38055-cf79-4362-bab8-577bcf98c1dd",
"type": "manage",
"originator_wallet_id": "c59a6d29-7256-43d6-ac5d-61673e1d29bb",
"request_type": "manage",
"state": "trusted",
"created_at": "2024-03-24T13:08:58.200Z",
"updated_at": "2024-03-24T13:08:58.200Z",
"active": true,
"originating_wallet": "testuser",
"actor_wallet": "testuser",
"target_wallet": "samwel"
},
{
"id": "261a2ba2-5dac-4ba7-8523-fe79b5600d5b",
"actor_wallet_id": "c59a6d29-7256-43d6-ac5d-61673e1d29bb",
"target_wallet_id": "dc937897-f5a0-411d-8fce-e6c570870865",
"type": "manage",
"originator_wallet_id": "c59a6d29-7256-43d6-ac5d-61673e1d29bb",
"request_type": "manage",
"state": "trusted",
"created_at": "2023-12-12T12:55:43.364Z",
"updated_at": "2023-12-12T12:55:43.364Z",
"active": true,
"originating_wallet": "testuser",
"actor_wallet": "testuser",
"target_wallet": "wallet12"
}
id: '08f64a56-4470-4774-9b7b-f218bb8c2302',
actor_wallet_id: 'c59a6d29-7256-43d6-ac5d-61673e1d29bb',
target_wallet_id: 'e009c2bf-e6ea-4614-ae68-cbef0d2aaf87',
type: 'send',
originator_wallet_id: 'c59a6d29-7256-43d6-ac5d-61673e1d29bb',
request_type: 'send',
state: 'requested',
created_at: '2024-03-24T13:06:25.226Z',
updated_at: '2024-03-24T13:06:25.226Z',
active: true,
originating_wallet: 'testuser',
actor_wallet: 'testuser',
target_wallet: 'sam-testwallet1',
},
{
id: '0e14c951-a646-4c27-b409-1ab444825caf',
actor_wallet_id: 'c59a6d29-7256-43d6-ac5d-61673e1d29bb',
target_wallet_id: '03b4a0ef-da95-4517-b4a0-0b61b5bec6ed',
type: 'send',
originator_wallet_id: 'c59a6d29-7256-43d6-ac5d-61673e1d29bb',
request_type: 'send',
state: 'requested',
created_at: '2024-03-24T13:11:09.707Z',
updated_at: '2024-03-24T13:11:09.707Z',
active: true,
originating_wallet: 'testuser',
actor_wallet: 'testuser',
target_wallet: 'sam-testwallet4',
},
{
id: '180f1bda-b143-451f-ab6a-65ea6822b86e',
actor_wallet_id: 'c59a6d29-7256-43d6-ac5d-61673e1d29bb',
target_wallet_id: '86d38055-cf79-4362-bab8-577bcf98c1dd',
type: 'manage',
originator_wallet_id: 'c59a6d29-7256-43d6-ac5d-61673e1d29bb',
request_type: 'manage',
state: 'trusted',
created_at: '2024-03-24T13:08:58.200Z',
updated_at: '2024-03-24T13:08:58.200Z',
active: true,
originating_wallet: 'testuser',
actor_wallet: 'testuser',
target_wallet: 'samwel',
},
{
id: '261a2ba2-5dac-4ba7-8523-fe79b5600d5b',
actor_wallet_id: 'c59a6d29-7256-43d6-ac5d-61673e1d29bb',
target_wallet_id: 'dc937897-f5a0-411d-8fce-e6c570870865',
type: 'manage',
originator_wallet_id: 'c59a6d29-7256-43d6-ac5d-61673e1d29bb',
request_type: 'manage',
state: 'trusted',
created_at: '2023-12-12T12:55:43.364Z',
updated_at: '2023-12-12T12:55:43.364Z',
active: true,
originating_wallet: 'testuser',
actor_wallet: 'testuser',
target_wallet: 'wallet12',
},
];



describe('TrustRelationship Table', () => {

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

describe('TrustRelationship Table', () => {
test('renders correctly', async () => {
render(
<TestWrapper>
Expand All @@ -86,32 +103,28 @@ const TestWrapper = (props) => {
tableRows={mockTableRows}
totalRowCount={mockTableRows.length}
/>
</TestWrapper>,
</TestWrapper>
);

expect(await screen.findByTestId('table-pagination')).toBeInTheDocument();
expect(
await screen.findByTestId('trust-relationships-table'),
await screen.findByTestId('trust-relationships-table')
).toBeInTheDocument();
expect(await screen.findByTestId('date-range-filter')).toBeInTheDocument();
expect(
await screen.findByTestId('state-select-filter'),
await screen.findByTestId('state-select-filter')
).toBeInTheDocument();
expect(await screen.findByTestId('reset-filters')).toBeInTheDocument();



expect(await screen.findByTestId('table-pagination')).toBeInTheDocument();
expect(
screen.getByRole('button', { name: /Rows per page/ }),
screen.getByRole('button', { name: /Rows per page/ })
).toBeInTheDocument();
expect(
screen.getByRole('button', { name: 'Go to previous page' }),
screen.getByRole('button', { name: 'Go to previous page' })
).toBeInTheDocument();
expect(
screen.getByRole('button', { name: 'Go to next page' }),
screen.getByRole('button', { name: 'Go to next page' })
).toBeInTheDocument();
});
});


0 comments on commit 4977138

Please sign in to comment.