2017-12-11 17:44:53 +00:00
|
|
|
import { swap, INITIAL_STATE } from 'reducers/swap';
|
2017-08-24 08:53:59 +00:00
|
|
|
import * as swapActions from 'actions/swap';
|
2017-12-11 17:44:53 +00:00
|
|
|
import { NormalizedBityRates, NormalizedOptions } from 'reducers/swap/types';
|
|
|
|
import { normalize } from 'normalizr';
|
|
|
|
import * as schema from 'reducers/swap/schema';
|
2017-08-24 08:53:59 +00:00
|
|
|
|
|
|
|
describe('swap reducer', () => {
|
2017-12-11 17:44:53 +00:00
|
|
|
const apiResponse = {
|
|
|
|
BTCETH: {
|
|
|
|
id: 'BTCETH',
|
|
|
|
options: [{ id: 'BTC' }, { id: 'ETH' }],
|
|
|
|
rate: 23.27855114
|
|
|
|
},
|
|
|
|
ETHBTC: {
|
|
|
|
id: 'ETHBTC',
|
|
|
|
options: [{ id: 'ETH' }, { id: 'BTC' }],
|
|
|
|
rate: 0.042958
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const normalizedbityRates: NormalizedBityRates = {
|
|
|
|
byId: normalize(apiResponse, [schema.bityRate]).entities.bityRates,
|
|
|
|
allIds: schema.allIds(normalize(apiResponse, [schema.bityRate]).entities.bityRates)
|
|
|
|
};
|
|
|
|
const normalizedOptions: NormalizedOptions = {
|
|
|
|
byId: normalize(apiResponse, [schema.bityRate]).entities.options,
|
|
|
|
allIds: schema.allIds(normalize(apiResponse, [schema.bityRate]).entities.options)
|
|
|
|
};
|
2017-08-24 08:53:59 +00:00
|
|
|
it('should handle SWAP_LOAD_BITY_RATES_SUCCEEDED', () => {
|
2017-12-11 17:44:53 +00:00
|
|
|
expect(swap(undefined, swapActions.loadBityRatesSucceededSwap(apiResponse))).toEqual({
|
2017-08-24 08:53:59 +00:00
|
|
|
...INITIAL_STATE,
|
|
|
|
isFetchingRates: false,
|
2017-12-11 17:44:53 +00:00
|
|
|
bityRates: normalizedbityRates,
|
|
|
|
options: normalizedOptions
|
2017-08-24 08:53:59 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle SWAP_STEP', () => {
|
|
|
|
const step = 2;
|
|
|
|
expect(swap(undefined, swapActions.changeStepSwap(step))).toEqual({
|
|
|
|
...INITIAL_STATE,
|
|
|
|
step
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle SWAP_DESTINATION_ADDRESS', () => {
|
|
|
|
const destinationAddress = '341a0sdf83';
|
2017-12-11 17:44:53 +00:00
|
|
|
expect(swap(undefined, swapActions.destinationAddressSwap(destinationAddress))).toEqual({
|
2017-08-24 08:53:59 +00:00
|
|
|
...INITIAL_STATE,
|
|
|
|
destinationAddress
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle SWAP_RESTART', () => {
|
|
|
|
expect(
|
|
|
|
swap(
|
|
|
|
{
|
|
|
|
...INITIAL_STATE,
|
2017-12-11 17:44:53 +00:00
|
|
|
bityRates: normalizedbityRates,
|
|
|
|
origin: { id: 'BTC', amount: 1 },
|
|
|
|
destination: { id: 'ETH', amount: 3 }
|
2017-08-24 08:53:59 +00:00
|
|
|
},
|
|
|
|
swapActions.restartSwap()
|
|
|
|
)
|
|
|
|
).toEqual({
|
|
|
|
...INITIAL_STATE,
|
2017-12-11 17:44:53 +00:00
|
|
|
bityRates: normalizedbityRates
|
2017-08-24 08:53:59 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle SWAP_ORDER_CREATE_REQUESTED', () => {
|
2017-10-25 02:17:26 +00:00
|
|
|
expect(
|
|
|
|
swap(undefined, {
|
|
|
|
type: 'SWAP_ORDER_CREATE_REQUESTED'
|
|
|
|
} as swapActions.SwapAction)
|
|
|
|
).toEqual({
|
2017-08-24 08:53:59 +00:00
|
|
|
...INITIAL_STATE,
|
|
|
|
isPostingOrder: true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle SWAP_ORDER_CREATE_FAILED', () => {
|
2017-10-25 02:17:26 +00:00
|
|
|
expect(
|
|
|
|
swap(undefined, {
|
|
|
|
type: 'SWAP_ORDER_CREATE_FAILED'
|
|
|
|
} as swapActions.SwapAction)
|
|
|
|
).toEqual({
|
2017-08-24 08:53:59 +00:00
|
|
|
...INITIAL_STATE,
|
|
|
|
isPostingOrder: false
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-11-17 21:12:27 +00:00
|
|
|
it('should handle SWAP_BITY_ORDER_CREATE_SUCCEEDED', () => {
|
|
|
|
const mockedBityOrder: swapActions.BityOrderPostResponse = {
|
|
|
|
payment_address: 'payment_address',
|
|
|
|
status: 'status',
|
|
|
|
input: {
|
|
|
|
amount: '1.111',
|
|
|
|
currency: 'input_currency',
|
|
|
|
reference: 'input_reference',
|
|
|
|
status: 'input_status'
|
|
|
|
},
|
|
|
|
output: {
|
|
|
|
amount: '1.111',
|
|
|
|
currency: 'output_currency',
|
|
|
|
reference: 'output_reference',
|
|
|
|
status: 'output_status'
|
|
|
|
},
|
|
|
|
timestamp_created: 'timestamp_created',
|
|
|
|
validFor: 0,
|
|
|
|
id: 'id'
|
|
|
|
};
|
|
|
|
|
2017-12-11 17:44:53 +00:00
|
|
|
expect(swap(undefined, swapActions.bityOrderCreateSucceededSwap(mockedBityOrder))).toEqual({
|
2017-11-17 21:12:27 +00:00
|
|
|
...INITIAL_STATE,
|
|
|
|
bityOrder: {
|
|
|
|
...mockedBityOrder
|
|
|
|
},
|
|
|
|
isPostingOrder: false,
|
|
|
|
originAmount: parseFloat(mockedBityOrder.input.amount),
|
|
|
|
destinationAmount: parseFloat(mockedBityOrder.output.amount),
|
|
|
|
secondsRemaining: mockedBityOrder.validFor,
|
|
|
|
validFor: mockedBityOrder.validFor,
|
|
|
|
orderTimestampCreatedISOString: mockedBityOrder.timestamp_created,
|
|
|
|
paymentAddress: mockedBityOrder.payment_address,
|
|
|
|
orderStatus: mockedBityOrder.status,
|
|
|
|
orderId: mockedBityOrder.id
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle SWAP_BITY_ORDER_STATUS_SUCCEEDED', () => {
|
|
|
|
const mockedBityResponse: swapActions.BityOrderResponse = {
|
|
|
|
input: {
|
|
|
|
amount: '1.111',
|
|
|
|
currency: 'input_currency',
|
|
|
|
reference: 'input_reference',
|
|
|
|
status: 'input_status'
|
|
|
|
},
|
|
|
|
output: {
|
|
|
|
amount: '1.111',
|
|
|
|
currency: 'output_currency',
|
|
|
|
reference: 'output_reference',
|
|
|
|
status: 'FILL'
|
|
|
|
},
|
|
|
|
status: 'status'
|
|
|
|
};
|
|
|
|
|
2017-12-11 17:44:53 +00:00
|
|
|
expect(swap(undefined, swapActions.orderStatusSucceededSwap(mockedBityResponse))).toEqual({
|
2017-11-17 21:12:27 +00:00
|
|
|
...INITIAL_STATE,
|
|
|
|
outputTx: mockedBityResponse.output.reference,
|
|
|
|
orderStatus: mockedBityResponse.output.status
|
|
|
|
});
|
|
|
|
});
|
2017-08-24 08:53:59 +00:00
|
|
|
|
|
|
|
it('should handle SWAP_ORDER_TIME', () => {
|
|
|
|
const secondsRemaining = 300;
|
2017-12-11 17:44:53 +00:00
|
|
|
expect(swap(undefined, swapActions.orderTimeSwap(secondsRemaining))).toEqual({
|
2017-08-24 08:53:59 +00:00
|
|
|
...INITIAL_STATE,
|
|
|
|
secondsRemaining
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle SWAP_LOAD_BITY_RATES_REQUESTED', () => {
|
|
|
|
expect(
|
2017-10-25 02:17:26 +00:00
|
|
|
swap(undefined, {
|
|
|
|
type: 'SWAP_LOAD_BITY_RATES_REQUESTED'
|
|
|
|
} as swapActions.SwapAction)
|
2017-08-24 08:53:59 +00:00
|
|
|
).toEqual({
|
|
|
|
...INITIAL_STATE,
|
|
|
|
isFetchingRates: true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle SWAP_STOP_LOAD_BITY_RATES', () => {
|
2017-10-25 02:17:26 +00:00
|
|
|
expect(
|
|
|
|
swap(undefined, {
|
|
|
|
type: 'SWAP_STOP_LOAD_BITY_RATES'
|
|
|
|
} as swapActions.SwapAction)
|
|
|
|
).toEqual({
|
2017-08-24 08:53:59 +00:00
|
|
|
...INITIAL_STATE,
|
|
|
|
isFetchingRates: false
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|