Mailspring/internal_packages/preferences/spec/preferences-account-details-spec.jsx

167 lines
6.6 KiB
React
Raw Normal View History

import React from 'react';
import {renderIntoDocument} from 'react-addons-test-utils';
import PreferencesAccountDetails from '../lib/tabs/preferences-account-details';
import {Account} from 'nylas-exports';
2016-05-06 13:30:34 +08:00
const makeComponent = (props = {}) => {
return renderIntoDocument(<PreferencesAccountDetails {...props} />);
};
const account = new Account({
id: 1,
clientId: 1,
name: 'someone',
emailAddress: 'someone@nylas.com',
aliases: [],
defaultAlias: null,
})
describe('PreferencesAccountDetails', function preferencesAccountDetails() {
2016-05-06 13:30:34 +08:00
beforeEach(() => {
this.account = account
this.onAccountUpdated = jasmine.createSpy('onAccountUpdated')
this.component = makeComponent({account, onAccountUpdated: this.onAccountUpdated})
spyOn(this.component, 'setState')
})
function assertAccountState(actual, expected) {
for (const key of Object.keys(expected)) {
expect(actual.account[key]).toEqual(expected[key]);
}
}
2016-05-06 13:30:34 +08:00
describe('_makeAlias', () => {
it('returns correct alias when empty string provided', () => {
const alias = this.component._makeAlias('', this.account)
expect(alias).toEqual('someone <someone@nylas.com>')
});
2016-05-06 13:30:34 +08:00
it('returns correct alias when only the name provided', () => {
const alias = this.component._makeAlias('Chad', this.account)
expect(alias).toEqual('Chad <someone@nylas.com>')
});
2016-05-06 13:30:34 +08:00
it('returns correct alias when email provided', () => {
const alias = this.component._makeAlias('keith@nylas.com', this.account)
expect(alias).toEqual('someone <keith@nylas.com>')
});
2016-05-06 13:30:34 +08:00
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>')
});
2016-05-06 13:30:34 +08:00
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>')
});
});
2016-05-06 13:30:34 +08:00
describe('_setState', () => {
it('sets the correct state', () => {
this.component._setState({aliases: ['something']})
assertAccountState(this.component.setState.calls[0].args[0], {aliases: ['something']})
});
});
2016-05-06 13:30:34 +08:00
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})
});
2016-05-06 13:30:34 +08:00
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'})
});
});
2016-05-06 13:30:34 +08:00
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})
2016-05-06 13:30:34 +08:00
spyOn(this.component, '_makeAlias').andCallFake((alias) => alias)
spyOn(this.component, 'setState')
})
2016-05-06 13:30:34 +08:00
describe('_onAccountAliasCreated', () => {
it('creates alias correctly', () => {
this.component._onAccountAliasCreated(this.newAlias)
assertAccountState(this.component.setState.calls[0].args[0],
{aliases: [this.currentAlias, this.newAlias]})
});
});
2016-05-06 13:30:34 +08:00
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]})
});
2016-05-06 13:30:34 +08:00
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})
2016-05-06 13:30:34 +08:00
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})
});
2016-05-06 13:30:34 +08:00
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})
2016-05-06 13:30:34 +08:00
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}
)
});
});
2016-05-06 13:30:34 +08:00
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: []})
});
2016-05-06 13:30:34 +08:00
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})
2016-05-06 13:30:34 +08:00
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})
});
2016-05-06 13:30:34 +08:00
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})
2016-05-06 13:30:34 +08:00
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}
)
});
});
});
});