2018-05-21 23:10:51 +00:00
|
|
|
import { runSaga } from 'redux-saga';
|
2018-06-18 01:53:00 +00:00
|
|
|
|
2018-05-21 23:10:51 +00:00
|
|
|
import { translateRaw } from 'translations';
|
2018-06-18 01:53:00 +00:00
|
|
|
import * as testHelpers from 'features/testHelpers';
|
|
|
|
import * as constants from './constants';
|
|
|
|
import * as types from './types';
|
|
|
|
import * as actions from './actions';
|
|
|
|
import * as sagas from './sagas';
|
2018-05-21 23:10:51 +00:00
|
|
|
|
|
|
|
describe('addressBook: Sagas', () => {
|
2018-06-18 01:53:00 +00:00
|
|
|
const initialState = testHelpers.getInitialState();
|
2018-05-21 23:10:51 +00:00
|
|
|
const getState = () => ({
|
|
|
|
...initialState,
|
|
|
|
addressBook: {
|
|
|
|
...initialState.addressBook,
|
|
|
|
addresses: {},
|
|
|
|
labels: {},
|
|
|
|
entries: {}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const id = '0';
|
|
|
|
const address = '0x081f37708032d0a7b3622591a8959b213fb47d6f';
|
|
|
|
const label = 'Foo';
|
|
|
|
|
|
|
|
describe('handleChangeAddressLabelEntry', () => {
|
|
|
|
it('should successfully change an address label entry with no errors', async () => {
|
2018-06-18 01:53:00 +00:00
|
|
|
const action = actions.changeAddressLabelEntry({
|
2018-05-21 23:10:51 +00:00
|
|
|
id,
|
|
|
|
address,
|
|
|
|
label
|
|
|
|
});
|
|
|
|
const dispatched: string[] = [];
|
|
|
|
|
|
|
|
await runSaga(
|
|
|
|
{
|
|
|
|
dispatch: (dispatching: string) => dispatched.push(dispatching),
|
|
|
|
getState
|
|
|
|
},
|
2018-06-18 01:53:00 +00:00
|
|
|
sagas.handleChangeAddressLabelEntry,
|
2018-05-21 23:10:51 +00:00
|
|
|
action
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(dispatched).toEqual([
|
2018-06-18 01:53:00 +00:00
|
|
|
actions.setAddressLabelEntry({
|
2018-05-21 23:10:51 +00:00
|
|
|
id,
|
|
|
|
address,
|
|
|
|
temporaryAddress: address,
|
|
|
|
addressError: undefined,
|
|
|
|
label,
|
|
|
|
temporaryLabel: label,
|
|
|
|
labelError: undefined
|
|
|
|
})
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
it('should change a temporary address and error when an error occurs, but not the address', async () => {
|
2018-06-18 01:53:00 +00:00
|
|
|
const action = actions.changeAddressLabelEntry({
|
2018-05-21 23:10:51 +00:00
|
|
|
id,
|
|
|
|
address: '0', // Invalid ETH address
|
|
|
|
label
|
|
|
|
});
|
|
|
|
const dispatched: string[] = [];
|
|
|
|
|
|
|
|
await runSaga(
|
|
|
|
{
|
|
|
|
dispatch: (dispatching: string) => dispatched.push(dispatching),
|
|
|
|
getState
|
|
|
|
},
|
2018-06-18 01:53:00 +00:00
|
|
|
sagas.handleChangeAddressLabelEntry,
|
2018-05-21 23:10:51 +00:00
|
|
|
action
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(dispatched).toEqual([
|
2018-06-18 01:53:00 +00:00
|
|
|
actions.setAddressLabelEntry({
|
2018-05-21 23:10:51 +00:00
|
|
|
id,
|
|
|
|
address: '',
|
|
|
|
temporaryAddress: '0',
|
|
|
|
addressError: translateRaw('INVALID_ADDRESS'),
|
|
|
|
label,
|
|
|
|
temporaryLabel: label,
|
|
|
|
labelError: undefined
|
|
|
|
})
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
it('should change a temporary label and error when an error occurs, but not the label', async () => {
|
2018-06-18 01:53:00 +00:00
|
|
|
const action = actions.changeAddressLabelEntry({
|
2018-05-21 23:10:51 +00:00
|
|
|
id,
|
|
|
|
address,
|
|
|
|
label: 'F' // Invalid label length
|
|
|
|
});
|
|
|
|
const dispatched: string[] = [];
|
|
|
|
|
|
|
|
await runSaga(
|
|
|
|
{
|
|
|
|
dispatch: (dispatching: string) => dispatched.push(dispatching),
|
|
|
|
getState: () => getState()
|
|
|
|
},
|
2018-06-18 01:53:00 +00:00
|
|
|
sagas.handleChangeAddressLabelEntry,
|
2018-05-21 23:10:51 +00:00
|
|
|
action
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(dispatched).toEqual([
|
2018-06-18 01:53:00 +00:00
|
|
|
actions.setAddressLabelEntry({
|
2018-05-21 23:10:51 +00:00
|
|
|
id,
|
|
|
|
address,
|
|
|
|
temporaryAddress: address,
|
|
|
|
addressError: undefined,
|
|
|
|
label: '',
|
|
|
|
temporaryLabel: 'F',
|
|
|
|
labelError: translateRaw('INVALID_LABEL_LENGTH')
|
|
|
|
})
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('handleSaveAddressLabelEntry', () => {
|
|
|
|
it('should flash an error without saving when an address or label error exists', async () => {
|
|
|
|
const state = {
|
|
|
|
...getState(),
|
|
|
|
addressBook: {
|
|
|
|
...getState().addressBook,
|
|
|
|
entries: {
|
2018-06-18 01:53:00 +00:00
|
|
|
[constants.ADDRESS_BOOK_TABLE_ID]: {
|
|
|
|
id: constants.ADDRESS_BOOK_TABLE_ID,
|
2018-05-21 23:10:51 +00:00
|
|
|
address: '0',
|
|
|
|
temporaryAddress: '0',
|
|
|
|
addressError: translateRaw('INVALID_ADDRESS'),
|
|
|
|
label,
|
|
|
|
temporaryLabel: label,
|
|
|
|
labelError: undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-06-18 01:53:00 +00:00
|
|
|
const action = actions.saveAddressLabelEntry(constants.ADDRESS_BOOK_TABLE_ID);
|
2018-05-21 23:10:51 +00:00
|
|
|
const dispatched: string[] = [];
|
|
|
|
|
|
|
|
await runSaga(
|
|
|
|
{
|
|
|
|
dispatch: (dispatching: string) => dispatched.push(dispatching),
|
|
|
|
getState: () => state
|
|
|
|
},
|
2018-06-18 01:53:00 +00:00
|
|
|
sagas.handleSaveAddressLabelEntry,
|
2018-05-21 23:10:51 +00:00
|
|
|
action
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(dispatched.length).toEqual(1);
|
|
|
|
});
|
|
|
|
it('should successfully create a new entry and clear the temporary entry', async () => {
|
|
|
|
const state = {
|
|
|
|
...getState(),
|
|
|
|
addressBook: {
|
|
|
|
...getState().addressBook,
|
|
|
|
entries: {
|
2018-06-18 01:53:00 +00:00
|
|
|
[constants.ADDRESS_BOOK_TABLE_ID]: {
|
|
|
|
id: constants.ADDRESS_BOOK_TABLE_ID,
|
2018-05-21 23:10:51 +00:00
|
|
|
address,
|
|
|
|
temporaryAddress: address,
|
|
|
|
label,
|
|
|
|
temporaryLabel: label
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-06-18 01:53:00 +00:00
|
|
|
const action = actions.saveAddressLabelEntry(constants.ADDRESS_BOOK_TABLE_ID);
|
2018-05-21 23:10:51 +00:00
|
|
|
const dispatched: string[] = [];
|
|
|
|
|
|
|
|
await runSaga(
|
|
|
|
{
|
|
|
|
dispatch: (dispatching: string) => dispatched.push(dispatching),
|
|
|
|
getState: () => state
|
|
|
|
},
|
2018-06-18 01:53:00 +00:00
|
|
|
sagas.handleSaveAddressLabelEntry,
|
2018-05-21 23:10:51 +00:00
|
|
|
action
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(dispatched).toEqual([
|
2018-06-18 01:53:00 +00:00
|
|
|
actions.clearAddressLabel(address),
|
|
|
|
actions.setAddressLabel({
|
2018-05-21 23:10:51 +00:00
|
|
|
address,
|
|
|
|
label
|
|
|
|
}),
|
2018-06-18 01:53:00 +00:00
|
|
|
actions.setAddressLabelEntry({
|
2018-05-21 23:10:51 +00:00
|
|
|
id: '1',
|
|
|
|
address,
|
|
|
|
temporaryAddress: address,
|
|
|
|
addressError: undefined,
|
|
|
|
label,
|
|
|
|
temporaryLabel: label,
|
|
|
|
labelError: undefined
|
|
|
|
}),
|
2018-06-18 01:53:00 +00:00
|
|
|
actions.setAddressLabelEntry({
|
|
|
|
id: constants.ADDRESS_BOOK_TABLE_ID,
|
2018-05-21 23:10:51 +00:00
|
|
|
address: '',
|
|
|
|
temporaryAddress: '',
|
|
|
|
addressError: undefined,
|
|
|
|
label: '',
|
|
|
|
temporaryLabel: '',
|
|
|
|
labelError: undefined
|
|
|
|
})
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
it('should successfully overwrite an existing address label entry with no errors', async () => {
|
|
|
|
const state = {
|
|
|
|
...getState(),
|
|
|
|
addressBook: {
|
|
|
|
...getState().addressBook,
|
|
|
|
entries: {
|
|
|
|
[id]: {
|
|
|
|
id,
|
|
|
|
address,
|
|
|
|
temporaryAddress: address,
|
|
|
|
label,
|
|
|
|
temporaryLabel: label
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-06-18 01:53:00 +00:00
|
|
|
const action = actions.saveAddressLabelEntry(id);
|
2018-05-21 23:10:51 +00:00
|
|
|
const dispatched: string[] = [];
|
|
|
|
|
|
|
|
await runSaga(
|
|
|
|
{
|
|
|
|
dispatch: (dispatching: string) => dispatched.push(dispatching),
|
|
|
|
getState: () => state
|
|
|
|
},
|
2018-06-18 01:53:00 +00:00
|
|
|
sagas.handleSaveAddressLabelEntry,
|
2018-05-21 23:10:51 +00:00
|
|
|
action
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(dispatched).toEqual([
|
2018-06-18 01:53:00 +00:00
|
|
|
actions.clearAddressLabel(address),
|
|
|
|
actions.setAddressLabel({
|
2018-05-21 23:10:51 +00:00
|
|
|
address,
|
|
|
|
label
|
|
|
|
}),
|
2018-06-18 01:53:00 +00:00
|
|
|
actions.setAddressLabelEntry({
|
2018-05-21 23:10:51 +00:00
|
|
|
id,
|
|
|
|
address,
|
|
|
|
temporaryAddress: address,
|
|
|
|
addressError: undefined,
|
|
|
|
label: 'Foo',
|
|
|
|
temporaryLabel: 'Foo',
|
|
|
|
labelError: undefined
|
|
|
|
})
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('handleRemoveAddressLabelEntry', () => {
|
|
|
|
it('should simply return if the requested entry is non-existent', async () => {
|
2018-06-18 01:53:00 +00:00
|
|
|
const action = actions.removeAddressLabelEntry('Foo');
|
|
|
|
const dispatched: types.AddressLabel[] = [];
|
2018-05-21 23:10:51 +00:00
|
|
|
|
|
|
|
await runSaga(
|
|
|
|
{
|
2018-06-18 01:53:00 +00:00
|
|
|
dispatch: (dispatching: types.AddressLabel) => dispatched.push(dispatching),
|
2018-05-21 23:10:51 +00:00
|
|
|
getState: () => getState()
|
|
|
|
},
|
2018-06-18 01:53:00 +00:00
|
|
|
sagas.handleRemoveAddressLabelEntry,
|
2018-05-21 23:10:51 +00:00
|
|
|
action
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(dispatched.length).toEqual(0);
|
|
|
|
});
|
|
|
|
it('should remove the address, the label, and the entry from the book', async () => {
|
|
|
|
const state = {
|
|
|
|
...getState(),
|
|
|
|
addressBook: {
|
|
|
|
...getState().addressBook,
|
|
|
|
addresses: {
|
|
|
|
[address]: label
|
|
|
|
},
|
|
|
|
labels: {
|
|
|
|
label: address
|
|
|
|
},
|
|
|
|
entries: {
|
|
|
|
[id]: {
|
|
|
|
id,
|
|
|
|
address,
|
|
|
|
temporaryAddress: address,
|
|
|
|
addressError: undefined,
|
|
|
|
label,
|
|
|
|
temporaryLabel: label,
|
|
|
|
labelError: undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-06-18 01:53:00 +00:00
|
|
|
const action = actions.removeAddressLabelEntry(id);
|
|
|
|
const dispatched: types.AddressLabel[] = [];
|
2018-05-21 23:10:51 +00:00
|
|
|
|
|
|
|
await runSaga(
|
|
|
|
{
|
2018-06-18 01:53:00 +00:00
|
|
|
dispatch: (dispatching: types.AddressLabel) => dispatched.push(dispatching),
|
2018-05-21 23:10:51 +00:00
|
|
|
getState: () => state
|
|
|
|
},
|
2018-06-18 01:53:00 +00:00
|
|
|
sagas.handleRemoveAddressLabelEntry,
|
2018-05-21 23:10:51 +00:00
|
|
|
action
|
|
|
|
);
|
|
|
|
|
2018-06-18 01:53:00 +00:00
|
|
|
expect(dispatched).toEqual([
|
|
|
|
actions.clearAddressLabel(address),
|
|
|
|
actions.clearAddressLabelEntry(id)
|
|
|
|
]);
|
2018-05-21 23:10:51 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|