Skip to content

Commit

Permalink
Refactor unit test background state fixtures
Browse files Browse the repository at this point in the history
All unit test background state fixtures have been updated to extend the
initial background state fixture. Any background state modifications
not necessary for tests to pass have been removed.

This should dramatically reduce the burden of maintaining these
fixtures, especially as we update our core packages.
  • Loading branch information
Gudahtt committed Jul 19, 2023
1 parent a2f00c2 commit 6e94698
Show file tree
Hide file tree
Showing 57 changed files with 173 additions and 900 deletions.
22 changes: 2 additions & 20 deletions app/components/UI/AccountApproval/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,13 @@ import React from 'react';
import AccountApproval from './';
import { shallow } from 'enzyme';
import configureMockStore from 'redux-mock-store';
import { SEPOLIA } from '../../../constants/network';
import { Provider } from 'react-redux';
import initialBackgroundState from '../../../util/test/initial-background-state.json';

const mockStore = configureMockStore();
const initialState = {
engine: {
backgroundState: {
AccountTrackerController: {
accounts: { '0x2': { balance: '0' } },
},
NetworkController: {
providerConfig: {
type: SEPOLIA,
},
},
TokensController: {
tokens: [],
},
PreferencesController: {
selectedAddress: '0xe7E125654064EEa56229f273dA586F10DF96B0a1',
identities: {
'0xe7E125654064EEa56229f273dA586F10DF96B0a1': { name: 'Account 1' },
},
},
},
backgroundState: initialBackgroundState,
},
};
const store = mockStore(initialState);
Expand Down
137 changes: 44 additions & 93 deletions app/components/UI/AccountFromToInfoCard/AccountFromToInfoCard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,67 +8,13 @@ import { ENSCache } from '../../../util/ENSUtils';
import { Transaction } from './AccountFromToInfoCard.types';
import AccountFromToInfoCard from '.';
import Engine from '../../../core/Engine';
import initialBackgroundState from '../../../util/test/initial-background-state.json';

jest.mock('../../../util/address', () => ({
...jest.requireActual('../../../util/address'),
isQRHardwareAccount: () => false,
}));

jest.mock('../../../core/Engine', () => ({
context: {
TokensController: {
addToken: () => undefined,
},
},
}));

jest.mock('../../../util/ENSUtils', () => ({
...jest.requireActual('../../../util/ENSUtils'),
doENSReverseLookup: jest.fn(),
}));

jest.mock('react-redux', () => ({
...jest.requireActual('react-redux'),
useSelector: (fn: any) =>
fn({
engine: {
backgroundState: {
PreferencesController: {
selectedAddress: '0x0',
identities: {
'0x0': {
address: '0x0',
name: 'Account 1',
},
'0x1': {
address: '0x1',
name: 'Account 2',
},
},
},
NetworkController: {
provider: {
ticker: 'eth',
},
},
AddressBookController: {
addressBook: {},
},
},
},
}),
}));

jest.mock('../../../util/address', () => ({
...jest.requireActual('../../../util/address'),
isQRHardwareAccount: jest.fn(),
}));

const mockStore = configureMockStore();
const initialState = {
const mockInitialState = {
settings: {},
engine: {
backgroundState: {
...initialBackgroundState,
AccountTrackerController: {
accounts: {
'0x0': {
Expand All @@ -84,7 +30,6 @@ const initialState = {
'0x326836cc6cd09B5aa59B81A7F72F25FcC0136b95': '0x5',
},
},
TokensController: {},
PreferencesController: {
selectedAddress: '0x0',
identities: {
Expand All @@ -98,40 +43,46 @@ const initialState = {
},
},
},
CurrencyRateController: {
conversionRate: 10,
currentCurrency: 'usd',
},
NetworkController: {
provider: {
ticker: 'eth',
},
network: '1',
},
AddressBookController: {
addressBook: {
'1': {
'0x0': {
address: '0x0',
name: 'Account 1',
},
'0x1': {
address: '0x1',
name: 'Account 2',
},
},
},
},
},
},
};
const store = mockStore(initialState);

jest.mock('../../../util/address', () => ({
...jest.requireActual('../../../util/address'),
isQRHardwareAccount: () => false,
}));

jest.mock('../../../core/Engine', () => ({
context: {
TokensController: {
addToken: () => undefined,
},
},
}));

jest.mock('../../../util/ENSUtils', () => ({
...jest.requireActual('../../../util/ENSUtils'),
doENSReverseLookup: jest.fn(),
}));

jest.mock('react-redux', () => ({
...jest.requireActual('react-redux'),
useSelector: (fn: any) => fn(mockInitialState),
}));

jest.mock('../../../util/address', () => ({
...jest.requireActual('../../../util/address'),
isQRHardwareAccount: jest.fn(),
}));

const mockStore = configureMockStore();
const store = mockStore(mockInitialState);

jest.mock('react-redux', () => ({
...jest.requireActual('react-redux'),
useSelector: jest
.fn()
.mockImplementation((callback) => callback(initialState)),
.mockImplementation((callback) => callback(mockInitialState)),
}));

const transactionState: Transaction = {
Expand All @@ -155,39 +106,39 @@ describe('AccountFromToInfoCard', () => {
it('should match snapshot', async () => {
const container = renderWithProvider(
<AccountFromToInfoCard transactionState={transactionState} />,
{ state: initialState },
{ state: mockInitialState },
);
expect(container).toMatchSnapshot();
});

it('should render from address', async () => {
const { findByText } = renderWithProvider(
<AccountFromToInfoCard transactionState={transactionState} />,
{ state: initialState },
{ state: mockInitialState },
);
expect(await findByText('Account 1')).toBeDefined();
});

it('should render balance of from address', async () => {
const { findByText } = renderWithProvider(
<AccountFromToInfoCard transactionState={transactionState} />,
{ state: initialState },
{ state: mockInitialState },
);
expect(await findByText('Balance: < 0.00001 ETH')).toBeDefined();
});

it('should render to account name', async () => {
const { findByText } = renderWithProvider(
<AccountFromToInfoCard transactionState={transactionState} />,
{ state: initialState },
{ state: mockInitialState },
);
expect(await findByText('Account 2')).toBeDefined();
});

it('should render to address', async () => {
const { findByText } = renderWithProvider(
<AccountFromToInfoCard transactionState={transactionState} />,
{ state: initialState },
{ state: mockInitialState },
);
expect(await findByText('0x1...0x1')).toBeDefined();
});
Expand All @@ -213,7 +164,7 @@ describe('AccountFromToInfoCard', () => {
};
const { findByText } = renderWithProvider(
<AccountFromToInfoCard transactionState={NFTTransaction as any} />,
{ state: initialState },
{ state: mockInitialState },
);
expect(await findByText('0xF4e8...287B')).toBeDefined();
});
Expand All @@ -236,7 +187,7 @@ describe('AccountFromToInfoCard', () => {
};
const { queryByText } = renderWithProvider(
<AccountFromToInfoCard transactionState={txState} />,
{ state: initialState },
{ state: mockInitialState },
);
expect(await queryByText('test1.eth')).toBeDefined();
expect(await queryByText('test3.eth')).toBeDefined();
Expand Down Expand Up @@ -274,7 +225,7 @@ describe('AccountFromToInfoCard', () => {
it('should render balance from AssetsContractController.getERC20BalanceOf if selectedAddress is different from fromAddress', async () => {
const { findByText } = renderWithProvider(
<AccountFromToInfoCard transactionState={ERC20Transaction as any} />,
{ state: initialState },
{ state: mockInitialState },
);
expect(mockGetERC20BalanceOf).toBeCalledTimes(1);
expect(await findByText('Balance: 10 TST')).toBeDefined();
Expand All @@ -291,7 +242,7 @@ describe('AccountFromToInfoCard', () => {
};
const { findByText } = renderWithProvider(
<AccountFromToInfoCard transactionState={transaction as any} />,
{ state: initialState },
{ state: mockInitialState },
);
expect(mockGetERC20BalanceOf).toBeCalledTimes(0);
expect(await findByText('Balance: 0.0005 TST')).toBeDefined();
Expand Down
7 changes: 2 additions & 5 deletions app/components/UI/AccountRightButton/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@ import { shallow } from 'enzyme';
import { Provider } from 'react-redux';
import configureMockStore from 'redux-mock-store';
import AccountRightButton from './';
import initialBackgroundState from '../../../util/test/initial-background-state.json';

const mockStore = configureMockStore();
const store = mockStore({
engine: {
backgroundState: {
PreferencesController: {
selectedAddress: '0xe7E125654064EEa56229f273dA586F10DF96B0a1',
},
},
backgroundState: initialBackgroundState,
},
});

Expand Down
7 changes: 2 additions & 5 deletions app/components/UI/AddCustomCollectible/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@ import { shallow } from 'enzyme';
import AddCustomCollectible from './';
import configureMockStore from 'redux-mock-store';
import { Provider } from 'react-redux';
import initialBackgroundState from '../../../util/test/initial-background-state.json';

const mockStore = configureMockStore();
const initialState = {
engine: {
backgroundState: {
PreferencesController: {
selectedAddress: '0x1',
},
},
backgroundState: initialBackgroundState,
},
};
const store = mockStore(initialState);
Expand Down
19 changes: 2 additions & 17 deletions app/components/UI/ApproveTransactionReview/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,12 @@ import { shallow } from 'enzyme';
import ApproveTransactionModal from './';
import configureMockStore from 'redux-mock-store';
import { Provider } from 'react-redux';
import initialBackgroundState from '../../../util/test/initial-background-state.json';

const mockStore = configureMockStore();
const initialState = {
engine: {
backgroundState: {
AccountTrackerController: {
accounts: { '0x2': { balance: '0' } },
},
CurrencyRateController: {
conversionRate: 5,
},
NetworkController: {
providerConfig: {
ticker: 'ETH',
type: 'ETH',
},
},
TokensController: {
tokens: [],
},
},
backgroundState: initialBackgroundState,
},
transaction: {},
settings: {
Expand Down
7 changes: 2 additions & 5 deletions app/components/UI/AssetSearch/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@ import { shallow } from 'enzyme';
import AssetSearch from './';
import configureMockStore from 'redux-mock-store';
import { Provider } from 'react-redux';
import initialBackgroundState from '../../../util/test/initial-background-state.json';

const mockStore = configureMockStore();
const initialState = {
engine: {
backgroundState: {
TokenListController: {
tokenList: {},
},
},
backgroundState: initialBackgroundState,
},
};
const store = mockStore(initialState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@ import { shallow } from 'enzyme';
import CollectibleContractInformation from './';
import configureMockStore from 'redux-mock-store';
import { Provider } from 'react-redux';
import initialBackgroundState from '../../../util/test/initial-background-state.json';

const mockStore = configureMockStore();
const initialState = {
engine: {
backgroundState: {
NetworkController: {
providerConfig: {
type: 'mainnet',
},
},
},
backgroundState: initialBackgroundState,
},
};
const store = mockStore(initialState);
Expand Down
Loading

0 comments on commit 6e94698

Please sign in to comment.