_ = require "underscore" proxyquire = require "proxyquire" React = require "react/addons" ReactTestUtils = React.addons.TestUtils {Actions, File, Contact, Message, Account, DraftStore, DatabaseStore, NylasTestUtils, AccountStore, FileUploadStore, ContactStore, ComponentRegistry} = require "nylas-exports" {InjectedComponent} = require 'nylas-component-kit' ParticipantsTextField = require '../lib/participants-text-field' u1 = new Contact(name: "Christine Spang", email: "spang@nylas.com") u2 = new Contact(name: "Michael Grinich", email: "mg@nylas.com") u3 = new Contact(name: "Evan Morikawa", email: "evan@nylas.com") u4 = new Contact(name: "Zoƫ Leiper", email: "zip@nylas.com") u5 = new Contact(name: "Ben Gotow", email: "ben@nylas.com") file = new File(id: 'file_1_id', filename: 'a.png', contentType: 'image/png', size: 10, object: "file") users = [u1, u2, u3, u4, u5] reactStub = (className) -> React.createClass({render: ->
On Sep 3 2015, at 12:14 pm, Evan Morikawa <evan@evanmorikawa.com> wrote:""" useDraft.call @, from: [u1] to: [u2] subject: "Test Reply Message 1" body: @replyBody makeComposer.call @ @editableNode = React.findDOMNode(ReactTestUtils.findRenderedDOMComponentWithAttr(@composer, 'contentEditable')) spyOn(@proxy.changes, "add") it 'begins with the replying message collapsed', -> expect(@editableNode.innerHTML).toBe "" it 'saves the full new body, plus quoted text', -> @editableNode.innerHTML = "Hello world" ReactTestUtils.Simulate.input(@editableNode) expect(@proxy.changes.add).toHaveBeenCalled() expect(@proxy.changes.add.calls.length).toBe 1 body = @proxy.changes.add.calls[0].args[0].body expect(body).toBe """Hello world#{@replyBody}""" describe "when sending a forwarded message message", -> beforeEach -> @fwdBody = """
This is a test!
Begin forwarded message:""" useDraft.call @, from: [u1] to: [u2] subject: "Fwd: Test Forward Message 1" body: @fwdBody makeComposer.call @ @editableNode = React.findDOMNode(ReactTestUtils.findRenderedDOMComponentWithAttr(@composer, 'contentEditable')) spyOn(@proxy.changes, "add") it 'begins with the forwarded message expanded', -> expect(@editableNode.innerHTML).toBe @fwdBody it 'saves the full new body, plus forwarded text', -> @editableNode.innerHTML = "Hello world#{@fwdBody}" ReactTestUtils.Simulate.input(@editableNode) expect(@proxy.changes.add).toHaveBeenCalled() expect(@proxy.changes.add.calls.length).toBe 1 body = @proxy.changes.add.calls[0].args[0].body expect(body).toBe """Hello world#{@fwdBody}""" describe "When displaying info from a draft", -> beforeEach -> useFullDraft.apply(@) makeComposer.call(@) it "attaches the draft to the proxy", -> expect(@draft).toBeDefined() expect(@composer._proxy.draft()).toBe @draft it "set the state based on the draft", -> expect(@composer.state.from).toEqual [u1] expect(@composer.state.showfrom).toEqual true expect(@composer.state.to).toEqual [u2] expect(@composer.state.cc).toEqual [u3, u4] expect(@composer.state.bcc).toEqual [u5] expect(@composer.state.subject).toEqual "Test Message 1" expect(@composer.state.body).toEqual "Hello World
From: Evan Morikawa <evan@evanmorikawa.com>
Subject: Test Forward Message 1
Date: Sep 3 2015, at 12:14 pm
To: Evan Morikawa <evan@nylas.com>
This is a test!
This is my quoted text!" makeComposer.call(@) @composer._sendDraft() expect(Actions.sendDraft).toHaveBeenCalled() it "does not warn if the user has attached a file", -> useDraft.call @, to: [u1] subject: "Hello World" body: "" files: [file] makeComposer.call(@) @composer._sendDraft() expect(Actions.sendDraft).toHaveBeenCalled() expect(@dialog.showMessageBox).not.toHaveBeenCalled() it "shows a warning if there's no subject", -> useDraft.call @, to: [u1], subject: "" makeComposer.call(@) @composer._sendDraft() expect(Actions.sendDraft).not.toHaveBeenCalled() expect(@dialog.showMessageBox).toHaveBeenCalled() dialogArgs = @dialog.showMessageBox.mostRecentCall.args[1] expect(dialogArgs.buttons).toEqual ['Send Anyway', 'Cancel'] it "doesn't show a warning if requirements are satisfied", -> useFullDraft.apply(@); makeComposer.call(@) @composer._sendDraft() expect(Actions.sendDraft).toHaveBeenCalled() expect(@dialog.showMessageBox).not.toHaveBeenCalled() describe "Checking for attachments", -> warn = (body) -> useDraft.call @, subject: "Subject", to: [u1], body: body makeComposer.call(@); @composer._sendDraft() expect(Actions.sendDraft).not.toHaveBeenCalled() expect(@dialog.showMessageBox).toHaveBeenCalled() dialogArgs = @dialog.showMessageBox.mostRecentCall.args[1] expect(dialogArgs.buttons).toEqual ['Send Anyway', 'Cancel'] noWarn = (body) -> useDraft.call @, subject: "Subject", to: [u1], body: body makeComposer.call(@); @composer._sendDraft() expect(Actions.sendDraft).toHaveBeenCalled() expect(@dialog.showMessageBox).not.toHaveBeenCalled() it "warns", -> warn.call(@, "Check out the attached file") it "warns", -> warn.call(@, "I've added an attachment") it "warns", -> warn.call(@, "I'm going to attach the file") it "warns", -> warn.call(@, "Hey attach me
sup") it "doesn't warn", -> noWarn.call(@, "sup yo") it "doesn't warn", -> noWarn.call(@, "Look at the file") it "doesn't warn", -> noWarn.call(@, "Hey there
attach") it "doesn't show a warning if you've attached a file", -> useDraft.call @, subject: "Subject" to: [u1] body: "Check out attached file" files: [file] makeComposer.call(@); @composer._sendDraft() expect(Actions.sendDraft).toHaveBeenCalled() expect(@dialog.showMessageBox).not.toHaveBeenCalled() it "bypasses the warning if force bit is set", -> useDraft.call @, to: [u1], subject: "" makeComposer.call(@) @composer._sendDraft(force: true) expect(Actions.sendDraft).toHaveBeenCalled() expect(@dialog.showMessageBox).not.toHaveBeenCalled() it "sends when you click the send button", -> useFullDraft.apply(@); makeComposer.call(@) sendBtn = React.findDOMNode(@composer.refs.sendButton) ReactTestUtils.Simulate.click sendBtn expect(Actions.sendDraft).toHaveBeenCalledWith(DRAFT_CLIENT_ID) expect(Actions.sendDraft.calls.length).toBe 1 it "doesn't send twice if you double click", -> useFullDraft.apply(@); makeComposer.call(@) sendBtn = React.findDOMNode(@composer.refs.sendButton) ReactTestUtils.Simulate.click sendBtn @isSending.state = true DraftStore.trigger() ReactTestUtils.Simulate.click sendBtn expect(Actions.sendDraft).toHaveBeenCalledWith(DRAFT_CLIENT_ID) expect(Actions.sendDraft.calls.length).toBe 1 describe "when sending a message with keyboard inputs", -> beforeEach -> useFullDraft.apply(@) makeComposer.call(@) spyOn(@composer, "_sendDraft") NylasTestUtils.loadKeymap("internal_packages/composer/keymaps/composer") it "sends the draft on cmd-enter", -> NylasTestUtils.keyPress("cmd-enter", React.findDOMNode(@composer)) expect(@composer._sendDraft).toHaveBeenCalled() it "does not send the draft on enter if the button isn't in focus", -> NylasTestUtils.keyPress("enter", React.findDOMNode(@composer)) expect(@composer._sendDraft).not.toHaveBeenCalled() it "sends the draft on enter when the button is in focus", -> sendBtn = ReactTestUtils.findRenderedDOMComponentWithClass(@composer, "btn-send") NylasTestUtils.keyPress("enter", React.findDOMNode(sendBtn)) expect(@composer._sendDraft).toHaveBeenCalled() it "doesn't let you send twice", -> sendBtn = ReactTestUtils.findRenderedDOMComponentWithClass(@composer, "btn-send") NylasTestUtils.keyPress("enter", React.findDOMNode(sendBtn)) expect(@composer._sendDraft).toHaveBeenCalled() describe "drag and drop", -> beforeEach -> useDraft.call @, to: [u1] subject: "Hello World" body: "" files: [file] makeComposer.call(@) describe "_shouldAcceptDrop", -> it "should return true if the event is carrying native files", -> event = dataTransfer: files:[{'pretend':'imafile'}] types:[] expect(@composer._shouldAcceptDrop(event)).toBe(true) it "should return true if the event is carrying a non-native file URL not on the draft", -> event = dataTransfer: files:[] types:['text/uri-list'] spyOn(@composer, '_nonNativeFilePathForDrop').andReturn("file://one-file") spyOn(FileUploadStore, 'linkedUpload').andReturn({filePath: "file://other-file"}) expect(@composer.state.files.length).toBe(1) expect(@composer._shouldAcceptDrop(event)).toBe(true) it "should return false if the event is carrying a non-native file URL already on the draft", -> event = dataTransfer: files:[] types:['text/uri-list'] spyOn(@composer, '_nonNativeFilePathForDrop').andReturn("file://one-file") spyOn(FileUploadStore, 'linkedUpload').andReturn({filePath: "file://one-file"}) expect(@composer.state.files.length).toBe(1) expect(@composer._shouldAcceptDrop(event)).toBe(false) it "should return false otherwise", -> event = dataTransfer: files:[] types:['text/plain'] expect(@composer._shouldAcceptDrop(event)).toBe(false) describe "_nonNativeFilePathForDrop", -> it "should return a path in the text/nylas-file-url data", -> event = dataTransfer: types: ['text/nylas-file-url'] getData: -> "image/png:test.png:file:///Users/bengotow/Desktop/test.png" expect(@composer._nonNativeFilePathForDrop(event)).toBe("/Users/bengotow/Desktop/test.png") it "should return a path in the text/uri-list data", -> event = dataTransfer: types: ['text/uri-list'] getData: -> "file:///Users/bengotow/Desktop/test.png" expect(@composer._nonNativeFilePathForDrop(event)).toBe("/Users/bengotow/Desktop/test.png") it "should return null otherwise", -> event = dataTransfer: types: ['text/plain'] getData: -> "Hello world" expect(@composer._nonNativeFilePathForDrop(event)).toBe(null) it "should urldecode the contents of the text/uri-list field", -> event = dataTransfer: types: ['text/uri-list'] getData: -> "file:///Users/bengotow/Desktop/Screen%20shot.png" expect(@composer._nonNativeFilePathForDrop(event)).toBe("/Users/bengotow/Desktop/Screen shot.png") it "should return null if text/uri-list contains a non-file path", -> event = dataTransfer: types: ['text/uri-list'] getData: -> "http://apple.com" expect(@composer._nonNativeFilePathForDrop(event)).toBe(null) it "should return null if text/nylas-file-url contains a non-file path", -> event = dataTransfer: types: ['text/nylas-file-url'] getData: -> "application/json:filename.json:undefined" expect(@composer._nonNativeFilePathForDrop(event)).toBe(null) describe "when scrolling to track your cursor", -> it "it tracks when you're at the end of the text", -> it "it doesn't track when typing in the middle of the body", -> it "it doesn't track when typing in the middle of the body", -> describe "When composing a new message", -> it "Can add someone in the to field", -> it "Can add someone in the cc field", -> it "Can add someone in the bcc field", -> describe "When replying to a message", -> describe "When replying all to a message", -> describe "When forwarding a message", -> describe "When changing the subject of a message", -> describe "A draft with files (attachments) and uploads", -> beforeEach -> @file1 = new File id: "f_1" filename: "f1.pdf" size: 1230 @file2 = new File id: "f_2" filename: "f2.jpg" size: 4560 @file3 = new File id: "f_3" filename: "f3.png" size: 7890 @up1 = uploadTaskId: 4 messageClientId: DRAFT_CLIENT_ID filePath: "/foo/bar/f4.bmp" fileName: "f4.bmp" fileSize: 1024 @up2 = uploadTaskId: 5 messageClientId: DRAFT_CLIENT_ID filePath: "/foo/bar/f5.zip" fileName: "f5.zip" fileSize: 1024 spyOn(Actions, "fetchFile") spyOn(FileUploadStore, "linkedUpload").andReturn null spyOn(FileUploadStore, "uploadsForMessage").andReturn [@up1, @up2] useDraft.call @, files: [@file1, @file2] makeComposer.call @ it 'starts fetching attached files', -> waitsFor -> Actions.fetchFile.callCount == 1 runs -> expect(Actions.fetchFile).toHaveBeenCalled() expect(Actions.fetchFile.calls.length).toBe(1) expect(Actions.fetchFile.calls[0].args[0]).toBe @file2 it 'injects an Attachment component for non image files', -> els = ReactTestUtils.scryRenderedComponentsWithTypeAndProps(@composer, InjectedComponent, matching: {role: "Attachment"}) expect(els.length).toBe 1 it 'injects an Attachment:Image component for image files', -> els = ReactTestUtils.scryRenderedComponentsWithTypeAndProps(@composer, InjectedComponent, matching: {role: "Attachment:Image"}) expect(els.length).toBe 1 describe "when the DraftStore `isSending` isn't stubbed out", -> beforeEach -> DraftStore._draftsSending = {} it "doesn't send twice in a popout", -> spyOn(Actions, "queueTask") spyOn(Actions, "sendDraft").andCallThrough() useFullDraft.call(@) makeComposer.call(@) @composer._sendDraft() @composer._sendDraft() expect(Actions.sendDraft.calls.length).toBe 1 it "doesn't send twice in the main window", -> spyOn(Actions, "queueTask") spyOn(Actions, "sendDraft").andCallThrough() spyOn(atom, "isMainWindow").andReturn true useFullDraft.call(@) makeComposer.call(@) @composer._sendDraft() @composer._sendDraft() expect(Actions.sendDraft.calls.length).toBe 1