Fix contact ranking and add tests

Summary:
Contact ranking is now tested.

There was a bug whereby the RankingsJSONCache would only update in the
workerwindow. This regressed when Contact ranking moved exclusively into
the main window and separate composer windws requested rankings via ipc

Test Plan: New tests

Reviewers: drew, bengotow

Reviewed By: bengotow

Differential Revision: https://phab.nylas.com/D2134
This commit is contained in:
Evan Morikawa 2015-10-09 09:31:52 -07:00
parent eb0a915bc2
commit a7aec14ca3
2 changed files with 54 additions and 1 deletions

View file

@ -1,6 +1,7 @@
_ = require 'underscore'
proxyquire = require 'proxyquire'
Contact = require '../../src/flux/models/contact'
NylasAPI = require '../../src/flux/nylas-api'
ContactStore = require '../../src/flux/stores/contact-store'
DatabaseStore = require '../../src/flux/stores/database-store'
AccountStore = require '../../src/flux/stores/account-store'
@ -8,6 +9,19 @@ AccountStore = require '../../src/flux/stores/account-store'
describe "ContactStore", ->
beforeEach ->
spyOn(atom, "isMainWindow").andReturn true
@rankings = [
["evanA@nylas.com", 10]
["evanB@nylas.com", 1]
["evanC@nylas.com", 0.1]
]
spyOn(NylasAPI, "makeRequest").andCallFake (options) =>
if options.path is "/contacts/rankings"
return Promise.resolve(@rankings)
else
throw new Error("Invalid request path!")
atom.testOrganizationUnit = "folder"
ContactStore._contactCache = []
ContactStore._fetchOffset = 0
@ -30,6 +44,45 @@ describe "ContactStore", ->
it "triggers a database fetch", ->
expect(ContactStore._refreshCache.calls.length).toBe 1
describe "ranking contacts", ->
beforeEach ->
ContactStore._accountId = TEST_ACCOUNT_ID
@c1 = new Contact(name: "Evan A", email: "evanA@nylas.com")
@c2 = new Contact(name: "Evan B", email: "evanB@nylas.com")
@c3 = new Contact(name: "Evan C", email: "evanC@nylas.com")
@c4 = new Contact(name: "Ben", email: "ben@nylas.com")
spyOn(DatabaseStore, "findAll").andCallFake ->
where: -> Promise.resolve([@c3, @c1, @c2, @c4])
it "Holds the appropriate rankings on refresh and lowercased the emails", ->
runs ->
spyOn(ContactStore._rankingsCache, "trigger")
ContactStore._rankingsCache.refresh()
waitsFor ->
ContactStore._rankingsCache.trigger.calls.length > 0
runs ->
rankings = ContactStore._rankingsCache.value()
expect(rankings).toEqual
"evana@nylas.com": 10
"evanb@nylas.com": 1
"evanc@nylas.com": 0.1
it "triggers a sort on a contact refresh", ->
spyOn(ContactStore, "_sortContactsCacheWithRankings")
waitsForPromise ->
ContactStore.__refreshCache().then -> # Non debounced version
expect(ContactStore._sortContactsCacheWithRankings).toHaveBeenCalled()
it "sorts the contact cache by the rankings", ->
ContactStore._contactCache = [@c3, @c1, @c2, @c4]
ContactStore._rankingsCache._value =
"evana@nylas.com": 10
"evanb@nylas.com": 1
"evanc@nylas.com": 0.1
ContactStore._sortContactsCacheWithRankings()
expect(ContactStore._contactCache).toEqual [@c1, @c2, @c3, @c4]
describe "when the Account updates but the ID doesn't change", ->
it "does nothing", ->
spyOn(ContactStore, "_refreshCache")

View file

@ -93,7 +93,7 @@ class RankingsJSONCache extends JSONCache
super(key: 'RankingsJSONCache', version: 1, maxAge: 60 * 60 * 1000 * 24)
refreshValue: (callback) =>
return unless atom.isWorkWindow()
return unless atom.isMainWindow()
accountId = AccountStore.current()?.id
return unless accountId