Mailspring/packages/client-app/internal_packages/composer-signature/spec/preferences-signatures-spec.jsx
Juan Tejada 72613baf5d [client-app] Fix selecting and creating signatures in preferences
Summary:
This commit fixes 2 issues with signatures in the preferences:
- Creating a signature (via any of the create buttons) would create 2 signatures
- Trying to select accounts to associate with a signature in the preferences would not work (the account would not be selected)

This was a regression introduced in e638e94084 (diff-4f38fd25aa24b48a309354be643165d3R26)

3111c16 made it so we attempt to `activate` any Stores that are registered with
the StoreRegistry. When adding stores to `nylas-exports`, they are
automatically registered in the StoreRegistry. Given that the
`SignatureStore` is in `nylas-exports`, and consequently is registered in
the StoreRegistry, it would be `activate`d upon window boot.

However, we were also manually activating it inside `internal_packages/composer-signatures/lib/main.es6`.
This caused it to register listeners for every action **twice**. For
this reason, 2 signatures would be created when trying to create 1, and
an account would be immediately unselected after being selected int he
signatures dropdown in preferences.

(Other changes in this are just stylistic)

Test Plan: manual

Reviewers: spang, evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D4043
2017-02-28 09:14:28 -08:00

92 lines
3.5 KiB
JavaScript

/* eslint quote-props: 0 */
import ReactTestUtils from 'react-addons-test-utils';
import React from 'react';
import {SignatureStore, Actions} from 'nylas-exports';
import PreferencesSignatures from '../lib/preferences-signatures';
const SIGNATURES = {
'1': {
id: '1',
title: 'one',
body: 'first test signature!',
},
'2': {
id: '2',
title: 'two',
body: 'Here is my second sig!',
},
}
const DEFAULTS = {
'one@nylas.com': '1',
'two@nylas.com': '2',
}
const makeComponent = (props = {}) => {
return ReactTestUtils.renderIntoDocument(<PreferencesSignatures {...props} />)
}
describe('PreferencesSignatures', function preferencesSignatures() {
this.component = null
describe('when there are no signatures', () => {
it('should add a signature when you click the button', () => {
spyOn(SignatureStore, 'getSignatures').andReturn({})
spyOn(SignatureStore, 'selectedSignature')
spyOn(SignatureStore, 'getDefaults').andReturn({})
this.component = makeComponent()
spyOn(Actions, 'addSignature')
this.button = ReactTestUtils.findRenderedDOMComponentWithClass(this.component, 'btn-create-signature')
ReactTestUtils.Simulate.click(this.button)
expect(Actions.addSignature).toHaveBeenCalled()
})
})
describe('when there are signatures', () => {
beforeEach(() => {
spyOn(SignatureStore, 'getSignatures').andReturn(SIGNATURES)
spyOn(SignatureStore, 'selectedSignature').andReturn(SIGNATURES['1'])
spyOn(SignatureStore, 'getDefaults').andReturn(DEFAULTS)
this.component = makeComponent()
})
it('should add a signature when you click the plus button', () => {
spyOn(Actions, 'addSignature')
this.plus = ReactTestUtils.scryRenderedDOMComponentsWithClass(this.component, 'btn-editable-list')[0]
ReactTestUtils.Simulate.click(this.plus)
expect(Actions.addSignature).toHaveBeenCalled()
})
it('should delete a signature when you click the minus button', () => {
spyOn(Actions, 'removeSignature')
this.minus = ReactTestUtils.scryRenderedDOMComponentsWithClass(this.component, 'btn-editable-list')[1]
ReactTestUtils.Simulate.click(this.minus)
expect(Actions.removeSignature).toHaveBeenCalledWith(SIGNATURES['1'])
})
it('should toggle default status when you click an email on the dropdown', () => {
spyOn(Actions, 'toggleAccount')
this.account = ReactTestUtils.scryRenderedDOMComponentsWithClass(this.component, 'item')[0]
ReactTestUtils.Simulate.mouseDown(this.account)
expect(Actions.toggleAccount).toHaveBeenCalledWith('tester@nylas.com')
})
it('should set the selected signature when you click on one that is not currently selected', () => {
spyOn(Actions, 'selectSignature')
this.item = ReactTestUtils.scryRenderedDOMComponentsWithClass(this.component, 'list-item')[1]
ReactTestUtils.Simulate.click(this.item)
expect(Actions.selectSignature).toHaveBeenCalledWith('2')
})
it('should modify the signature body when edited', () => {
spyOn(Actions, 'updateSignature')
const newText = 'Changed <strong>NEW 1 HTML</strong><br>'
this.component._onEditSignature({target: {value: newText}});
expect(Actions.updateSignature).toHaveBeenCalled()
})
it('should modify the signature title when edited', () => {
spyOn(Actions, 'updateSignature')
const newTitle = 'Changed'
this.component._onEditSignature(newTitle)
expect(Actions.updateSignature).toHaveBeenCalled()
})
})
})