WA-238 Adding tests of confirmations and transactions selectors

This commit is contained in:
apanizo 2018-05-26 12:01:08 +02:00
parent 8abc514413
commit 44ed495226
4 changed files with 242 additions and 6 deletions

View File

@ -23,9 +23,9 @@ type TransactionProps = {
transaction: Transaction,
}
const safeAddressSelector = (state: GlobalState, props: SafeProps) => props.safeAddress
const safePropAddressSelector = (state: GlobalState, props: SafeProps) => props.safeAddress
const safeAddessSelector = (state: GlobalState, props: RouterProps) => props.match.params[SAFE_PARAM_ADDRESS] || ''
const safeParamAddressSelector = (state: GlobalState, props: RouterProps) => props.match.params[SAFE_PARAM_ADDRESS] || ''
const balancesSelector = (state: GlobalState) => state[BALANCE_REDUCER_ID]
@ -35,7 +35,7 @@ const oneTransactionSelector = (state: GlobalState, props: TransactionProps) =>
export const safeTransactionsSelector: Selector<GlobalState, SafeProps, List<Transaction>> = createSelector(
transactionsSelector,
safeAddressSelector,
safePropAddressSelector,
(transactions: TransactionsState, address: string): List<Transaction> => {
if (!transactions) {
return List([])
@ -49,7 +49,7 @@ export const safeTransactionsSelector: Selector<GlobalState, SafeProps, List<Tra
},
)
export const confirmationsTransactionSelector = createSelector(
export const confirmationsTransactionSelector: Selector<GlobalState, TransactionProps, number> = createSelector(
oneTransactionSelector,
(tx: Transaction) => {
if (!tx) {
@ -69,7 +69,7 @@ export type SafeSelectorProps = Safe | typeof undefined
export const safeSelector: Selector<GlobalState, RouterProps, SafeSelectorProps> = createSelector(
safesMapSelector,
safeAddessSelector,
safeParamAddressSelector,
(safes: Map<string, Safe>, address: string) => {
if (!address) {
return undefined
@ -81,7 +81,7 @@ export const safeSelector: Selector<GlobalState, RouterProps, SafeSelectorProps>
export const balanceSelector: Selector<GlobalState, RouterProps, string> = createSelector(
balancesSelector,
safeAddessSelector,
safeParamAddressSelector,
(balances: Map<string, string>, address: string) => {
if (!address) {
return '0'

View File

@ -0,0 +1,99 @@
// @flow
import { List, Map } from 'immutable'
import { makeTransaction, type Transaction } from '~/routes/safe/store/model/transaction'
import { type Confirmation, makeConfirmation } from '~/routes/safe/store/model/confirmation'
import { makeOwner } from '~/routes/safe/store/model/owner'
import { confirmationsTransactionSelector } from '~/routes/safe/store/selectors/index'
import { makeProvider } from '~/wallets/store/model/provider'
const grantedSelectorTests = () => {
describe('Safe Selector[confirmationsTransactionSelector]', () => {
it('returns 1 confirmation if safe has only one owner when tx is created', () => {
// GIVEN
const firstConfirmation: Confirmation = makeConfirmation({
owner: makeOwner(),
status: true,
hash: 'asdf',
})
const transaction: Transaction = makeTransaction({
name: 'Buy batteries',
nonce: 1,
value: 2,
confirmations: List([firstConfirmation]),
destination: 'destAddress',
threshold: 2,
tx: '',
})
const reduxStore = {
safes: Map(),
providers: makeProvider(),
balances: Map(),
transactions: Map(),
}
// WHEN
const threshold = confirmationsTransactionSelector(reduxStore, { transaction })
// THEN
expect(threshold).toBe(1)
})
it('returns 1 confirmation if safe has two or more owners when multisig tx is created', () => {
// GIVEN
const firstConfirmation: Confirmation = makeConfirmation({
owner: makeOwner(),
status: true,
hash: 'asdf',
})
const secondConfirmation: Confirmation = makeConfirmation({
owner: makeOwner(),
status: false,
hash: '',
})
const transaction: Transaction = makeTransaction({
name: 'Buy batteries',
nonce: 1,
value: 2,
confirmations: List([firstConfirmation, secondConfirmation]),
destination: 'destAddress',
threshold: 2,
tx: '',
})
const reduxStore = {
safes: Map(),
providers: makeProvider(),
balances: Map(),
transactions: Map(),
}
// WHEN
const threshold = confirmationsTransactionSelector(reduxStore, { transaction })
// THEN
expect(threshold).toBe(1)
})
it('should return 0 confirmations if not transaction is sent as prop to component', () => {
const reduxStore = {
safes: Map(),
providers: makeProvider(),
balances: Map(),
transactions: Map(),
}
// WHEN
// $FlowFixMe
const threshold = confirmationsTransactionSelector(reduxStore, { transaction: undefined })
// THEN
expect(threshold).toBe(0)
})
})
}
export default grantedSelectorTests

View File

@ -5,6 +5,8 @@ import dailyLimitReducerTests from './dailyLimit.reducer'
import balanceSelectorTests from './balance.selector'
import safeSelectorTests from './safe.selector'
import grantedSelectorTests from './granted.selector'
import confirmationsSelectorTests from './confirmations.selector'
import transactionsSelectorTests from './transactions.selector'
describe('Safe Test suite', () => {
// ACTIONS AND REDUCERS
@ -20,4 +22,10 @@ describe('Safe Test suite', () => {
// GRANTED SELECTOR
grantedSelectorTests()
// CONFIRMATIONS SELECTOR
confirmationsSelectorTests()
// TRANSACTIONS SELECTOR
transactionsSelectorTests()
})

View File

@ -0,0 +1,129 @@
// @flow
import { List, Map } from 'immutable'
import { SAFE_REDUCER_ID } from '~/routes/safe/store/reducer/safe'
import { safeTransactionsSelector } from '~/routes/safe/store/selectors/index'
import { makeProvider } from '~/wallets/store/model/provider'
import { makeConfirmation, type Confirmation } from '~/routes/safe/store/model/confirmation'
import { makeOwner } from '~/routes/safe/store/model/owner'
import { makeTransaction, type Transaction } from '~/routes/safe/store/model/transaction'
const grantedSelectorTests = () => {
describe('Safe Selector[safeTransactionsSelector]', () => {
it('should return empty list if no transactions in store', () => {
// GIVEN
const reduxStore = {
[SAFE_REDUCER_ID]: Map(),
providers: makeProvider(),
balances: undefined,
transactions: Map(),
}
// WHEN
const transactions = safeTransactionsSelector(reduxStore, { safeAddress: 'fooAddress' })
// THEN
expect(transactions).toEqual(List([]))
})
it('should return empty list if transactions in store but not safe address in props', () => {
// GIVEN
const firstConfirmation: Confirmation = makeConfirmation({
owner: makeOwner(),
status: true,
hash: 'asdf',
})
const transaction: Transaction = makeTransaction({
name: 'Buy batteries',
nonce: 1,
value: 2,
confirmations: List([firstConfirmation]),
destination: 'destAddress',
threshold: 2,
tx: '',
})
const reduxStore = {
[SAFE_REDUCER_ID]: Map(),
providers: makeProvider(),
balances: undefined,
transactions: Map({ fooAddress: List([transaction]) }),
}
// WHEN
const transactionsEmpty = safeTransactionsSelector(reduxStore, { safeAddress: '' })
// $FlowFixMe
const transactionsUndefined = safeTransactionsSelector(reduxStore, { safeAddress: undefined })
// THEN
expect(transactionsEmpty).toEqual(List([]))
expect(transactionsUndefined).toEqual(List([]))
})
it('should return empty list if there are transactions belonging to different address', () => {
// GIVEN
const firstConfirmation: Confirmation = makeConfirmation({
owner: makeOwner(),
status: true,
hash: 'asdf',
})
const transaction: Transaction = makeTransaction({
name: 'Buy batteries',
nonce: 1,
value: 2,
confirmations: List([firstConfirmation]),
destination: 'destAddress',
threshold: 2,
tx: '',
})
const reduxStore = {
[SAFE_REDUCER_ID]: Map(),
providers: makeProvider(),
balances: undefined,
transactions: Map({ fooAddress: List([transaction]) }),
}
// WHEN
const transactions = safeTransactionsSelector(reduxStore, { safeAddress: 'invented' })
// THEN
expect(transactions).toEqual(List([]))
})
it('should return transactions of safe', () => {
// GIVEN
const firstConfirmation: Confirmation = makeConfirmation({
owner: makeOwner(),
status: true,
hash: 'asdf',
})
const transaction: Transaction = makeTransaction({
name: 'Buy batteries',
nonce: 1,
value: 2,
confirmations: List([firstConfirmation]),
destination: 'destAddress',
threshold: 2,
tx: '',
})
const reduxStore = {
[SAFE_REDUCER_ID]: Map(),
providers: makeProvider(),
balances: undefined,
transactions: Map({ fooAddress: List([transaction]) }),
}
// WHEN
const transactions = safeTransactionsSelector(reduxStore, { safeAddress: 'fooAddress' })
// THEN
expect(transactions).toEqual(List([transaction]))
})
})
}
export default grantedSelectorTests