From 5b63c7adf39fb9b2e97862ad37dfab2318f8951a Mon Sep 17 00:00:00 2001 From: Thomas Huang Date: Tue, 16 Aug 2022 08:25:30 -0700 Subject: [PATCH] Unit tests improvements (#15517) --- test/data/mock-state.json | 39 +++ test/lib/render-helpers.js | 8 +- .../import-token-link.test.js.snap | 53 +++++ .../import-token-link.component.js | 2 + .../import-token-link.test.js | 95 ++++++++ .../signature-request-original.test.js.snap | 225 ++++++++++++++++++ .../signature-request-original.test.js | 64 +++++ .../__snapshots__/seed-phrase.test.js.snap | 203 ++++++++++++++++ .../confirm-seed-phrase.component.js | 2 +- .../reveal-seed-phrase.component.js | 2 +- .../seed-phrase-intro.component.js | 2 +- .../seed-phrase/seed-phrase.test.js | 127 ++++++++++ 12 files changed, 816 insertions(+), 6 deletions(-) create mode 100644 ui/components/app/import-token-link/__snapshots__/import-token-link.test.js.snap create mode 100644 ui/components/app/import-token-link/import-token-link.test.js create mode 100644 ui/components/app/signature-request-original/__snapshots__/signature-request-original.test.js.snap create mode 100644 ui/components/app/signature-request-original/signature-request-original.test.js create mode 100644 ui/pages/first-time-flow/seed-phrase/__snapshots__/seed-phrase.test.js.snap create mode 100644 ui/pages/first-time-flow/seed-phrase/seed-phrase.test.js diff --git a/test/data/mock-state.json b/test/data/mock-state.json index 231f05069..b378b37c4 100644 --- a/test/data/mock-state.json +++ b/test/data/mock-state.json @@ -8,7 +8,44 @@ "context": "0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc" } }, + "history": { + "mostRecentOverviewPage": "/" + }, "metamask": { + "gasFeeEstimates": { + "low": { + "suggestedMaxPriorityFeePerGas": "1.233864249", + "suggestedMaxFeePerGas": "19.308202033", + "minWaitTimeEstimate": 15000, + "maxWaitTimeEstimate": 30000 + }, + "medium": { + "suggestedMaxPriorityFeePerGas": "1.5", + "suggestedMaxFeePerGas": "25.900356008", + "minWaitTimeEstimate": 15000, + "maxWaitTimeEstimate": 45000 + }, + "high": { + "suggestedMaxPriorityFeePerGas": "2", + "suggestedMaxFeePerGas": "32.726374232", + "minWaitTimeEstimate": 15000, + "maxWaitTimeEstimate": 60000 + }, + "estimatedBaseFee": "18.074337783", + "historicalBaseFeeRange": ["10.621659703", "19.673392581"], + "baseFeeTrend": "up", + "latestPriorityFeeRange": ["1.5", "17.536644176"], + "historicalPriorityFeeRange": ["0.110624865", "718.303816711"], + "priorityFeeTrend": "up", + "networkCongestion": 0.87115 + }, + "preferences": { + "hideZeroBalanceTokens": false, + "showFiatInTestnets": false, + "showTestNetworks": true, + "useNativeCurrencyAsPrimaryCurrency": true + }, + "ensResolutionsByAddress": {}, "featureFlags": { "showIncomingTransactions": true }, @@ -37,6 +74,8 @@ "1559": true } }, + "frequentRpcListDetail": [], + "subjectMetadata": {}, "notifications": { "test": { "id": "test", diff --git a/test/lib/render-helpers.js b/test/lib/render-helpers.js index 3fcc7528b..0291df0f8 100644 --- a/test/lib/render-helpers.js +++ b/test/lib/render-helpers.js @@ -93,9 +93,11 @@ export function renderWithProvider(component, store, pathname = '/') { ) : ( - - {children} - + + + {children} + + ); Wrapper.propTypes = { diff --git a/ui/components/app/import-token-link/__snapshots__/import-token-link.test.js.snap b/ui/components/app/import-token-link/__snapshots__/import-token-link.test.js.snap new file mode 100644 index 000000000..a3d8b9e7d --- /dev/null +++ b/ui/components/app/import-token-link/__snapshots__/import-token-link.test.js.snap @@ -0,0 +1,53 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Import Token Link should match snapshot for mainnet chainId 1`] = ` +
+ +
+`; + +exports[`Import Token Link should match snapshot for rinkeby chainId 1`] = ` +
+ +
+`; diff --git a/ui/components/app/import-token-link/import-token-link.component.js b/ui/components/app/import-token-link/import-token-link.component.js index 603741ee0..08473b387 100644 --- a/ui/components/app/import-token-link/import-token-link.component.js +++ b/ui/components/app/import-token-link/import-token-link.component.js @@ -35,6 +35,7 @@ export default function ImportTokenLink() { <> + + + + +`; diff --git a/ui/components/app/signature-request-original/signature-request-original.test.js b/ui/components/app/signature-request-original/signature-request-original.test.js new file mode 100644 index 000000000..fa0be4ca5 --- /dev/null +++ b/ui/components/app/signature-request-original/signature-request-original.test.js @@ -0,0 +1,64 @@ +import React from 'react'; +import configureMockStore from 'redux-mock-store'; +import { MESSAGE_TYPE } from '../../../../shared/constants/app'; +import mockState from '../../../../test/data/mock-state.json'; +import { renderWithProvider } from '../../../../test/lib/render-helpers'; +import SignatureRequestOriginal from '.'; + +const MOCK_SIGN_DATA = JSON.stringify({ + domain: { + name: 'happydapp.website', + }, + message: { + string: 'haay wuurl', + number: 42, + }, + primaryType: 'Mail', + types: { + EIP712Domain: [ + { name: 'name', type: 'string' }, + { name: 'version', type: 'string' }, + { name: 'chainId', type: 'uint256' }, + { name: 'verifyingContract', type: 'address' }, + ], + Group: [ + { name: 'name', type: 'string' }, + { name: 'members', type: 'Person[]' }, + ], + Mail: [ + { name: 'from', type: 'Person' }, + { name: 'to', type: 'Person[]' }, + { name: 'contents', type: 'string' }, + ], + Person: [ + { name: 'name', type: 'string' }, + { name: 'wallets', type: 'address[]' }, + ], + }, +}); + +describe('SignatureRequestOriginal', () => { + const store = configureMockStore()(mockState); + + it('should match snapshot', () => { + const props = { + signMessage: jest.fn(), + cancelMessage: jest.fn(), + txData: { + msgParams: { + from: '0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc', + data: MOCK_SIGN_DATA, + origin: 'https://happydapp.website/governance?futarchy=true', + }, + type: MESSAGE_TYPE.ETH_SIGN, + }, + }; + + const { container } = renderWithProvider( + , + store, + ); + + expect(container).toMatchSnapshot(); + }); +}); diff --git a/ui/pages/first-time-flow/seed-phrase/__snapshots__/seed-phrase.test.js.snap b/ui/pages/first-time-flow/seed-phrase/__snapshots__/seed-phrase.test.js.snap new file mode 100644 index 000000000..e3128df22 --- /dev/null +++ b/ui/pages/first-time-flow/seed-phrase/__snapshots__/seed-phrase.test.js.snap @@ -0,0 +1,203 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`SeedPhrase Component should match snapshot 1`] = ` +
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+`; diff --git a/ui/pages/first-time-flow/seed-phrase/confirm-seed-phrase/confirm-seed-phrase.component.js b/ui/pages/first-time-flow/seed-phrase/confirm-seed-phrase/confirm-seed-phrase.component.js index 05d0338fe..45ea77a2f 100644 --- a/ui/pages/first-time-flow/seed-phrase/confirm-seed-phrase/confirm-seed-phrase.component.js +++ b/ui/pages/first-time-flow/seed-phrase/confirm-seed-phrase/confirm-seed-phrase.component.js @@ -130,7 +130,7 @@ export default class ConfirmSeedPhrase extends PureComponent { this.state; return ( -
+
{ diff --git a/ui/pages/first-time-flow/seed-phrase/reveal-seed-phrase/reveal-seed-phrase.component.js b/ui/pages/first-time-flow/seed-phrase/reveal-seed-phrase/reveal-seed-phrase.component.js index d9bb82c17..3c78ac41c 100644 --- a/ui/pages/first-time-flow/seed-phrase/reveal-seed-phrase/reveal-seed-phrase.component.js +++ b/ui/pages/first-time-flow/seed-phrase/reveal-seed-phrase/reveal-seed-phrase.component.js @@ -148,7 +148,7 @@ export default class RevealSeedPhrase extends PureComponent { const { history, onboardingInitiator } = this.props; return ( -
+
diff --git a/ui/pages/first-time-flow/seed-phrase/seed-phrase-intro/seed-phrase-intro.component.js b/ui/pages/first-time-flow/seed-phrase/seed-phrase-intro/seed-phrase-intro.component.js index 841b13fe1..b656c6735 100644 --- a/ui/pages/first-time-flow/seed-phrase/seed-phrase-intro/seed-phrase-intro.component.js +++ b/ui/pages/first-time-flow/seed-phrase/seed-phrase-intro/seed-phrase-intro.component.js @@ -43,7 +43,7 @@ export default function SeedPhraseIntro() { }; return ( -
+
{ + afterEach(() => { + sinon.restore(); + }); + + it('should match snapshot', () => { + const props = { + history: { + push: sinon.stub(), + }, + verifySeedPhrase: sinon.stub().resolves('verifed seed'), + }; + + const { container } = renderWithProvider(); + + expect(container).toMatchSnapshot(); + }); + + it('should render confirm seed phrase component with seed-phrase/confirm route', () => { + const mockState = { + metamask: {}, + }; + + const store = configureMockStore()(mockState); + const props = { + history: { + push: sinon.stub(), + }, + verifySeedPhrase: sinon.stub().resolves(), + }; + + renderWithProvider( + , + store, + INITIALIZE_CONFIRM_SEED_PHRASE_ROUTE, + ); + + const confirmSeedPhrase = screen.queryByTestId('confirm-seed-phrase'); + expect(confirmSeedPhrase).toBeInTheDocument(); + }); + + it('should render reveal-seed-phrase component with /seed-phrase route', () => { + const mockState = { + metamask: {}, + }; + + const store = configureMockStore()(mockState); + const props = { + history: { + push: sinon.stub(), + }, + verifySeedPhrase: sinon.stub().resolves(), + }; + + renderWithProvider( + , + store, + INITIALIZE_SEED_PHRASE_ROUTE, + ); + + const confirmSeedPhrase = screen.queryByTestId('reveal-seed-phrase'); + expect(confirmSeedPhrase).toBeInTheDocument(); + }); + + it('should render reveal-seed-phrase component with /backup-seed-phrase route', () => { + const mockState = { + metamask: { + onboardingTabs: [], + }, + }; + + const store = configureMockStore()(mockState); + const props = { + history: { + push: sinon.stub(), + }, + verifySeedPhrase: sinon.stub().resolves(), + }; + + renderWithProvider( + , + store, + INITIALIZE_BACKUP_SEED_PHRASE_ROUTE, + ); + + const confirmSeedPhrase = screen.queryByTestId('reveal-seed-phrase'); + expect(confirmSeedPhrase).toBeInTheDocument(); + }); + + it('should render reveal-seed-phrase component with /seed-phrase-intro route', () => { + const mockState = { + metamask: { + onboardingTabs: [], + }, + }; + + const store = configureMockStore()(mockState); + const props = { + history: { + push: sinon.stub(), + }, + verifySeedPhrase: sinon.stub().resolves(), + }; + + renderWithProvider( + , + store, + INITIALIZE_SEED_PHRASE_INTRO_ROUTE, + ); + + const seedPhraseIntro = screen.queryByTestId('seed-phrase-intro'); + expect(seedPhraseIntro).toBeInTheDocument(); + }); +});