Skip to content
This repository has been archived by the owner on Jun 30, 2021. It is now read-only.

Commit

Permalink
Add Wallet test (#326)
Browse files Browse the repository at this point in the history
* add action test wallet

* add wallet test

* remove 1000
  • Loading branch information
jarindr authored Jul 11, 2018
1 parent 4260f0f commit bc02c4a
Show file tree
Hide file tree
Showing 7 changed files with 257 additions and 94 deletions.
132 changes: 49 additions & 83 deletions apps/admin_panel/assets/src/omg-wallet/action.js
Original file line number Diff line number Diff line change
@@ -1,85 +1,51 @@
import * as walletService from '../services/walletService'
export const getWalletsByAccountId = ({
accountId,
search,
page,
perPage,
cacheKey
}) => async dispatch => {
dispatch({ type: 'WALLETS/REQUEST/INITIATED' })
try {
const result = await walletService.getWalletsByAccountId({
perPage: perPage,
sort: { by: 'created_at', dir: 'asc' },
search_term: search,
accountId,
page
})
if (result.data.success) {
return dispatch({
type: 'WALLETS/REQUEST/SUCCESS',
data: result.data.data.data,
pagination: result.data.data.pagination,
cacheKey
})
} else {
return dispatch({ type: 'WALLETS/REQUEST/FAILED', error: result.data.data })
}
} catch (error) {
return dispatch({ type: 'WALLETS/REQUEST/FAILED', error })
}
}
export const getWallets = ({ search, page, perPage, cacheKey }) => async dispatch => {
dispatch({ type: 'WALLETS/REQUEST/INITIATED' })
try {
const result = await walletService.getWallets({
perPage: perPage,
sort: { by: 'created_at', dir: 'asc' },
search,
page
})
if (result.data.success) {
return dispatch({
type: 'WALLETS/REQUEST/SUCCESS',
data: result.data.data.data,
pagination: result.data.data.pagination,
cacheKey
})
} else {
return dispatch({ type: 'WALLETS/REQUEST/FAILED', error: result.data.data })
}
} catch (error) {
return dispatch({ type: 'WALLETS/REQUEST/FAILED', error })
}
}
export const getWalletsByUserId = ({ userId, search }) => async dispatch => {
dispatch({ type: 'USER_WALLETS/REQUEST/INITIATED' })
try {
const result = await walletService.getWalletsByUserId({
perPage: 1000,
sort: { by: 'created_at', dir: 'asc' },
search_term: search,
userId
})
if (result.data.success) {
return dispatch({ type: 'USER_WALLETS/REQUEST/SUCCESS', data: result.data.data.data })
} else {
return dispatch({ type: 'USER_WALLETS/REQUEST/FAILED', error: result.data.data })
}
} catch (error) {
return dispatch({ type: 'USER_WALLETS/REQUEST/FAILED', error })
}
}
import { createActionCreator, createPaginationActionCreator } from '../utils/createActionCreator'
export const getWalletsByAccountId = ({ accountId, search, page, perPage, cacheKey }) =>
createPaginationActionCreator({
actionName: 'WALLETS',
action: 'REQUEST',
service: async () =>
walletService.getWalletsByAccountId({
perPage: perPage,
sort: { by: 'created_at', dir: 'desc' },
search_term: search,
accountId,
page
}),
cacheKey
})

export const getWalletById = id => async dispatch => {
try {
const result = await walletService.getWallet(id)
if (result.data.success) {
return dispatch({ type: 'WALLET/REQUEST/SUCCESS', wallet: result.data.data })
} else {
return dispatch({ type: 'WALLET/REQUEST/FAILED', error: result.data.data })
}
} catch (error) {
return dispatch({ type: 'WALLET/REQUEST/FAILED', error })
}
}
export const getWallets = ({ search, page, perPage, cacheKey }) =>
createPaginationActionCreator({
actionName: 'WALLETS',
action: 'REQUEST',
service: async () =>
walletService.getWallets({
perPage,
page,
sort: { by: 'created_at', dir: 'desc' },
search
}),
cacheKey
})
export const getWalletsByUserId = ({ userId, perPage, search, page, cacheKey }) =>
createPaginationActionCreator({
actionName: 'USER_WALLETS',
action: 'REQUEST',
service: async () =>
walletService.getWalletsByUserId({
perPage,
page,
sort: { by: 'created_at', dir: 'desc' },
search,
userId
}),
cacheKey
})

export const getWalletById = id =>
createActionCreator({
actionName: 'WALLET',
action: 'REQUEST',
service: async () => walletService.getWallet(id)
})
125 changes: 125 additions & 0 deletions apps/admin_panel/assets/src/omg-wallet/action.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import configureMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'
import { getWalletsByAccountId, getWalletsByUserId, getWalletById, getWallets } from './action'
import * as walletService from '../services/walletService'
const middlewares = [thunk]
const mockStore = configureMockStore(middlewares)
jest.mock('../services/walletService')
let store
describe('wallet actions', () => {
beforeEach(() => {
jest.resetAllMocks()
store = mockStore()
})

test('[getWalletById] should dispatch failed action if get account unsuccessfully', () => {
walletService.getWallet.mockImplementation(() => {
return Promise.resolve({ data: { success: true, data: { id: 'a' } } })
})
const expectedActions = [
{ type: 'WALLET/REQUEST/INITIATED' },
{ type: 'WALLET/REQUEST/SUCCESS', data: { id: 'a' } }
]
return store.dispatch(getWalletById({ id: 'a' })).then(() => {
expect(walletService.getWallet).toBeCalledWith({
id: 'a'
})
expect(store.getActions()).toEqual(expectedActions)
})
})

test('[getWallets] should dispatch success action if get account successfully', () => {
walletService.getWallets.mockImplementation(() => {
return Promise.resolve({
data: {
success: true,
data: { data: 'data', pagination: 'pagination' }
}
})
})
const expectedActions = [
{ type: 'WALLETS/REQUEST/INITIATED' },
{
type: 'WALLETS/REQUEST/SUCCESS',
data: 'data',
pagination: 'pagination',
cacheKey: 'key'
}
]
return store
.dispatch(getWallets({ page: 1, perPage: 10, cacheKey: 'key', search: 'search' }))
.then(() => {
expect(walletService.getWallets).toBeCalledWith(
expect.objectContaining({
page: 1,
perPage: 10,
search: 'search',
sort: { by: 'created_at', dir: 'desc' }
})
)
expect(store.getActions()).toEqual(expectedActions)
})
})
test('[getWalletsByAccountId] should dispatch success action if get account successfully', () => {
walletService.getWalletsByAccountId.mockImplementation(() => {
return Promise.resolve({
data: {
success: true,
data: { data: 'data', pagination: 'pagination' }
}
})
})
const expectedActions = [
{ type: 'WALLETS/REQUEST/INITIATED' },
{
type: 'WALLETS/REQUEST/SUCCESS',
data: 'data',
pagination: 'pagination',
cacheKey: 'key'
}
]
return store
.dispatch(getWalletsByAccountId({ page: 1, perPage: 10, cacheKey: 'key' }))
.then(() => {
expect(walletService.getWalletsByAccountId).toBeCalledWith(
expect.objectContaining({
page: 1,
perPage: 10,
sort: { by: 'created_at', dir: 'desc' }
})
)
expect(store.getActions()).toEqual(expectedActions)
})
})
test('[getWalletsByUserId] should dispatch success action if get account successfully', () => {
walletService.getWalletsByUserId.mockImplementation(() => {
return Promise.resolve({
data: {
success: true,
data: { data: 'data', pagination: 'pagination' }
}
})
})
const expectedActions = [
{ type: 'USER_WALLETS/REQUEST/INITIATED' },
{
type: 'USER_WALLETS/REQUEST/SUCCESS',
data: 'data',
pagination: 'pagination',
cacheKey: 'key'
}
]
return store
.dispatch(getWalletsByUserId({ page: 1, perPage: 10, cacheKey: 'key' }))
.then(() => {
expect(walletService.getWalletsByUserId).toBeCalledWith(
expect.objectContaining({
page: 1,
perPage: 10,
sort: { by: 'created_at', dir: 'desc' }
})
)
expect(store.getActions()).toEqual(expectedActions)
})
})
})
10 changes: 5 additions & 5 deletions apps/admin_panel/assets/src/omg-wallet/reducer.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import createReducer from '../reducer/createReducer'

import _ from 'lodash'
export const walletsReducer = createReducer(
{},
{
'WALLETS/REQUEST/SUCCESS': (state, action) => {
return {...state, ..._.keyBy(action.data, 'address')}
return { ...state, ..._.keyBy(action.data, 'address') }
},
'USER_WALLETS/REQUEST/SUCCESS': (state, action) => {
return {...state, ..._.keyBy(action.data, 'address')}
return { ...state, ..._.keyBy(action.data, 'address') }
},
'WALLET/REQUEST/SUCCESS': (state, { wallet }) => {
return { ...state, ...{ [wallet.address]: wallet } }
'WALLET/REQUEST/SUCCESS': (state, { data }) => {
return { ...state, ...{ [data.address]: data } }
},
'CURRENT_ACCOUNT/SWITCH': () => ({})
}
Expand Down
49 changes: 49 additions & 0 deletions apps/admin_panel/assets/src/omg-wallet/reducer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { walletsReducer } from './reducer'

describe('wallets reducer', () => {
test('should return the initial state', () => {
expect(walletsReducer({}, 'FAKE_ACTION')).toEqual({})
})
test('should return the key by id object with action [WALLETS/REQUEST/SUCCESS]', () => {
expect(
walletsReducer(
{},
{
type: 'WALLETS/REQUEST/SUCCESS',
data: [{ address: '1', data: '1' }]
}
)
).toEqual({
'1': {
address: '1',
data: '1'
}
})
})
test('should return the key by id object with action [USER_WALLETS/REQUEST/SUCCESS]', () => {
expect(
walletsReducer(
{},
{
type: 'USER_WALLETS/REQUEST/SUCCESS',
data: [{ address: '1', data: '1' }]
}
)
).toEqual({
'1': {
address: '1',
data: '1'
}
})
})
test('should reset state when switch account', () => {
expect(
walletsReducer(
{},
{
type: 'CURRENT_ACCOUNT/SWITCH'
}
)
).toEqual({})
})
})
1 change: 1 addition & 0 deletions apps/admin_panel/assets/src/omg-wallet/selector.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createSelectAllPagesCachedQuery } from '../omg-cache/selector'
import { selectCurrentAccount } from '../omg-account-current/selector'
import _ from 'lodash'
export const selectWallets = (state, search) => {
return _.values(state.wallets).filter(wallet => {
const reg = new RegExp(search)
Expand Down
23 changes: 23 additions & 0 deletions apps/admin_panel/assets/src/omg-wallet/selector.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { selectPrimaryWalletCurrentAccount } from './selector'

describe('selectors wallet', () => {
test('should select primary wallet of current account', () => {
const state = {
currentAccount: { id: 'a' },
wallets: {
b: {
account_id: 'a',
identifier: 'primary'
},
c: {
account_id: 'a',
identifier: 'burn'
}
}
}
expect(selectPrimaryWalletCurrentAccount(state)).toEqual({
account_id: 'a',
identifier: 'primary'
})
})
})
11 changes: 5 additions & 6 deletions apps/admin_panel/assets/src/services/walletService.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export function getWallets ({ perPage, sort, search, searchTerms }) {
sort_dir: sort.dir,
search_term: search,
search_terms: searchTerms

}
})
}
Expand All @@ -29,16 +28,17 @@ export function getWalletsByAccountId ({ accountId, perPage, sort, search, ...re
})
}

export function getWalletsByUserId ({ userId, perPage, sort, query, ...rest }) {
export function getWalletsByUserId ({ userId, perPage, page, sort, search, searchTerms }) {
return authenticatedRequest({
path: '/user.get_wallets',
data: {
page,
per_page: perPage,
sort_by: sort.by,
sort_dir: sort.dir,
search_term: query,
id: userId,
...rest
search_term: search,
search_terms: searchTerms,
id: userId
}
})
}
Expand All @@ -51,4 +51,3 @@ export function getWallet (address) {
}
})
}

0 comments on commit bc02c4a

Please sign in to comment.