This repository has been archived by the owner on Jun 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add action test wallet * add wallet test * remove 1000
- Loading branch information
Showing
7 changed files
with
257 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters