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
This commit is contained in:
Eliabe Júnior 2019-02-01 09:31:56 -03:00 committed by GitHub
parent 89ce3e4292
commit 89aab4264b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 114 additions and 16 deletions

View File

@ -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);
});
});
});

View File

@ -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);
})
})

View File

@ -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);
});
})

View File

@ -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));
});
})

View File

@ -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);
});
})

View File

@ -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';

View File

@ -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';

View File

@ -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()}`;

View File

@ -1,4 +1,4 @@
// @flow
/* eslint-disable max-len */
// $FlowFixMe
export const sortBy = <T>(field: string) => (arr: T[]): T[] => arr.sort((a, b) => (a[field] < b[field] ? 1 : -1));
export default <T>(field: string) => (arr: T[]): T[] => arr.sort((a, b) => (a[field] < b[field] ? 1 : -1));

View File

@ -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,
)}`;

View File

@ -425,7 +425,7 @@ export class SendView extends PureComponent<Props, State> {
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({

View File

@ -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

View File

@ -1 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="125" height="20"><linearGradient id="b" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><mask id="a"><rect width="125" height="20" rx="3" fill="#fff"/></mask><g mask="url(#a)"><path fill="#555" d="M0 0h91v20H0z"/><path fill="#4C1" d="M91 0h34v20H91z"/><path fill="url(#b)" d="M0 0h125v20H0z"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,DejaVu Sans,Geneva,sans-serif" font-size="11"><text x="45.5" y="15" fill="#010101" fill-opacity=".3">flow-coverage</text><text x="45.5" y="14">flow-coverage</text><text x="107" y="15" fill="#010101" fill-opacity=".3">82%</text><text x="107" y="14">82%</text></g></svg>
<svg xmlns="http://www.w3.org/2000/svg" width="125" height="20"><linearGradient id="b" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><mask id="a"><rect width="125" height="20" rx="3" fill="#fff"/></mask><g mask="url(#a)"><path fill="#555" d="M0 0h91v20H0z"/><path fill="#4C1" d="M91 0h34v20H91z"/><path fill="url(#b)" d="M0 0h125v20H0z"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,DejaVu Sans,Geneva,sans-serif" font-size="11"><text x="45.5" y="15" fill="#010101" fill-opacity=".3">flow-coverage</text><text x="45.5" y="14">flow-coverage</text><text x="107" y="15" fill="#010101" fill-opacity=".3">81%</text><text x="107" y="14">81%</text></g></svg>

Before

Width:  |  Height:  |  Size: 745 B

After

Width:  |  Height:  |  Size: 745 B