From 89aab4264b32c282046f8da8d5ad6edbbc3c69c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eliabe=20J=C3=BAnior?= <8146889+eliabejr@users.noreply.github.com> Date: Fri, 1 Feb 2019 09:31:56 -0300 Subject: [PATCH] Feature/test utils with jest (#56) * fix(test-utils): wallet summary tests * feat(test-utils): create tests for utils functions * chore(test-utils): rename utils to kebab case * chore(test-utils): move utils to right folder * fix(test-utils): utils import * fix(test-utils): bignumber toFormat function types * feat(test-utils): increase formatNumber test coverage --- __tests__/reducers/wallet-summary.test.js | 18 ++++++--- .../utils/filter-object-null-keys.test.js | 22 +++++++++++ __tests__/utils/format-number.test.js | 37 +++++++++++++++++++ __tests__/utils/timestamp.test.js | 13 +++++++ __tests__/utils/truncate-address.test.js | 14 +++++++ app/containers/dashboard.js | 2 +- app/containers/transactions.js | 2 +- app/utils/format-number.js | 3 +- app/utils/sort-by.js | 2 +- app/utils/truncate-address.js | 5 ++- app/views/send.js | 2 +- flow-custom-typedefs/bignumber.js | 8 ++-- public/flow-coverage-badge.svg | 2 +- 13 files changed, 114 insertions(+), 16 deletions(-) create mode 100644 __tests__/utils/filter-object-null-keys.test.js create mode 100644 __tests__/utils/format-number.test.js create mode 100644 __tests__/utils/timestamp.test.js create mode 100644 __tests__/utils/truncate-address.test.js diff --git a/__tests__/reducers/wallet-summary.test.js b/__tests__/reducers/wallet-summary.test.js index 7095f5e..b7a9156 100644 --- a/__tests__/reducers/wallet-summary.test.js +++ b/__tests__/reducers/wallet-summary.test.js @@ -8,12 +8,14 @@ import walletSummaryReducer, { describe('WalletSummary Reducer', () => { test('should return the valid initial state', () => { const initialState = { + addresses: [], + transactions: [], total: 0, shielded: 0, transparent: 0, error: null, isLoading: false, - dollarValue: 0, + zecPrice: 0, }; const action = { type: 'UNKNOWN_ACTION', @@ -29,12 +31,14 @@ describe('WalletSummary Reducer', () => { payload: {}, }; const expectedState = { + addresses: [], + transactions: [], total: 0, shielded: 0, transparent: 0, error: null, isLoading: true, - dollarValue: 0, + zecPrice: 0, }; expect(walletSummaryReducer(undefined, action)).toEqual(expectedState); @@ -53,7 +57,9 @@ describe('WalletSummary Reducer', () => { ...action.payload, error: null, isLoading: false, - dollarValue: 0, + addresses: [], + transactions: [], + zecPrice: 0, }; expect(walletSummaryReducer(undefined, action)).toEqual(expectedState); @@ -72,9 +78,11 @@ describe('WalletSummary Reducer', () => { transparent: 0, error: action.payload.error, isLoading: false, - dollarValue: 0, + addresses: [], + transactions: [], + zecPrice: 0 }; expect(walletSummaryReducer(undefined, action)).toEqual(expectedState); }); -}); +}); \ No newline at end of file diff --git a/__tests__/utils/filter-object-null-keys.test.js b/__tests__/utils/filter-object-null-keys.test.js new file mode 100644 index 0000000..b0bb3fd --- /dev/null +++ b/__tests__/utils/filter-object-null-keys.test.js @@ -0,0 +1,22 @@ +// @flow +import 'jest-dom/extend-expect'; + +import { filterObjectNullKeys } from '../../app/utils/filter-object-null-keys'; + +describe('filterObjectNullKeys', () => { + test('should filter null keys from object', () => { + const initialState = { + name: 'John Doe', + address: null, + amount: 0, + transactions: undefined, + }; + + const expectedState = { + name: 'John Doe', + amount: 0, + }; + + expect(filterObjectNullKeys(initialState)).toEqual(expectedState); + }) +}) \ No newline at end of file diff --git a/__tests__/utils/format-number.test.js b/__tests__/utils/format-number.test.js new file mode 100644 index 0000000..af44a55 --- /dev/null +++ b/__tests__/utils/format-number.test.js @@ -0,0 +1,37 @@ +// @flow +import { BigNumber } from 'bignumber.js'; +import 'jest-dom/extend-expect'; + +import { formatNumber } from '../../app/utils/format-number'; + +describe('formatNumber', () => { + test('should append ZEC in balance amount', () => { + const myBalance = formatNumber({ value: 2.5, append: 'ZEC ' }); + + const expectedState = 'ZEC 2.5'; + + expect(myBalance).toEqual(expectedState); + }); + + test('should multiply ZEC balance and show it in USD', () => { + const myBalanceInUsd = formatNumber({ + value: new BigNumber(2.5).times(1.35).toNumber(), + append: 'USD $', + }); + + const expectedState = 'USD $3.375'; + + expect(myBalanceInUsd).toEqual(expectedState); + }); + + test('should multiply decimal ZEC balance and show it in USD', () => { + const myBalanceInUsd = formatNumber({ + value: new BigNumber(0.1).times(0.2).toNumber(), + append: 'USD $', + }); + + const expectedState = 'USD $0.02'; + + expect(myBalanceInUsd).toEqual(expectedState); + }); +}) \ No newline at end of file diff --git a/__tests__/utils/timestamp.test.js b/__tests__/utils/timestamp.test.js new file mode 100644 index 0000000..5d912db --- /dev/null +++ b/__tests__/utils/timestamp.test.js @@ -0,0 +1,13 @@ +// @flow +import 'jest-dom/extend-expect'; + +import { getTimestamp } from '../../app/utils/timestamp'; + +describe('generate timestamp', () => { + test('should generate a random string', () => { + const now = getTimestamp(); + + expect(now).toEqual(expect.any(Number)); + + }); +}) \ No newline at end of file diff --git a/__tests__/utils/truncate-address.test.js b/__tests__/utils/truncate-address.test.js new file mode 100644 index 0000000..460ede5 --- /dev/null +++ b/__tests__/utils/truncate-address.test.js @@ -0,0 +1,14 @@ +// @flow +import 'jest-dom/extend-expect'; + +import { truncateAddress } from '../../app/utils/truncate-address'; + +describe('truncateAddress', () => { + test('should truncate ZEC address', () => { + const myAddress = truncateAddress('t14oHp2v54vfmdgQ3v3SNuQga8JKHTNi2a1'); + + const expectedState = 't14oHp2v54vfmdgQ3v3S...8JKHTNi2a1'; + + expect(myAddress).toEqual(expectedState); + }); +}) \ No newline at end of file diff --git a/app/containers/dashboard.js b/app/containers/dashboard.js index 28b88cd..1c0f085 100644 --- a/app/containers/dashboard.js +++ b/app/containers/dashboard.js @@ -14,7 +14,7 @@ import { loadWalletSummarySuccess, loadWalletSummaryError, } from '../redux/modules/wallet'; -import { sortBy } from '../utils/sort-by'; +import sortBy from '../utils/sort-by'; import type { AppState } from '../types/app-state'; import type { Dispatch } from '../types/redux'; diff --git a/app/containers/transactions.js b/app/containers/transactions.js index fd8dffc..1584fec 100644 --- a/app/containers/transactions.js +++ b/app/containers/transactions.js @@ -15,7 +15,7 @@ import { import rpc from '../../services/api'; import store from '../../config/electron-store'; -import { sortBy } from '../utils/sort-by'; +import sortBy from '../utils/sort-by'; import type { AppState } from '../types/app-state'; import type { Dispatch } from '../types/redux'; diff --git a/app/utils/format-number.js b/app/utils/format-number.js index 3fc4ad7..ca140f4 100644 --- a/app/utils/format-number.js +++ b/app/utils/format-number.js @@ -1,2 +1,3 @@ // @flow -export const formatNumber = ({ value, append = '' }: { value: number | string, append?: string }) => `${append}${(value || 0).toLocaleString()}`; + +export const formatNumber = ({ value, append = '' }: { value: number, append?: string }) => `${append}${(value || 0).toLocaleString()}`; diff --git a/app/utils/sort-by.js b/app/utils/sort-by.js index f8eefb6..25a102e 100644 --- a/app/utils/sort-by.js +++ b/app/utils/sort-by.js @@ -1,4 +1,4 @@ // @flow /* eslint-disable max-len */ // $FlowFixMe -export const sortBy = (field: string) => (arr: T[]): T[] => arr.sort((a, b) => (a[field] < b[field] ? 1 : -1)); +export default (field: string) => (arr: T[]): T[] => arr.sort((a, b) => (a[field] < b[field] ? 1 : -1)); diff --git a/app/utils/truncate-address.js b/app/utils/truncate-address.js index 49493cd..978a324 100644 --- a/app/utils/truncate-address.js +++ b/app/utils/truncate-address.js @@ -1,3 +1,6 @@ // @flow -export const truncateAddress = (address: string = '') => `${address.substr(0, 20)}...${address.substr(address.length - 10, address.length)}`; +export const truncateAddress = (address: string = '') => `${address.substr(0, 20)}...${address.substr( + address.length - 10, + address.length, +)}`; diff --git a/app/views/send.js b/app/views/send.js index 16f244c..abff385 100644 --- a/app/views/send.js +++ b/app/views/send.js @@ -425,7 +425,7 @@ export class SendView extends PureComponent { const isEmpty = amount === ''; - const fixedAmount = isEmpty ? '0.00' : amount; + const fixedAmount = isEmpty ? 0.0 : amount; const zecBalance = formatNumber({ value: balance, append: 'ZEC ' }); const zecBalanceInUsd = formatNumber({ diff --git a/flow-custom-typedefs/bignumber.js b/flow-custom-typedefs/bignumber.js index 65bbb11..612c835 100644 --- a/flow-custom-typedefs/bignumber.js +++ b/flow-custom-typedefs/bignumber.js @@ -999,10 +999,10 @@ x.toFormat(3, BigNumber.ROUND_UP, fmt) // '12.34.56.789,124' roundingMode: BigNumber$RoundingMode, format?: BigNumber$Format, ): string; - toFormat(decimalPlaces: number, roundingMode?: BigNumber$RoundingMode): string; - toFormat(decimalPlaces?: number): string; - toFormat(decimalPlaces: number, format: BigNumber$Format): string; - toFormat(format: BigNumber$Format): string; + toFormat(decimalPlaces: number, roundingMode?: BigNumber$RoundingMode): number; + toFormat(decimalPlaces?: number): number; + toFormat(decimalPlaces: number, format: BigNumber$Format): number; + toFormat(format: BigNumber$Format): number; /** * Returns an array of two BigNumbers representing the value of this BigNumber as a simple diff --git a/public/flow-coverage-badge.svg b/public/flow-coverage-badge.svg index 665ba93..d61c9ff 100644 --- a/public/flow-coverage-badge.svg +++ b/public/flow-coverage-badge.svg @@ -1 +1 @@ -flow-coverageflow-coverage82%82% \ No newline at end of file +flow-coverageflow-coverage81%81% \ No newline at end of file