fixes for draft store specs, contact.isMe now returns true if it matches any account

This commit is contained in:
Ben Gotow 2016-01-11 13:23:30 -08:00
parent d98eca2cca
commit 2c74b38323
8 changed files with 52 additions and 43 deletions

View file

@ -19,6 +19,8 @@ describe "UnreadNotifications", ->
spyOn(CategoryStore, "getStandardCategory").andReturn inbox spyOn(CategoryStore, "getStandardCategory").andReturn inbox
account = AccountStore.accounts()[0]
@threadA = new Thread @threadA = new Thread
labels: [inbox] labels: [inbox]
@threadB = new Thread @threadB = new Thread
@ -81,7 +83,7 @@ describe "UnreadNotifications", ->
@msgFromMe = new Message @msgFromMe = new Message
unread: true unread: true
date: new Date() date: new Date()
from: [AccountStore.current().me()] from: [account.me()]
subject: "A Sent Mail!" subject: "A Sent Mail!"
threadId: "A" threadId: "A"

View file

@ -6,6 +6,8 @@ contact_1 =
name: "Evan Morikawa" name: "Evan Morikawa"
email: "evan@nylas.com" email: "evan@nylas.com"
account = AccountStore.accounts()[0]
describe "Contact", -> describe "Contact", ->
it "can be built via the constructor", -> it "can be built via the constructor", ->
@ -126,20 +128,20 @@ describe "Contact", ->
expect(c8.lastName()).toBe "K@ylor" expect(c8.lastName()).toBe "K@ylor"
it "should properly return `You` as the display name for the current user", -> it "should properly return `You` as the display name for the current user", ->
c1 = new Contact {name: " Test Monkey", email: AccountStore.current().emailAddress} c1 = new Contact {name: " Test Monkey", email: account.emailAddress}
expect(c1.displayName()).toBe "You" expect(c1.displayName()).toBe "You"
expect(c1.displayFirstName()).toBe "You" expect(c1.displayFirstName()).toBe "You"
expect(c1.displayLastName()).toBe "" expect(c1.displayLastName()).toBe ""
describe "isMe", -> describe "isMe", ->
it "returns true if the contact name matches the account email address", -> it "returns true if the contact name matches the account email address", ->
c1 = new Contact {email: AccountStore.current().emailAddress} c1 = new Contact {email: account.emailAddress}
expect(c1.isMe()).toBe(true) expect(c1.isMe()).toBe(true)
c1 = new Contact {email: 'ben@nylas.com'} c1 = new Contact {email: 'ben@nylas.com'}
expect(c1.isMe()).toBe(false) expect(c1.isMe()).toBe(false)
it "is case insensitive", -> it "is case insensitive", ->
c1 = new Contact {email: AccountStore.current().emailAddress.toUpperCase()} c1 = new Contact {email: account.emailAddress.toUpperCase()}
expect(c1.isMe()).toBe(true) expect(c1.isMe()).toBe(true)
it "also matches any aliases you've created", -> it "also matches any aliases you've created", ->

View file

@ -54,7 +54,7 @@ jasmine.getEnv().addEqualityTester(_.isEqual) # Use underscore's definition of e
if process.env.JANKY_SHA1 and process.platform is 'win32' if process.env.JANKY_SHA1 and process.platform is 'win32'
jasmine.getEnv().defaultTimeoutInterval = 60000 jasmine.getEnv().defaultTimeoutInterval = 60000
else else
jasmine.getEnv().defaultTimeoutInterval = 10000 jasmine.getEnv().defaultTimeoutInterval = 250
specPackageName = null specPackageName = null
specPackagePath = null specPackagePath = null
@ -147,15 +147,18 @@ beforeEach ->
# prevent specs from modifying N1's menus # prevent specs from modifying N1's menus
spyOn(NylasEnv.menu, 'sendToBrowserProcess') spyOn(NylasEnv.menu, 'sendToBrowserProcess')
# Log in a fake user # Log in a fake user, and ensure that accountForId, etc. work
spyOn(AccountStore, 'current').andCallFake -> AccountStore._index = 0
new Account AccountStore._accounts = [
new Account({
provider: "gmail" provider: "gmail"
name: TEST_ACCOUNT_NAME name: TEST_ACCOUNT_NAME
emailAddress: TEST_ACCOUNT_EMAIL emailAddress: TEST_ACCOUNT_EMAIL
organizationUnit: NylasEnv.testOrganizationUnit organizationUnit: NylasEnv.testOrganizationUnit
clientId: TEST_ACCOUNT_CLIENT_ID clientId: TEST_ACCOUNT_CLIENT_ID
serverId: TEST_ACCOUNT_ID serverId: TEST_ACCOUNT_ID
})
]
# reset config before each spec; don't load or save from/to `config.json` # reset config before each spec; don't load or save from/to `config.json`
spyOn(Config::, 'load') spyOn(Config::, 'load')

View file

@ -30,6 +30,7 @@ msgWithReplyTo = null
messageWithStyleTags = null messageWithStyleTags = null
fakeMessageWithFiles = null fakeMessageWithFiles = null
msgWithReplyToDuplicates = null msgWithReplyToDuplicates = null
account = null
class TestExtension extends ComposerExtension class TestExtension extends ComposerExtension
@prepareNewDraft: ({draft}) -> @prepareNewDraft: ({draft}) ->
@ -57,14 +58,18 @@ describe "DraftStore", ->
# requires us to add `advanceClock` blocks. # requires us to add `advanceClock` blocks.
_.defer -> DraftStore._onInlineStylesResult({}, body) _.defer -> DraftStore._onInlineStylesResult({}, body)
account = AccountStore.accounts()[0]
fakeThread = new Thread fakeThread = new Thread
id: 'fake-thread-id' id: 'fake-thread-id'
accountId: account.id
subject: 'Fake Subject' subject: 'Fake Subject'
fakeMessage1 = new Message fakeMessage1 = new Message
id: 'fake-message-1' id: 'fake-message-1'
accountId: account.id
to: [new Contact(email: 'ben@nylas.com'), new Contact(email: 'evan@nylas.com')] to: [new Contact(email: 'ben@nylas.com'), new Contact(email: 'evan@nylas.com')]
cc: [new Contact(email: 'mg@nylas.com'), new Contact(email: AccountStore.current().me().email)] cc: [new Contact(email: 'mg@nylas.com'), account.me()]
bcc: [new Contact(email: 'recruiting@nylas.com')] bcc: [new Contact(email: 'recruiting@nylas.com')]
from: [new Contact(email: 'customer@example.com', name: 'Customer')] from: [new Contact(email: 'customer@example.com', name: 'Customer')]
threadId: 'fake-thread-id' threadId: 'fake-thread-id'
@ -74,6 +79,7 @@ describe "DraftStore", ->
fakeMessage2 = new Message fakeMessage2 = new Message
id: 'fake-message-2' id: 'fake-message-2'
accountId: account.id
to: [new Contact(email: 'customer@example.com')] to: [new Contact(email: 'customer@example.com')]
from: [new Contact(email: 'ben@nylas.com')] from: [new Contact(email: 'ben@nylas.com')]
threadId: 'fake-thread-id' threadId: 'fake-thread-id'
@ -83,8 +89,9 @@ describe "DraftStore", ->
fakeMessageWithFiles = new Message fakeMessageWithFiles = new Message
id: 'fake-message-with-files' id: 'fake-message-with-files'
accountId: account.id
to: [new Contact(email: 'ben@nylas.com'), new Contact(email: 'evan@nylas.com')] to: [new Contact(email: 'ben@nylas.com'), new Contact(email: 'evan@nylas.com')]
cc: [new Contact(email: 'mg@nylas.com'), new Contact(email: AccountStore.current().me().email)] cc: [new Contact(email: 'mg@nylas.com'), account.me()]
bcc: [new Contact(email: 'recruiting@nylas.com')] bcc: [new Contact(email: 'recruiting@nylas.com')]
from: [new Contact(email: 'customer@example.com', name: 'Customer')] from: [new Contact(email: 'customer@example.com', name: 'Customer')]
files: [new File(filename: "test.jpg"), new File(filename: "test.pdj")] files: [new File(filename: "test.jpg"), new File(filename: "test.pdj")]
@ -95,10 +102,11 @@ describe "DraftStore", ->
msgFromMe = new Message msgFromMe = new Message
id: 'fake-message-3' id: 'fake-message-3'
accountId: account.id
to: [new Contact(email: '1@1.com'), new Contact(email: '2@2.com')] to: [new Contact(email: '1@1.com'), new Contact(email: '2@2.com')]
cc: [new Contact(email: '3@3.com'), new Contact(email: '4@4.com')] cc: [new Contact(email: '3@3.com'), new Contact(email: '4@4.com')]
bcc: [new Contact(email: '5@5.com'), new Contact(email: '6@6.com')] bcc: [new Contact(email: '5@5.com'), new Contact(email: '6@6.com')]
from: [new Contact(email: AccountStore.current().me().email)] from: [account.me()]
threadId: 'fake-thread-id' threadId: 'fake-thread-id'
body: 'Fake Message 2' body: 'Fake Message 2'
subject: 'Re: Fake Subject' subject: 'Re: Fake Subject'
@ -106,6 +114,7 @@ describe "DraftStore", ->
msgWithReplyTo = new Message msgWithReplyTo = new Message
id: 'fake-message-reply-to' id: 'fake-message-reply-to'
accountId: account.id
to: [new Contact(email: '1@1.com'), new Contact(email: '2@2.com')] to: [new Contact(email: '1@1.com'), new Contact(email: '2@2.com')]
cc: [new Contact(email: '3@3.com'), new Contact(email: '4@4.com')] cc: [new Contact(email: '3@3.com'), new Contact(email: '4@4.com')]
bcc: [new Contact(email: '5@5.com'), new Contact(email: '6@6.com')] bcc: [new Contact(email: '5@5.com'), new Contact(email: '6@6.com')]
@ -118,6 +127,7 @@ describe "DraftStore", ->
msgWithReplyToDuplicates = new Message msgWithReplyToDuplicates = new Message
id: 'fake-message-reply-to-duplicates' id: 'fake-message-reply-to-duplicates'
accountId: account.id
to: [new Contact(email: '1@1.com'), new Contact(email: '2@2.com')] to: [new Contact(email: '1@1.com'), new Contact(email: '2@2.com')]
cc: [new Contact(email: '1@1.com'), new Contact(email: '4@4.com')] cc: [new Contact(email: '1@1.com'), new Contact(email: '4@4.com')]
from: [new Contact(email: 'reply-to@5.com')] from: [new Contact(email: 'reply-to@5.com')]
@ -129,8 +139,9 @@ describe "DraftStore", ->
messageWithStyleTags = new Message messageWithStyleTags = new Message
id: 'message-with-style-tags' id: 'message-with-style-tags'
accountId: account.id
to: [new Contact(email: 'ben@nylas.com'), new Contact(email: 'evan@nylas.com')] to: [new Contact(email: 'ben@nylas.com'), new Contact(email: 'evan@nylas.com')]
cc: [new Contact(email: 'mg@nylas.com'), new Contact(email: AccountStore.current().me().email)] cc: [new Contact(email: 'mg@nylas.com'), account.me()]
bcc: [new Contact(email: 'recruiting@nylas.com')] bcc: [new Contact(email: 'recruiting@nylas.com')]
from: [new Contact(email: 'customer@example.com', name: 'Customer')] from: [new Contact(email: 'customer@example.com', name: 'Customer')]
threadId: 'fake-thread-id' threadId: 'fake-thread-id'
@ -241,7 +252,7 @@ describe "DraftStore", ->
it "should not include you when you were cc'd on the previous message", -> it "should not include you when you were cc'd on the previous message", ->
ccEmails = @model.cc.map (cc) -> cc.email ccEmails = @model.cc.map (cc) -> cc.email
expect(ccEmails.indexOf(AccountStore.current().me().email)).toEqual(-1) expect(ccEmails.indexOf(account.me().email)).toEqual(-1)
it "should set the replyToMessageId to the previous message's ids", -> it "should set the replyToMessageId to the previous message's ids", ->
expect(@model.replyToMessageId).toEqual(fakeMessage1.id) expect(@model.replyToMessageId).toEqual(fakeMessage1.id)

View file

@ -69,11 +69,14 @@ class Account extends Model
# Returns a {Contact} model that represents the current user. # Returns a {Contact} model that represents the current user.
me: -> me: ->
Contact = require './contact' if @defaultAlias
return new Contact return @meUsingAlias(@defaultAlias)
accountId: @id else
name: @name Contact = require './contact'
email: @emailAddress return new Contact
accountId: @id
name: @name
email: @emailAddress
meUsingAlias: (alias) -> meUsingAlias: (alias) ->
Contact = require './contact' Contact = require './contact'

View file

@ -4,10 +4,6 @@ Attributes = require '../attributes'
RegExpUtils = require '../../regexp-utils' RegExpUtils = require '../../regexp-utils'
_ = require 'underscore' _ = require 'underscore'
# Only load the FocusedPerspectiveStore the first time we actually need it. This
# lets us safely require a `Contact` object without side effects.
FocusedPerspectiveStore = null
name_prefixes = {} name_prefixes = {}
name_suffixes = {} name_suffixes = {}
@ -97,17 +93,15 @@ class Contact extends Model
# You should use this method instead of comparing the user's email address to # You should use this method instead of comparing the user's email address to
# the account email, since it is case-insensitive and future-proof. # the account email, since it is case-insensitive and future-proof.
isMe: -> isMe: ->
FocusedPerspectiveStore = require '../stores/focused-perspective-store' AccountStore = require '../stores/account-store'
account = FocusedPerspectiveStore.current()?.account for account in AccountStore.accounts()
return false unless account if Utils.emailIsEquivalent(@email, account.emailAddress)
if Utils.emailIsEquivalent(@email, account.emailAddress)
return true
for alias in account.aliases
if Utils.emailIsEquivalent(@email, Contact.fromString(alias).email)
return true return true
for alias in account.aliases
if Utils.emailIsEquivalent(@email, Contact.fromString(alias).email)
return true
return false return false
# Returns a {String} display name. # Returns a {String} display name.

View file

@ -157,9 +157,8 @@ class AccountStore
Utils.emailIsEquivalent(email, account.emailAddress) Utils.emailIsEquivalent(email, account.emailAddress)
# Public: Returns the {Account} for the given account id, or null. # Public: Returns the {Account} for the given account id, or null.
accountForId: (accountId) => accountForId: (id) =>
_.find @_accounts, (account) -> _.findWhere(@_accounts, {id})
accountId is account.accountId
# Public: Returns the currently active {Account}. # Public: Returns the currently active {Account}.
current: => current: =>

View file

@ -152,12 +152,6 @@ class DraftStore
########### PRIVATE #################################################### ########### PRIVATE ####################################################
_getFromField: (account) ->
if account.defaultAlias?
account.meUsingAlias(account.defaultAlias)
else
account.me()
_doneWithSession: (session) -> _doneWithSession: (session) ->
session.teardown() session.teardown()
delete @_draftSessions[session.draftClientId] delete @_draftSessions[session.draftClientId]
@ -289,8 +283,9 @@ class DraftStore
_constructDraft: ({attributes, thread}) => _constructDraft: ({attributes, thread}) =>
account = AccountStore.accountForId(thread.accountId) account = AccountStore.accountForId(thread.accountId)
throw new Error("Cannot find #{thread.accountId}") unless account
return new Message _.extend {}, attributes, return new Message _.extend {}, attributes,
from: [@_getFromField(account)] from: [account.me()]
date: (new Date) date: (new Date)
draft: true draft: true
pristine: true pristine: true
@ -380,7 +375,7 @@ class DraftStore
draft = new Message draft = new Message
body: "" body: ""
from: [@_getFromField(account)] from: [account.me()]
date: (new Date) date: (new Date)
draft: true draft: true
pristine: true pristine: true
@ -457,7 +452,7 @@ class DraftStore
draft = new Message draft = new Message
body: query.body || '' body: query.body || ''
subject: query.subject || '', subject: query.subject || '',
from: [@_getFromField(account)] from: [account.me()]
date: (new Date) date: (new Date)
draft: true draft: true
pristine: true pristine: true