1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Return Promise from addToAddressBook thunk (#8450)

`addToAddressBook` returned a thunk that didn't return a Promise,
despite doing async work. It now returns a Promise.

The callers of this action creator were updated to `await` the
completion of the operation. It was called just before redirecting the
user to a different page or closing a modal, and it seemed appropriate
to wait before doing those things.
This commit is contained in:
Mark Stacey 2020-04-29 01:59:49 -03:00 committed by GitHub
parent c011c0406b
commit e20ca4668e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 33 additions and 34 deletions

View File

@ -1131,10 +1131,11 @@ describe('Actions', function () {
}) })
describe('#addToAddressBook', function () { describe('#addToAddressBook', function () {
it('calls setAddressBook', function () { it('calls setAddressBook', async function () {
const addToAddressBookSpy = sinon.stub(background, 'setAddressBook') const addToAddressBookSpy = sinon.stub(background, 'setAddressBook')
.callsArgWith(4, null, true)
const store = mockStore() const store = mockStore()
store.dispatch(actions.addToAddressBook('test')) await store.dispatch(actions.addToAddressBook('test'))
assert(addToAddressBookSpy.calledOnce) assert(addToAddressBookSpy.calledOnce)
addToAddressBookSpy.restore() addToAddressBookSpy.restore()
}) })

View File

@ -18,9 +18,9 @@ export default class AddToAddressBookModal extends Component {
alias: '', alias: '',
} }
onSave = () => { onSave = async () => {
const { recipient, addToAddressBook, hideModal } = this.props const { recipient, addToAddressBook, hideModal } = this.props
addToAddressBook(recipient, this.state.alias) await addToAddressBook(recipient, this.state.alias)
hideModal() hideModal()
} }
@ -30,7 +30,7 @@ export default class AddToAddressBookModal extends Component {
}) })
} }
onKeyPress = (e) => { onKeyPress = async (e) => {
if (e.key === 'Enter' && this.state.alias) { if (e.key === 'Enter' && this.state.alias) {
this.onSave() this.onSave()
} }

View File

@ -39,7 +39,7 @@ export default class SendFooter extends Component {
this.props.history.push(DEFAULT_ROUTE) this.props.history.push(DEFAULT_ROUTE)
} }
onSubmit (event) { async onSubmit (event) {
event.preventDefault() event.preventDefault()
const { const {
addToAddressBookIfNew, addToAddressBookIfNew,
@ -69,7 +69,7 @@ export default class SendFooter extends Component {
// } // }
// TODO: add nickname functionality // TODO: add nickname functionality
addToAddressBookIfNew(to, toAccounts) await addToAddressBookIfNew(to, toAccounts)
const promise = editingTransactionId const promise = editingTransactionId
? update({ ? update({
amount, amount,

View File

@ -145,8 +145,8 @@ describe('SendFooter Component', function () {
) )
}) })
it('should call props.update if editingTransactionId is truthy', function () { it('should call props.update if editingTransactionId is truthy', async function () {
wrapper.instance().onSubmit(MOCK_EVENT) await wrapper.instance().onSubmit(MOCK_EVENT)
assert(propsMethodSpies.update.calledOnce) assert(propsMethodSpies.update.calledOnce)
assert.deepEqual( assert.deepEqual(
propsMethodSpies.update.getCall(0).args[0], propsMethodSpies.update.getCall(0).args[0],
@ -168,9 +168,9 @@ describe('SendFooter Component', function () {
assert.equal(propsMethodSpies.sign.callCount, 0) assert.equal(propsMethodSpies.sign.callCount, 0)
}) })
it('should call props.sign if editingTransactionId is falsy', function () { it('should call props.sign if editingTransactionId is falsy', async function () {
wrapper.setProps({ editingTransactionId: null }) wrapper.setProps({ editingTransactionId: null })
wrapper.instance().onSubmit(MOCK_EVENT) await wrapper.instance().onSubmit(MOCK_EVENT)
assert(propsMethodSpies.sign.calledOnce) assert(propsMethodSpies.sign.calledOnce)
assert.deepEqual( assert.deepEqual(
propsMethodSpies.sign.getCall(0).args[0], propsMethodSpies.sign.getCall(0).args[0],
@ -190,13 +190,10 @@ describe('SendFooter Component', function () {
assert.equal(propsMethodSpies.update.callCount, 0) assert.equal(propsMethodSpies.update.callCount, 0)
}) })
it('should call history.push', function (done) { it('should call history.push', async function () {
Promise.resolve(wrapper.instance().onSubmit(MOCK_EVENT)) await wrapper.instance().onSubmit(MOCK_EVENT)
.then(() => {
assert.equal(historySpies.push.callCount, 1) assert.equal(historySpies.push.callCount, 1)
assert.equal(historySpies.push.getCall(0).args[0], CONFIRM_TRANSACTION_ROUTE) assert.equal(historySpies.push.getCall(0).args[0], CONFIRM_TRANSACTION_ROUTE)
done()
})
}) })
}) })

View File

@ -119,8 +119,8 @@ export default class AddContact extends PureComponent {
<PageContainerFooter <PageContainerFooter
cancelText={this.context.t('cancel')} cancelText={this.context.t('cancel')}
disabled={Boolean(this.state.error)} disabled={Boolean(this.state.error)}
onSubmit={() => { onSubmit={async () => {
addToAddressBook(this.state.ensAddress || this.state.ethAddress, this.state.newName) await addToAddressBook(this.state.ensAddress || this.state.ethAddress, this.state.newName)
history.push(CONTACT_LIST_ROUTE) history.push(CONTACT_LIST_ROUTE)
}} }}
onCancel={() => { onCancel={() => {

View File

@ -111,12 +111,12 @@ export default class EditContact extends PureComponent {
</div> </div>
<PageContainerFooter <PageContainerFooter
cancelText={this.context.t('cancel')} cancelText={this.context.t('cancel')}
onSubmit={() => { onSubmit={async () => {
if (this.state.newAddress !== '' && this.state.newAddress !== address) { if (this.state.newAddress !== '' && this.state.newAddress !== address) {
// if the user makes a valid change to the address field, remove the original address // if the user makes a valid change to the address field, remove the original address
if (isValidAddress(this.state.newAddress)) { if (isValidAddress(this.state.newAddress)) {
removeFromAddressBook(chainId, address) removeFromAddressBook(chainId, address)
addToAddressBook(this.state.newAddress, this.state.newName || name, this.state.newMemo || memo) await addToAddressBook(this.state.newAddress, this.state.newName || name, this.state.newMemo || memo)
setAccountLabel(this.state.newAddress, this.state.newName || name) setAccountLabel(this.state.newAddress, this.state.newName || name)
history.push(listRoute) history.push(listRoute)
} else { } else {
@ -124,7 +124,7 @@ export default class EditContact extends PureComponent {
} }
} else { } else {
// update name // update name
addToAddressBook(address, this.state.newName || name, this.state.newMemo || memo) await addToAddressBook(address, this.state.newName || name, this.state.newMemo || memo)
setAccountLabel(address, this.state.newName || name) setAccountLabel(address, this.state.newName || name)
history.push(listRoute) history.push(listRoute)
} }

View File

@ -1527,19 +1527,20 @@ export function delRpcTarget (oldRpc) {
export function addToAddressBook (recipient, nickname = '', memo = '') { export function addToAddressBook (recipient, nickname = '', memo = '') {
log.debug(`background.addToAddressBook`) log.debug(`background.addToAddressBook`)
return (dispatch, getState) => { return async (dispatch, getState) => {
const chainId = getState().metamask.network const chainId = getState().metamask.network
background.setAddressBook(checksumAddress(recipient), nickname, chainId, memo, (err, set) => {
if (err) { let set
log.error(err) try {
set = await promisifiedBackground.setAddressBook(checksumAddress(recipient), nickname, chainId, memo)
} catch (error) {
log.error(error)
dispatch(displayWarning('Address book failed to update')) dispatch(displayWarning('Address book failed to update'))
throw err throw error
} }
if (!set) { if (!set) {
return dispatch(displayWarning('Address book failed to update')) return dispatch(displayWarning('Address book failed to update'))
} }
})
} }
} }