mirror of
https://github.com/Foundry376/Mailspring.git
synced 2025-09-11 23:24:32 +08:00
fix(aliases): Fix regression to properly create/update/remove aliases
- This fixes #1428 - Add specs
This commit is contained in:
parent
f766c73430
commit
8e382f8c26
2 changed files with 175 additions and 5 deletions
|
@ -75,14 +75,14 @@ class PreferencesAccountDetails extends Component {
|
||||||
|
|
||||||
_onAccountAliasCreated = (newAlias)=> {
|
_onAccountAliasCreated = (newAlias)=> {
|
||||||
const coercedAlias = this._makeAlias(newAlias);
|
const coercedAlias = this._makeAlias(newAlias);
|
||||||
const aliases = this.state.aliases.concat([coercedAlias]);
|
const aliases = this.state.account.aliases.concat([coercedAlias]);
|
||||||
this._setStateAndSave({aliases})
|
this._setStateAndSave({aliases})
|
||||||
};
|
};
|
||||||
|
|
||||||
_onAccountAliasUpdated = (newAlias, alias, idx)=> {
|
_onAccountAliasUpdated = (newAlias, alias, idx)=> {
|
||||||
const coercedAlias = this._makeAlias(newAlias);
|
const coercedAlias = this._makeAlias(newAlias);
|
||||||
const aliases = this.state.aliases.slice();
|
const aliases = this.state.account.aliases.slice();
|
||||||
let defaultAlias = this.state.defaultAlias;
|
let defaultAlias = this.state.account.defaultAlias;
|
||||||
if (defaultAlias === alias) {
|
if (defaultAlias === alias) {
|
||||||
defaultAlias = coercedAlias;
|
defaultAlias = coercedAlias;
|
||||||
}
|
}
|
||||||
|
@ -91,8 +91,8 @@ class PreferencesAccountDetails extends Component {
|
||||||
};
|
};
|
||||||
|
|
||||||
_onAccountAliasRemoved = (alias, idx)=> {
|
_onAccountAliasRemoved = (alias, idx)=> {
|
||||||
const aliases = this.state.aliases.slice();
|
const aliases = this.state.account.aliases.slice();
|
||||||
let defaultAlias = this.state.defaultAlias;
|
let defaultAlias = this.state.account.defaultAlias;
|
||||||
if (defaultAlias === alias) {
|
if (defaultAlias === alias) {
|
||||||
defaultAlias = null;
|
defaultAlias = null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,170 @@
|
||||||
|
import React, {addons} from 'react/addons';
|
||||||
|
import PreferencesAccountDetails from '../lib/tabs/preferences-account-details';
|
||||||
|
const {TestUtils: {renderIntoDocument}} = addons;
|
||||||
|
|
||||||
|
|
||||||
|
const makeComponent = (props = {})=> {
|
||||||
|
return renderIntoDocument(<PreferencesAccountDetails {...props} />);
|
||||||
|
};
|
||||||
|
|
||||||
|
const account = {
|
||||||
|
id: 1,
|
||||||
|
name: 'someone',
|
||||||
|
emailAddress: 'someone@nylas.com',
|
||||||
|
aliases: [],
|
||||||
|
defaultAlias: null,
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('PreferencesAccountDetails', ()=> {
|
||||||
|
beforeEach(()=> {
|
||||||
|
this.account = account
|
||||||
|
this.onAccountUpdated = jasmine.createSpy('onAccountUpdated')
|
||||||
|
this.component = makeComponent({account, onAccountUpdated: this.onAccountUpdated})
|
||||||
|
spyOn(this.component, 'setState')
|
||||||
|
})
|
||||||
|
|
||||||
|
function assertAccountState(actual, expected) {
|
||||||
|
expect(actual).toEqual({
|
||||||
|
account: {
|
||||||
|
id: expected.id || 1,
|
||||||
|
name: expected.name || 'someone',
|
||||||
|
emailAddress: expected.emailAddress || 'someone@nylas.com',
|
||||||
|
aliases: expected.aliases || [],
|
||||||
|
defaultAlias: expected.defaultAlias || null,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('_makeAlias', ()=> {
|
||||||
|
it('returns correct alias when empty string provided', ()=> {
|
||||||
|
const alias = this.component._makeAlias('', this.account)
|
||||||
|
expect(alias).toEqual('someone <someone@nylas.com>')
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns correct alias when only the name provided', ()=> {
|
||||||
|
const alias = this.component._makeAlias('Chad', this.account)
|
||||||
|
expect(alias).toEqual('Chad <someone@nylas.com>')
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns correct alias when email provided', ()=> {
|
||||||
|
const alias = this.component._makeAlias('keith@nylas.com', this.account)
|
||||||
|
expect(alias).toEqual('someone <keith@nylas.com>')
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns correct alias if name and email provided', ()=> {
|
||||||
|
const alias = this.component._makeAlias('Donald donald@nylas.com', this.account)
|
||||||
|
expect(alias).toEqual('Donald <donald@nylas.com>')
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns correct alias if alias provided', ()=> {
|
||||||
|
const alias = this.component._makeAlias('Donald <donald@nylas.com>', this.account)
|
||||||
|
expect(alias).toEqual('Donald <donald@nylas.com>')
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('_setState', ()=> {
|
||||||
|
it('sets the correct state', ()=> {
|
||||||
|
this.component._setState({aliases: ['something']})
|
||||||
|
assertAccountState(this.component.setState.calls[0].args[0], {aliases: ['something']})
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('_onDefaultAliasSelected', ()=> {
|
||||||
|
it('sets the default alias correctly when set to None', ()=> {
|
||||||
|
this.component._onDefaultAliasSelected({target: {value: 'None'}})
|
||||||
|
assertAccountState(this.component.setState.calls[0].args[0], {defaultAlias: null})
|
||||||
|
});
|
||||||
|
|
||||||
|
it('sets the default alias correctly when set to any value', ()=> {
|
||||||
|
this.component._onDefaultAliasSelected({target: {value: 'my alias'}})
|
||||||
|
assertAccountState(this.component.setState.calls[0].args[0], {defaultAlias: 'my alias'})
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('alias handlers', ()=> {
|
||||||
|
beforeEach(()=> {
|
||||||
|
this.currentAlias = 'juan <blah@nylas>'
|
||||||
|
this.newAlias = 'some <alias@nylas.com>';
|
||||||
|
this.account.aliases = [
|
||||||
|
this.currentAlias,
|
||||||
|
]
|
||||||
|
this.component = makeComponent({account: this.account, onAccountUpdated: this.onAccountUpdated})
|
||||||
|
spyOn(this.component, '_makeAlias').andCallFake((alias)=> alias)
|
||||||
|
spyOn(this.component, 'setState')
|
||||||
|
})
|
||||||
|
describe('_onAccountAliasCreated', ()=> {
|
||||||
|
it('creates alias correctly', ()=> {
|
||||||
|
this.component._onAccountAliasCreated(this.newAlias)
|
||||||
|
assertAccountState(this.component.setState.calls[0].args[0],
|
||||||
|
{aliases: [this.currentAlias, this.newAlias]})
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('_onAccountAliasUpdated', ()=> {
|
||||||
|
it('updates alias correctly when no default alias present', ()=> {
|
||||||
|
this.component._onAccountAliasUpdated(this.newAlias, this.currentAlias, 0)
|
||||||
|
assertAccountState(this.component.setState.calls[0].args[0],
|
||||||
|
{aliases: [this.newAlias]})
|
||||||
|
});
|
||||||
|
|
||||||
|
it('updates alias correctly when default alias present and it is being updated', ()=> {
|
||||||
|
this.account.defaultAlias = this.currentAlias
|
||||||
|
this.component = makeComponent({account: this.account, onAccountUpdated: this.onAccountUpdated})
|
||||||
|
spyOn(this.component, '_makeAlias').andCallFake((alias)=> alias)
|
||||||
|
spyOn(this.component, 'setState')
|
||||||
|
|
||||||
|
this.component._onAccountAliasUpdated(this.newAlias, this.currentAlias, 0)
|
||||||
|
assertAccountState(this.component.setState.calls[0].args[0],
|
||||||
|
{aliases: [this.newAlias], defaultAlias: this.newAlias})
|
||||||
|
});
|
||||||
|
|
||||||
|
it('updates alias correctly when default alias present and it is not being updated', ()=> {
|
||||||
|
this.account.defaultAlias = this.currentAlias
|
||||||
|
this.account.aliases.push('otheralias')
|
||||||
|
this.component = makeComponent({account: this.account, onAccountUpdated: this.onAccountUpdated})
|
||||||
|
spyOn(this.component, '_makeAlias').andCallFake((alias)=> alias)
|
||||||
|
spyOn(this.component, 'setState')
|
||||||
|
|
||||||
|
this.component._onAccountAliasUpdated(this.newAlias, 'otheralias', 1)
|
||||||
|
assertAccountState(
|
||||||
|
this.component.setState.calls[0].args[0],
|
||||||
|
{aliases: [this.currentAlias, this.newAlias], defaultAlias: this.currentAlias}
|
||||||
|
)
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
describe('_onAccountAliasRemoved', ()=> {
|
||||||
|
it('removes alias correctly when no default alias present', ()=> {
|
||||||
|
this.component._onAccountAliasRemoved(this.currentAlias, 0)
|
||||||
|
assertAccountState(this.component.setState.calls[0].args[0], {aliases: []})
|
||||||
|
});
|
||||||
|
|
||||||
|
it('removes alias correctly when default alias present and it is being removed', ()=> {
|
||||||
|
this.account.defaultAlias = this.currentAlias
|
||||||
|
this.component = makeComponent({account: this.account, onAccountUpdated: this.onAccountUpdated})
|
||||||
|
spyOn(this.component, '_makeAlias').andCallFake((alias)=> alias)
|
||||||
|
spyOn(this.component, 'setState')
|
||||||
|
|
||||||
|
this.component._onAccountAliasRemoved(this.currentAlias, 0)
|
||||||
|
assertAccountState(this.component.setState.calls[0].args[0],
|
||||||
|
{aliases: [], defaultAlias: null})
|
||||||
|
});
|
||||||
|
|
||||||
|
it('removes alias correctly when default alias present and it is not being removed', ()=> {
|
||||||
|
this.account.defaultAlias = this.currentAlias
|
||||||
|
this.account.aliases.push('otheralias')
|
||||||
|
this.component = makeComponent({account: this.account, onAccountUpdated: this.onAccountUpdated})
|
||||||
|
spyOn(this.component, '_makeAlias').andCallFake((alias)=> alias)
|
||||||
|
spyOn(this.component, 'setState')
|
||||||
|
|
||||||
|
this.component._onAccountAliasRemoved('otheralias', 1)
|
||||||
|
assertAccountState(
|
||||||
|
this.component.setState.calls[0].args[0],
|
||||||
|
{aliases: [this.currentAlias], defaultAlias: this.currentAlias}
|
||||||
|
)
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
Loading…
Add table
Reference in a new issue