diff --git a/internal_packages/unread-notifications/spec/main-spec.coffee b/internal_packages/unread-notifications/spec/main-spec.coffee index dceca0d0e..37a1280c0 100644 --- a/internal_packages/unread-notifications/spec/main-spec.coffee +++ b/internal_packages/unread-notifications/spec/main-spec.coffee @@ -19,6 +19,8 @@ describe "UnreadNotifications", -> spyOn(CategoryStore, "getStandardCategory").andReturn inbox + account = AccountStore.accounts()[0] + @threadA = new Thread labels: [inbox] @threadB = new Thread @@ -81,7 +83,7 @@ describe "UnreadNotifications", -> @msgFromMe = new Message unread: true date: new Date() - from: [AccountStore.current().me()] + from: [account.me()] subject: "A Sent Mail!" threadId: "A" diff --git a/spec/models/contact-spec.coffee b/spec/models/contact-spec.coffee index b1937a39f..9b1a39365 100644 --- a/spec/models/contact-spec.coffee +++ b/spec/models/contact-spec.coffee @@ -6,6 +6,8 @@ contact_1 = name: "Evan Morikawa" email: "evan@nylas.com" +account = AccountStore.accounts()[0] + describe "Contact", -> it "can be built via the constructor", -> @@ -126,20 +128,20 @@ describe "Contact", -> expect(c8.lastName()).toBe "K@ylor" 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.displayFirstName()).toBe "You" expect(c1.displayLastName()).toBe "" describe "isMe", -> 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) c1 = new Contact {email: 'ben@nylas.com'} expect(c1.isMe()).toBe(false) it "is case insensitive", -> - c1 = new Contact {email: AccountStore.current().emailAddress.toUpperCase()} + c1 = new Contact {email: account.emailAddress.toUpperCase()} expect(c1.isMe()).toBe(true) it "also matches any aliases you've created", -> diff --git a/spec/spec-helper.coffee b/spec/spec-helper.coffee index 838f8fadb..ae64a8139 100644 --- a/spec/spec-helper.coffee +++ b/spec/spec-helper.coffee @@ -54,7 +54,7 @@ jasmine.getEnv().addEqualityTester(_.isEqual) # Use underscore's definition of e if process.env.JANKY_SHA1 and process.platform is 'win32' jasmine.getEnv().defaultTimeoutInterval = 60000 else - jasmine.getEnv().defaultTimeoutInterval = 10000 + jasmine.getEnv().defaultTimeoutInterval = 250 specPackageName = null specPackagePath = null @@ -147,15 +147,18 @@ beforeEach -> # prevent specs from modifying N1's menus spyOn(NylasEnv.menu, 'sendToBrowserProcess') - # Log in a fake user - spyOn(AccountStore, 'current').andCallFake -> - new Account + # Log in a fake user, and ensure that accountForId, etc. work + AccountStore._index = 0 + AccountStore._accounts = [ + new Account({ provider: "gmail" name: TEST_ACCOUNT_NAME emailAddress: TEST_ACCOUNT_EMAIL organizationUnit: NylasEnv.testOrganizationUnit clientId: TEST_ACCOUNT_CLIENT_ID serverId: TEST_ACCOUNT_ID + }) + ] # reset config before each spec; don't load or save from/to `config.json` spyOn(Config::, 'load') diff --git a/spec/stores/draft-store-spec.coffee b/spec/stores/draft-store-spec.coffee index 5d065c252..1019d3a90 100644 --- a/spec/stores/draft-store-spec.coffee +++ b/spec/stores/draft-store-spec.coffee @@ -30,6 +30,7 @@ msgWithReplyTo = null messageWithStyleTags = null fakeMessageWithFiles = null msgWithReplyToDuplicates = null +account = null class TestExtension extends ComposerExtension @prepareNewDraft: ({draft}) -> @@ -57,14 +58,18 @@ describe "DraftStore", -> # requires us to add `advanceClock` blocks. _.defer -> DraftStore._onInlineStylesResult({}, body) + account = AccountStore.accounts()[0] + fakeThread = new Thread id: 'fake-thread-id' + accountId: account.id subject: 'Fake Subject' fakeMessage1 = new Message id: 'fake-message-1' + accountId: account.id 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')] from: [new Contact(email: 'customer@example.com', name: 'Customer')] threadId: 'fake-thread-id' @@ -74,6 +79,7 @@ describe "DraftStore", -> fakeMessage2 = new Message id: 'fake-message-2' + accountId: account.id to: [new Contact(email: 'customer@example.com')] from: [new Contact(email: 'ben@nylas.com')] threadId: 'fake-thread-id' @@ -83,8 +89,9 @@ describe "DraftStore", -> fakeMessageWithFiles = new Message id: 'fake-message-with-files' + accountId: account.id 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')] from: [new Contact(email: 'customer@example.com', name: 'Customer')] files: [new File(filename: "test.jpg"), new File(filename: "test.pdj")] @@ -95,10 +102,11 @@ describe "DraftStore", -> msgFromMe = new Message id: 'fake-message-3' + accountId: account.id 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')] 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' body: 'Fake Message 2' subject: 'Re: Fake Subject' @@ -106,6 +114,7 @@ describe "DraftStore", -> msgWithReplyTo = new Message id: 'fake-message-reply-to' + accountId: account.id 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')] bcc: [new Contact(email: '5@5.com'), new Contact(email: '6@6.com')] @@ -118,6 +127,7 @@ describe "DraftStore", -> msgWithReplyToDuplicates = new Message id: 'fake-message-reply-to-duplicates' + accountId: account.id 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')] from: [new Contact(email: 'reply-to@5.com')] @@ -129,8 +139,9 @@ describe "DraftStore", -> messageWithStyleTags = new Message id: 'message-with-style-tags' + accountId: account.id 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')] from: [new Contact(email: 'customer@example.com', name: 'Customer')] threadId: 'fake-thread-id' @@ -241,7 +252,7 @@ describe "DraftStore", -> it "should not include you when you were cc'd on the previous message", -> 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", -> expect(@model.replyToMessageId).toEqual(fakeMessage1.id) diff --git a/src/flux/models/account.coffee b/src/flux/models/account.coffee index 6d1acd98e..92544b8b9 100644 --- a/src/flux/models/account.coffee +++ b/src/flux/models/account.coffee @@ -69,11 +69,14 @@ class Account extends Model # Returns a {Contact} model that represents the current user. me: -> - Contact = require './contact' - return new Contact - accountId: @id - name: @name - email: @emailAddress + if @defaultAlias + return @meUsingAlias(@defaultAlias) + else + Contact = require './contact' + return new Contact + accountId: @id + name: @name + email: @emailAddress meUsingAlias: (alias) -> Contact = require './contact' diff --git a/src/flux/models/contact.coffee b/src/flux/models/contact.coffee index 00c00deba..cbe381b6d 100644 --- a/src/flux/models/contact.coffee +++ b/src/flux/models/contact.coffee @@ -4,10 +4,6 @@ Attributes = require '../attributes' RegExpUtils = require '../../regexp-utils' _ = 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_suffixes = {} @@ -97,17 +93,15 @@ class Contact extends Model # 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. isMe: -> - FocusedPerspectiveStore = require '../stores/focused-perspective-store' - account = FocusedPerspectiveStore.current()?.account - return false unless account - - if Utils.emailIsEquivalent(@email, account.emailAddress) - return true - - for alias in account.aliases - if Utils.emailIsEquivalent(@email, Contact.fromString(alias).email) + AccountStore = require '../stores/account-store' + for account in AccountStore.accounts() + if Utils.emailIsEquivalent(@email, account.emailAddress) return true + for alias in account.aliases + if Utils.emailIsEquivalent(@email, Contact.fromString(alias).email) + return true + return false # Returns a {String} display name. diff --git a/src/flux/stores/account-store.coffee b/src/flux/stores/account-store.coffee index 947bf1e48..c8bc6dcdd 100644 --- a/src/flux/stores/account-store.coffee +++ b/src/flux/stores/account-store.coffee @@ -157,9 +157,8 @@ class AccountStore Utils.emailIsEquivalent(email, account.emailAddress) # Public: Returns the {Account} for the given account id, or null. - accountForId: (accountId) => - _.find @_accounts, (account) -> - accountId is account.accountId + accountForId: (id) => + _.findWhere(@_accounts, {id}) # Public: Returns the currently active {Account}. current: => diff --git a/src/flux/stores/draft-store.coffee b/src/flux/stores/draft-store.coffee index 2f84c2c16..d896b58cd 100644 --- a/src/flux/stores/draft-store.coffee +++ b/src/flux/stores/draft-store.coffee @@ -152,12 +152,6 @@ class DraftStore ########### PRIVATE #################################################### - _getFromField: (account) -> - if account.defaultAlias? - account.meUsingAlias(account.defaultAlias) - else - account.me() - _doneWithSession: (session) -> session.teardown() delete @_draftSessions[session.draftClientId] @@ -289,8 +283,9 @@ class DraftStore _constructDraft: ({attributes, thread}) => account = AccountStore.accountForId(thread.accountId) + throw new Error("Cannot find #{thread.accountId}") unless account return new Message _.extend {}, attributes, - from: [@_getFromField(account)] + from: [account.me()] date: (new Date) draft: true pristine: true @@ -380,7 +375,7 @@ class DraftStore draft = new Message body: "" - from: [@_getFromField(account)] + from: [account.me()] date: (new Date) draft: true pristine: true @@ -457,7 +452,7 @@ class DraftStore draft = new Message body: query.body || '' subject: query.subject || '', - from: [@_getFromField(account)] + from: [account.me()] date: (new Date) draft: true pristine: true