Skip to content

Commit

Permalink
Replaced balance, claim, & transaction history actions/reducers with …
Browse files Browse the repository at this point in the history
…new actions architecture (#611)

* Added actions for fetching asset & token balances

* Removed unused prop mapping from app store

* Added balance actions and HOC's to replaced most of loadWalletData function

* Reset stores on logout

* Renamed accountActions to authActions

* Removed unused deprecated functions

* Fixed fetching state from action that has not been fetched

* Added claims actions and account actions

* Replaced load wallet claim sync with new action data

* Replaced load wallet transactions sync with new action data

* Fixed tokens argument being passed to account request action
  • Loading branch information
mhuggins authored and evgenyboxer committed Jan 30, 2018
1 parent 737f3cd commit bcb8a4c
Show file tree
Hide file tree
Showing 53 changed files with 1,004 additions and 1,050 deletions.
178 changes: 59 additions & 119 deletions __tests__/components/Claim.test.js
Original file line number Diff line number Diff line change
@@ -1,170 +1,110 @@
import React from 'react'
import * as neonjs from 'neon-js'
import { cloneDeep } from 'lodash'
import { Provider } from 'react-redux'
import configureStore from 'redux-mock-store'
import thunk from 'redux-thunk'
import { shallow, mount } from 'enzyme'
import { merge } from 'lodash'
import { mount } from 'enzyme'

import { createStore, provideStore } from '../testHelpers'
import Claim from '../../app/containers/Claim'
import { setClaimRequest, disableClaim } from '../../app/modules/claim'
import { SHOW_NOTIFICATION, HIDE_NOTIFICATIONS, DEFAULT_POSITION } from '../../app/modules/notifications'
import { SHOW_NOTIFICATION } from '../../app/modules/notifications'
import { NOTIFICATION_LEVELS, MAIN_NETWORK_ID } from '../../app/core/constants'
import { LOADED } from '../../app/values/state'

jest.useFakeTimers()

const initialState = {
api: {
NETWORK: {
batch: false,
state: LOADED,
data: MAIN_NETWORK_ID
},
SETTINGS: {
CLAIMS: {
batch: false,
state: LOADED,
data: {}
data: {
total: '0.01490723'
}
},
BALANCES: {
batch: false,
state: LOADED,
data: {
NEO: '10',
GAS: '10.00000000'
}
}
},
claim: {
claimAmount: 10,
claimRequest: false,
claimWasUpdated: false,
disableClaimButton: false
},
account: {
wif: 'wif',
address: 'address'
},
wallet: {
NEO: '1'
}
}

const setup = (state = initialState, shallowRender = true) => {
const store = configureStore([thunk])(state)

let wrapper
if (shallowRender) {
wrapper = shallow(<Claim store={store} />)
} else {
wrapper = mount(
<Provider store={store}>
<Claim />
</Provider>
)
}

return {
store,
wrapper
}
const simulateSendAsset = (result) => {
return jest.fn(() => {
return new Promise((resolve, reject) => {
resolve({ result })
})
})
}

describe('Claim', () => {
test('should render without crashing', () => {
const { wrapper } = setup()
const store = createStore(initialState)
const wrapper = mount(provideStore(<Claim />, store))
expect(wrapper).toMatchSnapshot()
})

test('should render claim GAS button when claim button is not disabled', () => {
const { wrapper } = setup()
expect(wrapper.dive()).toMatchSnapshot()
test('should render claim GAS button as enabled', () => {
const store = createStore(initialState)
const wrapper = mount(provideStore(<Claim />, store))
expect(wrapper).toMatchSnapshot()
})

test('should not render claim GAS button when claim button is disabled', () => {
const newState = cloneDeep(initialState)
newState.claim.disableClaimButton = true
const { wrapper } = setup(newState)
expect(wrapper.dive()).toMatchSnapshot()
test('should render claim GAS button as disabled', () => {
const state = merge({}, initialState, { claim: { disableClaimButton: true } })
const store = createStore(state)
const wrapper = mount(provideStore(<Claim />, store))
expect(wrapper).toMatchSnapshot()
})

describe('when do GAS claim button is clicked', () => {
test('should dispatch transaction failure event', async () => {
const { wrapper, store } = setup()
neonjs.api.neonDB.doSendAsset = jest.fn(() => {
return new Promise((resolve, reject) => {
resolve({ result: undefined })
})
})
wrapper.dive().find('#claim').simulate('click')
describe('when claim GAS button is clicked', () => {
test('should dispatch transaction failure event', async (done) => {
const store = createStore(initialState)
const wrapper = mount(provideStore(<Claim />, store))
neonjs.api.neonDB.doSendAsset = simulateSendAsset(false)

wrapper.find('button#claim').simulate('click')
jest.runAllTimers()
await Promise.resolve().then().then().then()

const actions = store.getActions()
expect(actions.length).toEqual(4)
expect(actions[0]).toEqual({
type: HIDE_NOTIFICATIONS,
payload: expect.objectContaining({
dismissible: true,
position: DEFAULT_POSITION
})
})
expect(actions[1]).toEqual({
type: SHOW_NOTIFICATION,
payload: expect.objectContaining({
message: 'Sending NEO to Yourself...',
level: NOTIFICATION_LEVELS.INFO
})
})
expect(actions[2]).toEqual({
type: HIDE_NOTIFICATIONS,
payload: expect.objectContaining({
dismissible: true,
position: DEFAULT_POSITION
})
})
expect(actions[3]).toEqual({

expect(actions).toContainEqual(expect.objectContaining({
type: SHOW_NOTIFICATION,
payload: expect.objectContaining({
message: 'Transaction failed!',
level: NOTIFICATION_LEVELS.ERROR
level: NOTIFICATION_LEVELS.ERROR,
message: 'Transaction failed!'
})
})
}))
done()
})

test('should dispatch transaction waiting, set claim request and disable claim event', async () => {
const { wrapper, store } = setup()
neonjs.api.neonDB.doSendAsset = jest.fn(() => {
return new Promise((resolve, reject) => {
resolve({ result: true })
})
})

wrapper.dive().find('#claim').simulate('click')
test('should dispatch transaction waiting, set claim request, and disable claim events', async (done) => {
const store = createStore(initialState)
const wrapper = mount(provideStore(<Claim />, store))
neonjs.api.neonDB.doSendAsset = simulateSendAsset(true)

wrapper.find('button#claim').simulate('click')
jest.runAllTimers()
await Promise.resolve().then().then().then()

const actions = store.getActions()
expect(actions.length).toEqual(6)
expect(actions[0]).toEqual({
type: HIDE_NOTIFICATIONS,
payload: expect.objectContaining({
dismissible: true,
position: DEFAULT_POSITION
})
})
expect(actions[1]).toEqual({
type: SHOW_NOTIFICATION,
payload: expect.objectContaining({
message: 'Sending NEO to Yourself...',
level: NOTIFICATION_LEVELS.INFO
})
})

expect(actions[2]).toEqual({
type: HIDE_NOTIFICATIONS,
payload: expect.objectContaining({
dismissible: true,
position: DEFAULT_POSITION
})
})
expect(actions[3]).toEqual({
type: SHOW_NOTIFICATION,
payload: expect.objectContaining({
message: 'Waiting for transaction to clear...',
level: NOTIFICATION_LEVELS.INFO
})
})
expect(actions[4]).toEqual(setClaimRequest(true))
expect(actions[5]).toEqual(disableClaim(true))
expect(actions).toContainEqual(setClaimRequest(true))
expect(actions).toContainEqual(disableClaim(true))
done()
})
})
})
2 changes: 1 addition & 1 deletion __tests__/components/LoginNep2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,6 @@ describe('LoginNep2', () => {

const actions = store.getActions()
expect(actions.length).toEqual(1)
expect(actions[0].type).toEqual('ACCOUNT/REQ/REQUEST')
expect(actions[0].type).toEqual('AUTH/REQ/REQUEST')
})
})
81 changes: 33 additions & 48 deletions __tests__/components/TransactionHistory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ import configureStore from 'redux-mock-store'
import { Provider } from 'react-redux'
import thunk from 'redux-thunk'
import { shallow, mount } from 'enzyme'
import { merge } from 'lodash'

import TransactionHistory from '../../app/containers/TransactionHistory'
import { setTransactionHistory } from '../../app/modules/wallet'
import { setIsLoadingTransaction } from '../../app/modules/transactions'
import { MAIN_NETWORK_ID } from '../../app/core/constants'
import { MAIN_NETWORK_ID, EXPLORERS } from '../../app/core/constants'
import { LOADED } from '../../app/values/state'

const initialState = {
api: {
AUTH: {
address: 'AWy7RNBVr9vDadRMK9p7i7Z1tL7GrLAxoh',
wif: 'L4SLRcPgqNMAMwM3nFSxnh36f1v5omjPg3Ewy1tg2PnEon8AcHou'
},
NETWORK: {
batch: false,
state: LOADED,
Expand All @@ -20,38 +23,30 @@ const initialState = {
SETTINGS: {
batch: false,
state: LOADED,
data: {}
data: {
blockExplorer: EXPLORERS.NEO_TRACKER
}
},
TRANSACTION_HISTORY: {
batch: false,
state: LOADED,
data: []
}
},
account: {
loggedIn: true,
wif: 'L4SLRcPgqNMAMwM3nFSxnh36f1v5omjPg3Ewy1tg2PnEon8AcHou',
address: 'AWy7RNBVr9vDadRMK9p7i7Z1tL7GrLAxoh'
},
wallet: {
transactions: []
},
transactions: {
isLoadingTransactions: false
}
}

const transactions = {
wallet: {
transactions: [
{
NEO: '50',
GAS: '0.00000000',
txid: '76938979'
},
{
NEO: '0',
GAS: '0.40000000',
txid: '76938980'
}
]
const transactions = [
{
NEO: '50',
GAS: '0.00000000',
txid: '76938979'
},
{
NEO: '0',
GAS: '0.40000000',
txid: '76938980'
}
}
]

const setup = (state = initialState, shallowRender = true) => {
const store = configureStore([thunk])(state)
Expand All @@ -74,42 +69,32 @@ const setup = (state = initialState, shallowRender = true) => {
}

describe('TransactionHistory', () => {
test('renders without crashing', (done) => {
test('renders without crashing', () => {
const { wrapper } = setup()
expect(wrapper).toMatchSnapshot()
done()
})

test('calls syncTransactionHistory after rendering', async () => {
const { store } = setup(initialState, false)
await Promise.resolve('Pause').then().then().then()
const actions = store.getActions()
expect(actions[0]).toEqual(setIsLoadingTransaction(true))
expect(actions[1]).toEqual(setIsLoadingTransaction(false))
expect(actions[2]).toEqual(setTransactionHistory(initialState.wallet.transactions))
})

test('correctly renders no transaction history', (done) => {
test('correctly renders no transaction history', () => {
const { wrapper } = setup(initialState, false)

const columnHeader = wrapper.find('#columnHeader')
expect(columnHeader.text()).toEqual('Transaction History ')

const transactionList = wrapper.find('#transactionList')
expect(transactionList.children().length).toEqual(0)
done()
})

test('correctly renders with NEO and GAS transaction history', (done) => {
const transactionState = Object.assign({}, initialState, transactions)
test('correctly renders with NEO and GAS transaction history', () => {
const transactionState = merge({}, initialState, {
api: { TRANSACTION_HISTORY: { data: transactions } }
})
const { wrapper } = setup(transactionState, false)

const transactionList = wrapper.find('#transactionList')
expect(transactionList.children().length).toEqual(2)
expect(transactionList.childAt(0).find('.txid').first().text()).toEqual(transactions.wallet.transactions[0].txid)
expect(transactionList.childAt(1).find('.txid').first().text()).toEqual(transactions.wallet.transactions[1].txid)
expect(transactionList.childAt(0).find('.txid').first().text()).toEqual(transactions[0].txid)
expect(transactionList.childAt(1).find('.txid').first().text()).toEqual(transactions[1].txid)
expect(transactionList.childAt(0).find('.amountNEO').text()).toEqual('50 NEO')
expect(transactionList.childAt(1).find('.amountGAS').text()).toEqual('0.40000000 GAS')
done()
})
})
Loading

0 comments on commit bcb8a4c

Please sign in to comment.