Skip to content

Commit

Permalink
Add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aminick committed Aug 18, 2022
1 parent 43ef767 commit 74c08f3
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 36 deletions.
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ module.exports = {
// Handle module aliases
'^@/(.*)$': '<rootDir>/src/$1',
},
roots: ['<rootDir>'],
modulePaths: ['<rootDir>'],
moduleDirectories: ['node_modules', 'src'],
transform: {
// Use babel-jest to transpile tests with the next/babel preset
// https://jestjs.io/docs/configuration#transform-objectstring-pathtotransformer--pathtotransformer-object
Expand Down
46 changes: 11 additions & 35 deletions pages/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import styled from 'styled-components';
import { Spacer } from '@/components/layout/Spacer';
import { SearchAddressBar } from '@/components/SearchAddressBar/SearchAddressBar';
import AddressCard, { AddressStatus } from '@/pages/Search/AddressCard';
import { getChecksumByTicker } from '@/utils/validators/checksum';
import { getLocateAddress } from '@/api';
import { getPropsFromLocateAddress } from '@/pages/Search/utils';

export const TickerName = styled.span`
color: var(--text-tertiary);
Expand Down Expand Up @@ -72,39 +71,16 @@ const Search = ({
export const getServerSideProps: GetServerSideProps = async (context) => {
const searchString = context.query.search as string | undefined;

let addressStatus: AddressStatus = 'not-found';
let dashboards: string[] = [];
let addressType: string | undefined = undefined;
const { dashboards, addressStatus, isAddressValid } =
await getPropsFromLocateAddress(searchString);

if (searchString) {
if (getChecksumByTicker('eth')(searchString)) addressType = 'eth';
if (getChecksumByTicker('xch')(searchString)) addressType = 'xch';

const result = await getLocateAddress(searchString);

const { all } = result;

// TODO: this piece of code has to be tested
const isPending = result.pendingStats === true;
const isMining = !isPending && result.result !== null;

if (isPending) {
if (addressType === 'eth') dashboards = ['eth', 'etc'];
if (addressType === 'xch') dashboards = ['xch'];
addressStatus = 'pending';
} else if (isMining) {
dashboards = all;
addressStatus = 'mining';
}

if (dashboards.length === 1) {
return {
redirect: {
destination: `/miner/${all[0]}/${searchString}`,
permanent: false,
},
};
}
if (dashboards.length === 1) {
return {
redirect: {
destination: `/miner/${dashboards[0]}/${searchString}`,
permanent: false,
},
};
}

return {
Expand All @@ -118,7 +94,7 @@ export const getServerSideProps: GetServerSideProps = async (context) => {
address: searchString,
dashboards,
status: addressStatus,
isAddressValid: addressType !== undefined,
isAddressValid,
},
};
};
Expand Down
2 changes: 1 addition & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fetchApi } from '@/utils/fetchApi';

type LocateAddressResponse = {
export type LocateAddressResponse = {
all: string[];
error: string | null;
pendingStats: boolean;
Expand Down
127 changes: 127 additions & 0 deletions src/pages/Search/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { getPropsFromLocateAddress } from './utils';
const mockGetLocateAddress = jest.fn();

jest.mock('@/api', () => {
return {
getLocateAddress: () => mockGetLocateAddress(),
};
});

describe('getPropsFromLocateAddress', () => {
it('handles correctly for incorrect address format', async () => {
mockGetLocateAddress.mockResolvedValue({
pendingStats: false,
result: null,
all: [],
});

const { dashboards, addressStatus, isAddressValid } =
await getPropsFromLocateAddress('0x0');

expect(dashboards).toEqual([]);
expect(addressStatus).toEqual('not-found');
expect(isAddressValid).toEqual(false);
});

it('handles correctly for wallet mining only ethereum', async () => {
mockGetLocateAddress.mockResolvedValue({
pendingStats: false,
result: 'eth',
all: ['eth'],
});

const { dashboards, addressStatus, isAddressValid } =
await getPropsFromLocateAddress(
'0xb794f5ea0ba39494ce839613fffba74279579268'
);

expect(dashboards).toEqual(['eth']);
expect(addressStatus).toEqual('mining');
expect(isAddressValid).toEqual(true);
});

it('handles correctly for wallet mining only chia', async () => {
mockGetLocateAddress.mockResolvedValue({
pendingStats: false,
result: 'xch',
all: ['xch'],
});

const { dashboards, addressStatus, isAddressValid } =
await getPropsFromLocateAddress(
'xch1ch36xj8vtc2yr72f95u7nr36ykdtfsz0l2cannpskr6n46m6m2zsh8husq'
);

expect(dashboards).toEqual(['xch']);
expect(addressStatus).toEqual('mining');
expect(isAddressValid).toEqual(true);
});

it('handles correctly for wallet mining only etc', async () => {
mockGetLocateAddress.mockResolvedValue({
pendingStats: false,
result: 'etc',
all: ['etc'],
});

const { dashboards, addressStatus, isAddressValid } =
await getPropsFromLocateAddress(
'xch1ch36xj8vtc2yr72f95u7nr36ykdtfsz0l2cannpskr6n46m6m2zsh8husq'
);

expect(dashboards).toEqual(['etc']);
expect(addressStatus).toEqual('mining');
expect(isAddressValid).toEqual(true);
});

it('handles correctly for wallet mining both eth and etc', async () => {
mockGetLocateAddress.mockResolvedValue({
pendingStats: false,
result: 'eth',
all: ['eth', 'etc'],
});

const { dashboards, addressStatus, isAddressValid } =
await getPropsFromLocateAddress(
'0xb794f5ea0ba39494ce839613fffba74279579268'
);

expect(dashboards).toEqual(['eth', 'etc']);
expect(addressStatus).toEqual('mining');
expect(isAddressValid).toEqual(true);
});

it('handles correctly for pending eth wallet ', async () => {
mockGetLocateAddress.mockResolvedValue({
pendingStats: true,
result: null,
all: [],
});

const { dashboards, addressStatus, isAddressValid } =
await getPropsFromLocateAddress(
'0xb794f5ea0ba39494ce839613fffba74279579268'
);

expect(dashboards).toEqual(['eth', 'etc']);
expect(addressStatus).toEqual('pending');
expect(isAddressValid).toEqual(true);
});

it('handles correctly for pending xch wallet ', async () => {
mockGetLocateAddress.mockResolvedValue({
pendingStats: true,
result: null,
all: [],
});

const { dashboards, addressStatus, isAddressValid } =
await getPropsFromLocateAddress(
'xch1ch36xj8vtc2yr72f95u7nr36ykdtfsz0l2cannpskr6n46m6m2zsh8husq'
);

expect(dashboards).toEqual(['xch']);
expect(addressStatus).toEqual('pending');
expect(isAddressValid).toEqual(true);
});
});
36 changes: 36 additions & 0 deletions src/pages/Search/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { getLocateAddress } from '@/api';
import { getChecksumByTicker } from '@/utils/validators/checksum';
import { AddressStatus } from '@/pages/Search/AddressCard';

export const getPropsFromLocateAddress = async (
address: string | undefined
) => {
let addressStatus: AddressStatus = 'not-found';
let dashboards: string[] = [];
let addressType: string | undefined = undefined;

if (address) {
const result = await getLocateAddress(address);

if (getChecksumByTicker('eth')(address)) addressType = 'eth';
if (getChecksumByTicker('xch')(address)) addressType = 'xch';

const isPending = result.pendingStats === true;
const isMining = !isPending && result.result !== null;

if (isPending) {
if (addressType === 'eth') dashboards = ['eth', 'etc'];
if (addressType === 'xch') dashboards = ['xch'];
addressStatus = 'pending';
} else if (isMining) {
dashboards = result.all;
addressStatus = 'mining';
}
}

return {
dashboards,
addressStatus,
isAddressValid: addressType !== undefined,
};
};
81 changes: 81 additions & 0 deletions src/rdx/addressSearch/addressSearch.reducer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { reducer } from './addressSearch.reducer';
import { addressSearchSet } from './addressSearch.actions';

describe('addressSearch reducer should handle items being added to the list', () => {
it('should handle single item', () => {
expect(
reducer(
undefined,
addressSearchSet({
address: 'address_0',
coin: 'eth',
})
)
).toEqual([
{
address: 'address_0',
coin: 'eth',
},
]);
});

it('should handle multiple items', () => {
expect(
reducer(
[
{
address: 'address_0',
coin: 'eth',
},
{
address: 'address_1',
coin: 'eth',
},
],
addressSearchSet({
address: 'address_1',
coin: 'eth',
})
)
).toEqual([
{
address: 'address_1',
coin: 'eth',
},
{
address: 'address_0',
coin: 'eth',
},
]);
});

it('should handle complex item', () => {
expect(
reducer(
[
{
address: 'address_0',
coin: 'eth',
},
{
address: 'address_1',
coin: null,
},
],
addressSearchSet({
address: 'address_1',
coin: ['eth', 'etc'],
})
)
).toEqual([
{
address: 'address_1',
coin: ['eth', 'etc'],
},
{
address: 'address_0',
coin: 'eth',
},
]);
});
});

0 comments on commit 74c08f3

Please sign in to comment.