diff --git a/apps/admin_panel/assets/src/omg-wallet/action.js b/apps/admin_panel/assets/src/omg-wallet/action.js index 033df1e9f..2a62d795c 100644 --- a/apps/admin_panel/assets/src/omg-wallet/action.js +++ b/apps/admin_panel/assets/src/omg-wallet/action.js @@ -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) + }) diff --git a/apps/admin_panel/assets/src/omg-wallet/action.test.js b/apps/admin_panel/assets/src/omg-wallet/action.test.js new file mode 100644 index 000000000..60d590580 --- /dev/null +++ b/apps/admin_panel/assets/src/omg-wallet/action.test.js @@ -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) + }) + }) +}) diff --git a/apps/admin_panel/assets/src/omg-wallet/reducer.js b/apps/admin_panel/assets/src/omg-wallet/reducer.js index 1c1b93b3d..d6615c141 100644 --- a/apps/admin_panel/assets/src/omg-wallet/reducer.js +++ b/apps/admin_panel/assets/src/omg-wallet/reducer.js @@ -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': () => ({}) } diff --git a/apps/admin_panel/assets/src/omg-wallet/reducer.test.js b/apps/admin_panel/assets/src/omg-wallet/reducer.test.js new file mode 100644 index 000000000..2e7af7721 --- /dev/null +++ b/apps/admin_panel/assets/src/omg-wallet/reducer.test.js @@ -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({}) + }) +}) diff --git a/apps/admin_panel/assets/src/omg-wallet/selector.js b/apps/admin_panel/assets/src/omg-wallet/selector.js index 05c38d905..c7166c82c 100644 --- a/apps/admin_panel/assets/src/omg-wallet/selector.js +++ b/apps/admin_panel/assets/src/omg-wallet/selector.js @@ -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) diff --git a/apps/admin_panel/assets/src/omg-wallet/selector.test.js b/apps/admin_panel/assets/src/omg-wallet/selector.test.js new file mode 100644 index 000000000..19df463f7 --- /dev/null +++ b/apps/admin_panel/assets/src/omg-wallet/selector.test.js @@ -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' + }) + }) +}) diff --git a/apps/admin_panel/assets/src/services/walletService.js b/apps/admin_panel/assets/src/services/walletService.js index ebe8008c4..37334005d 100644 --- a/apps/admin_panel/assets/src/services/walletService.js +++ b/apps/admin_panel/assets/src/services/walletService.js @@ -9,7 +9,6 @@ export function getWallets ({ perPage, sort, search, searchTerms }) { sort_dir: sort.dir, search_term: search, search_terms: searchTerms - } }) } @@ -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 } }) } @@ -51,4 +51,3 @@ export function getWallet (address) { } }) } -