2015-04-01 06:54:16 +08:00
|
|
|
_ = require 'underscore-plus'
|
|
|
|
React = require 'react/addons'
|
|
|
|
ReactTestUtils = React.addons.TestUtils
|
|
|
|
proxyquire = require 'proxyquire'
|
|
|
|
|
|
|
|
{InboxTestUtils,
|
|
|
|
Namespace,
|
|
|
|
NamespaceStore,
|
|
|
|
ContactStore,
|
|
|
|
Contact,
|
|
|
|
Utils,
|
2015-05-15 08:08:30 +08:00
|
|
|
} = require 'nylas-exports'
|
2015-04-01 06:54:16 +08:00
|
|
|
|
|
|
|
ParticipantsTextField = proxyquire '../lib/participants-text-field',
|
2015-05-15 08:08:30 +08:00
|
|
|
'nylas-exports': {Contact, ContactStore}
|
2015-04-01 06:54:16 +08:00
|
|
|
|
|
|
|
participant1 = new Contact
|
2015-05-15 08:08:30 +08:00
|
|
|
email: 'ben@nylas.com'
|
2015-04-01 06:54:16 +08:00
|
|
|
participant2 = new Contact
|
|
|
|
email: 'ben@example.com'
|
2015-05-15 08:08:30 +08:00
|
|
|
name: 'Ben Gotow'
|
2015-04-01 06:54:16 +08:00
|
|
|
participant3 = new Contact
|
|
|
|
email: 'ben@inboxapp.com'
|
2015-05-15 08:08:30 +08:00
|
|
|
name: 'Ben Inboxer'
|
2015-04-01 06:54:16 +08:00
|
|
|
participant4 = new Contact
|
|
|
|
email: 'ben@elsewhere.com',
|
2015-05-15 08:08:30 +08:00
|
|
|
name: 'ben Again'
|
2015-04-01 06:54:16 +08:00
|
|
|
participant5 = new Contact
|
|
|
|
email: 'evan@elsewhere.com',
|
|
|
|
name: 'EVAN'
|
|
|
|
|
|
|
|
describe 'ParticipantsTextField', ->
|
|
|
|
InboxTestUtils.loadKeymap()
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@propChange = jasmine.createSpy('change')
|
|
|
|
|
|
|
|
@fieldName = 'to'
|
|
|
|
@tabIndex = '100'
|
|
|
|
@participants =
|
|
|
|
to: [participant1, participant2]
|
|
|
|
cc: [participant3]
|
|
|
|
bcc: []
|
|
|
|
|
|
|
|
@renderedField = ReactTestUtils.renderIntoDocument(
|
|
|
|
<ParticipantsTextField
|
|
|
|
field={@fieldName}
|
|
|
|
tabIndex={@tabIndex}
|
|
|
|
visible={true}
|
|
|
|
participants={@participants}
|
|
|
|
change={@propChange} />
|
|
|
|
)
|
2015-04-25 02:33:10 +08:00
|
|
|
@renderedInput = React.findDOMNode(ReactTestUtils.findRenderedDOMComponentWithTag(@renderedField, 'input'))
|
2015-04-01 06:54:16 +08:00
|
|
|
|
|
|
|
@expectInputToYield = (input, expected) ->
|
|
|
|
ReactTestUtils.Simulate.change(@renderedInput, {target: {value: input}})
|
|
|
|
InboxTestUtils.keyPress('enter', @renderedInput)
|
|
|
|
|
|
|
|
reviver = (k,v) ->
|
|
|
|
return undefined if k in ["id", "object"]
|
|
|
|
return v
|
|
|
|
found = @propChange.mostRecentCall.args[0]
|
|
|
|
found = JSON.parse(JSON.stringify(found), reviver)
|
|
|
|
expected = JSON.parse(JSON.stringify(expected), reviver)
|
|
|
|
expect(found).toEqual(expected)
|
|
|
|
|
|
|
|
it 'renders into the document', ->
|
|
|
|
expect(ReactTestUtils.isCompositeComponentWithType @renderedField, ParticipantsTextField).toBe(true)
|
|
|
|
|
|
|
|
it 'applies the tabIndex provided to the inner input', ->
|
|
|
|
expect(@renderedInput.tabIndex/1).toBe(@tabIndex/1)
|
|
|
|
|
|
|
|
describe "inserting participant text", ->
|
|
|
|
it "should fire onChange with an updated participants hash", ->
|
|
|
|
@expectInputToYield 'abc@abc.com',
|
|
|
|
to: [participant1, participant2, new Contact(name: 'abc@abc.com', email: 'abc@abc.com')]
|
|
|
|
cc: [participant3]
|
|
|
|
bcc: []
|
|
|
|
|
|
|
|
it "should remove added participants from other fields", ->
|
|
|
|
@expectInputToYield participant3.email,
|
|
|
|
to: [participant1, participant2, new Contact(name: participant3.email, email: participant3.email)]
|
|
|
|
cc: []
|
|
|
|
bcc: []
|
|
|
|
|
|
|
|
it "should use the name of an existing contact in the ContactStore if possible", ->
|
|
|
|
spyOn(ContactStore, 'searchContacts').andCallFake (val, options) ->
|
|
|
|
return [participant3] if val is participant3.email
|
|
|
|
return []
|
|
|
|
|
|
|
|
@expectInputToYield participant3.email,
|
|
|
|
to: [participant1, participant2, participant3]
|
|
|
|
cc: []
|
|
|
|
bcc: []
|
|
|
|
|
|
|
|
it "should not allow the same contact to appear multiple times", ->
|
|
|
|
spyOn(ContactStore, 'searchContacts').andCallFake (val, options) ->
|
|
|
|
return [participant2] if val is participant2.email
|
|
|
|
return []
|
|
|
|
|
|
|
|
@expectInputToYield participant2.email,
|
|
|
|
to: [participant1, participant2]
|
|
|
|
cc: [participant3]
|
|
|
|
bcc: []
|
|
|
|
|
|
|
|
describe "when text contains Name (Email) formatted data", ->
|
|
|
|
it "should correctly parse it into named Contact objects", ->
|
2015-05-15 08:08:30 +08:00
|
|
|
newContact1 = new Contact(name:'Ben Imposter', email:'imposter@nylas.com')
|
|
|
|
newContact2 = new Contact(name:'Nylas Team', email:'feedback@nylas.com')
|
|
|
|
|
2015-04-01 06:54:16 +08:00
|
|
|
inputs = [
|
2015-05-15 08:08:30 +08:00
|
|
|
"Ben Imposter <imposter@nylas.com>, Nylas Team <feedback@nylas.com>",
|
|
|
|
"\n\nbla\nBen Imposter (imposter@nylas.com), Nylas Team (feedback@nylas.com)",
|
|
|
|
"Hello world! I like cheese. \rBen Imposter (imposter@nylas.com)\nNylas Team (feedback@nylas.com)",
|
|
|
|
"Ben Imposter<imposter@nylas.com>Nylas Team (feedback@nylas.com)"
|
2015-04-01 06:54:16 +08:00
|
|
|
]
|
|
|
|
|
|
|
|
for input in inputs
|
|
|
|
@expectInputToYield input,
|
|
|
|
to: [participant1, participant2, newContact1, newContact2]
|
|
|
|
cc: [participant3]
|
|
|
|
bcc: []
|
|
|
|
|
|
|
|
describe "when text contains emails mixed with garbage text", ->
|
|
|
|
it "should still parse out emails into Contact objects", ->
|
2015-05-15 08:08:30 +08:00
|
|
|
newContact1 = new Contact(name:'garbage-man@nylas.com', email:'garbage-man@nylas.com')
|
|
|
|
newContact2 = new Contact(name:'recycling-guy@nylas.com', email:'recycling-guy@nylas.com')
|
|
|
|
|
2015-04-01 06:54:16 +08:00
|
|
|
inputs = [
|
2015-05-15 08:08:30 +08:00
|
|
|
"Hello world I real. \n asd. garbage-man@nylas.com—he's cool Also 'recycling-guy@nylas.com'!",
|
|
|
|
"garbage-man@nylas.com|recycling-guy@nylas.com",
|
|
|
|
"garbage-man@nylas.com1WHOA I REALLY HATE DATA,recycling-guy@nylas.com",
|
|
|
|
"nils.com garbage-man@nylas.com @nylas.com nope@.com nope!recycling-guy@nylas.com HOLLA AT recycling-guy@nylas."
|
2015-04-01 06:54:16 +08:00
|
|
|
]
|
|
|
|
|
|
|
|
for input in inputs
|
|
|
|
@expectInputToYield input,
|
|
|
|
to: [participant1, participant2, newContact1, newContact2]
|
|
|
|
cc: [participant3]
|
|
|
|
bcc: []
|