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 dummy test * actions test * add action test for token * add reducer test * change reducer concept * fix typo
- Loading branch information
Showing
6 changed files
with
282 additions
and
116 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
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,110 +1,63 @@ | ||
import * as tokenSerivce from '../services/tokenService' | ||
export const createToken = ({ name, symbol, decimal, amount }) => async dispatch => { | ||
dispatch({ type: 'TOKEN/CREATE/INITIATED' }) | ||
try { | ||
const result = await tokenSerivce.createToken({ | ||
name, | ||
symbol, | ||
decimal, | ||
amount | ||
}) | ||
if (result.data.success) { | ||
return dispatch({ type: 'TOKEN/CREATE/SUCCESS', data: result.data.data }) | ||
} else { | ||
return dispatch({ type: 'TOKEN/CREATE/FAILED', error: result.data.data }) | ||
} | ||
} catch (error) { | ||
return dispatch({ type: 'TOKEN/CREATE/FAILED', error }) | ||
} | ||
} | ||
export const mintToken = ({ id, amount }) => async dispatch => { | ||
try { | ||
const result = await tokenSerivce.mintToken({ | ||
id, | ||
amount | ||
}) | ||
if (result.data.success) { | ||
dispatch({ type: 'TOKEN/MINT/SUCCESS', data: result.data.data }) | ||
} else { | ||
dispatch({ type: 'TOKEN/MINT/FAILED', error: result.data.data }) | ||
} | ||
return result | ||
} catch (error) { | ||
return dispatch({ type: 'TOKEN/MINT/FAILED', error }) | ||
} | ||
} | ||
|
||
export const getTokens = ({ search, page, perPage, cacheKey }) => async dispatch => { | ||
dispatch({ type: 'TOKENS/REQUEST/INITIATED' }) | ||
try { | ||
const result = await tokenSerivce.getAllTokens({ | ||
perPage: perPage, | ||
page, | ||
sort: { by: 'created_at', dir: 'desc' }, | ||
search | ||
}) | ||
if (result.data.success) { | ||
return dispatch({ | ||
type: 'TOKENS/REQUEST/SUCCESS', | ||
data: result.data.data.data, | ||
pagination: result.data.data.pagination, | ||
cacheKey | ||
}) | ||
} else { | ||
return dispatch({ type: 'TOKENS/REQUEST/FAILED', error: result.data.data }) | ||
} | ||
} catch (error) { | ||
console.log(error) | ||
return dispatch({ type: 'TOKENS/REQUEST/FAILED', error }) | ||
} | ||
} | ||
export const getMintedTokenHistory = ({ | ||
tokenId, | ||
search, | ||
page, | ||
perPage, | ||
searchTerms, | ||
cacheKey | ||
}) => async dispatch => { | ||
dispatch({ type: 'TOKENS/REQUEST/INITIATED' }) | ||
try { | ||
const result = await tokenSerivce.getMintedTokenHistory({ | ||
perPage, | ||
page, | ||
sort: { by: 'created_at', dir: 'desc' }, | ||
search, | ||
searchTerms, | ||
tokenId | ||
}) | ||
if (result.data.success) { | ||
return dispatch({ | ||
type: 'TOKEN_HISTORY/REQUEST/SUCCESS', | ||
data: result.data.data.data, | ||
pagination: result.data.data.pagination, | ||
cacheKey | ||
import * as tokenService from '../services/tokenService' | ||
import { createActionCreator, createPaginationActionCreator } from '../utils/createActionCreator' | ||
export const createToken = ({ name, symbol, decimal, amount }) => | ||
createActionCreator({ | ||
actionName: 'TOKEN', | ||
action: 'CREATE', | ||
service: () => | ||
tokenService.createToken({ | ||
name, | ||
symbol, | ||
decimal, | ||
amount | ||
}) | ||
} else { | ||
return dispatch({ type: 'TOKEN_HISTORY/REQUEST/FAILED', error: result.data.data }) | ||
} | ||
} catch (error) { | ||
console.log(error) | ||
return dispatch({ type: 'TOKEN_HISTORY/REQUEST/FAILED', error }) | ||
} | ||
} | ||
}) | ||
|
||
export const getTokenById = id => async dispatch => { | ||
try { | ||
const result = await tokenSerivce.getTokenStatsById(id) | ||
if (result.data.success) { | ||
dispatch({ | ||
type: 'TOKEN/REQUEST/SUCCESS', | ||
data: { ...result.data.data.token, total_supply: result.data.data.total_supply } | ||
export const mintToken = ({ id, amount }) => | ||
createActionCreator({ | ||
actionName: 'TOKEN', | ||
action: 'MINT', | ||
service: () => | ||
tokenService.mintToken({ | ||
id, | ||
amount | ||
}) | ||
} else { | ||
dispatch({ type: 'TOKEN/REQUEST/FAILED', error: result.data.data }) | ||
} | ||
return result | ||
} catch (error) { | ||
return dispatch({ type: 'TOKEN/REQUEST/FAILED', error }) | ||
} | ||
} | ||
}) | ||
|
||
export const getTokens = ({ search, page, perPage, cacheKey, searchTerms }) => | ||
createPaginationActionCreator({ | ||
actionName: 'TOKENS', | ||
action: 'REQUEST', | ||
service: () => | ||
tokenService.getAllTokens({ | ||
perPage, | ||
page, | ||
searchTerms, | ||
sort: { by: 'created_at', dir: 'desc' }, | ||
search | ||
}), | ||
cacheKey | ||
}) | ||
|
||
export const getMintedTokenHistory = ({ tokenId, search, page, perPage, searchTerms, cacheKey }) => | ||
createPaginationActionCreator({ | ||
actionName: 'TOKEN_HISTORY', | ||
action: 'REQUEST', | ||
service: () => | ||
tokenService.getMintedTokenHistory({ | ||
perPage, | ||
page, | ||
sort: { by: 'created_at', dir: 'desc' }, | ||
search, | ||
searchTerms, | ||
tokenId | ||
}), | ||
cacheKey | ||
}) | ||
|
||
export const getTokenById = id => | ||
createActionCreator({ | ||
actionName: 'TOKEN', | ||
action: 'REQUEST', | ||
service: () => tokenService.getTokenStatsById(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,130 @@ | ||
import configureMockStore from 'redux-mock-store' | ||
import thunk from 'redux-thunk' | ||
import { createToken, mintToken, getMintedTokenHistory, getTokenById, getTokens } from './action' | ||
import * as tokenService from '../services/tokenService' | ||
const middlewares = [thunk] | ||
const mockStore = configureMockStore(middlewares) | ||
jest.mock('../services/tokenService') | ||
let store | ||
describe('token actions', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks() | ||
store = mockStore() | ||
}) | ||
test('[createToken] should dispatch success action if successfully create token', () => { | ||
tokenService.createToken.mockImplementation(() => { | ||
return Promise.resolve({ data: { success: true, data: 'data' } }) | ||
}) | ||
const expectedActions = [ | ||
{ type: 'TOKEN/CREATE/INITIATED' }, | ||
{ type: 'TOKEN/CREATE/SUCCESS', data: 'data' } | ||
] | ||
return store | ||
.dispatch(createToken({ name: 'name', symbol: 'symbol', decimal: 'decimal', amount: '50' })) | ||
.then(() => { | ||
expect(tokenService.createToken).toBeCalledWith({ | ||
name: 'name', | ||
symbol: 'symbol', | ||
decimal: 'decimal', | ||
amount: '50' | ||
}) | ||
expect(store.getActions()).toEqual(expectedActions) | ||
}) | ||
}) | ||
|
||
test('[mintToken] should dispatch success action if successfully mintToken', () => { | ||
tokenService.mintToken.mockImplementation(() => { | ||
return Promise.resolve({ data: { success: true, data: 'data' } }) | ||
}) | ||
const expectedActions = [ | ||
{ type: 'TOKEN/MINT/INITIATED' }, | ||
{ type: 'TOKEN/MINT/SUCCESS', data: 'data' } | ||
] | ||
return store.dispatch(mintToken({ id: 'id', amount: '50' })).then(() => { | ||
expect(tokenService.mintToken).toBeCalledWith({ id: 'id', amount: '50' }) | ||
expect(store.getActions()).toEqual(expectedActions) | ||
}) | ||
}) | ||
|
||
test('[getMintedTokenHistory] should dispatch success action if successfully getMintedTokenHistory', () => { | ||
tokenService.getMintedTokenHistory.mockImplementation(() => { | ||
return Promise.resolve({ | ||
data: { | ||
success: true, | ||
data: { data: 'data', pagination: 'pagination' } | ||
} | ||
}) | ||
}) | ||
const expectedActions = [ | ||
{ type: 'TOKEN_HISTORY/REQUEST/INITIATED' }, | ||
{ | ||
type: 'TOKEN_HISTORY/REQUEST/SUCCESS', | ||
data: 'data', | ||
pagination: 'pagination', | ||
cacheKey: 'key' | ||
} | ||
] | ||
return store | ||
.dispatch(getMintedTokenHistory({ page: 1, perPage: 10, cacheKey: 'key', search: 'search' })) | ||
.then(() => { | ||
expect(tokenService.getMintedTokenHistory).toBeCalledWith( | ||
expect.objectContaining({ | ||
page: 1, | ||
perPage: 10, | ||
sort: { by: 'created_at', dir: 'desc' }, | ||
search: 'search' | ||
}) | ||
) | ||
expect(store.getActions()).toEqual(expectedActions) | ||
}) | ||
}) | ||
|
||
test('[getTokens] should dispatch success action if successfully getTokens', () => { | ||
tokenService.getAllTokens.mockImplementation(() => { | ||
return Promise.resolve({ | ||
data: { | ||
success: true, | ||
data: { data: 'data', pagination: 'pagination' } | ||
} | ||
}) | ||
}) | ||
const expectedActions = [ | ||
{ type: 'TOKENS/REQUEST/INITIATED' }, | ||
{ | ||
type: 'TOKENS/REQUEST/SUCCESS', | ||
data: 'data', | ||
pagination: 'pagination', | ||
cacheKey: 'key' | ||
} | ||
] | ||
return store | ||
.dispatch(getTokens({ page: 1, perPage: 10, cacheKey: 'key', search: 'search' })) | ||
.then(() => { | ||
expect(tokenService.getAllTokens).toBeCalledWith( | ||
expect.objectContaining({ | ||
page: 1, | ||
perPage: 10, | ||
sort: { by: 'created_at', dir: 'desc' }, | ||
search: 'search' | ||
}) | ||
) | ||
expect(store.getActions()).toEqual(expectedActions) | ||
}) | ||
}) | ||
|
||
test('[getTokenById] should dispatch success action if successfully getTokenById', () => { | ||
tokenService.getTokenStatsById.mockImplementation(() => { | ||
return Promise.resolve({ | ||
data: { success: true, data: { token: { id: '1' }, total_supply: 1 } } | ||
}) | ||
}) | ||
const expectedActions = [ | ||
{ type: 'TOKEN/REQUEST/INITIATED' }, | ||
{ type: 'TOKEN/REQUEST/SUCCESS', data: { token: { id: '1' }, total_supply: 1 } } | ||
] | ||
return store.dispatch(getTokenById('id')).then(() => { | ||
expect(tokenService.getTokenStatsById).toBeCalledWith('id') | ||
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
Oops, something went wrong.