fix(unread-count): Add specs, fix logic in UnreadCountStore

Summary: Fixes T2376

Test Plan: Run 5 new specs

Reviewers: evan

Reviewed By: evan

Maniphest Tasks: T2376

Differential Revision: https://phab.nylas.com/D1805
This commit is contained in:
Ben Gotow 2015-07-28 09:25:40 -07:00
parent a85a65d3ff
commit 34b35a27da
2 changed files with 98 additions and 13 deletions

View file

@ -0,0 +1,75 @@
UnreadCountStore = require '../../src/flux/stores/unread-count-store'
NamespaceStore = require '../../src/flux/stores/namespace-store'
DatabaseStore = require '../../src/flux/stores/database-store'
Folder = require '../../src/flux/models/folder'
Label = require '../../src/flux/models/label'
Thread = require '../../src/flux/models/thread'
Category = require '../../src/flux/models/category'
describe "UnreadCountStore", ->
describe "_fetchCount", ->
beforeEach ->
atom.testOrganizationUnit = 'folder'
spyOn(DatabaseStore, 'findBy').andCallFake =>
Promise.resolve(new Category({id: 'inbox-category-id'}))
spyOn(DatabaseStore, 'count').andCallFake =>
Promise.resolve(100)
it "should create the correct query when using folders", ->
atom.testOrganizationUnit = 'folder'
UnreadCountStore._fetchCount()
advanceClock()
expect(DatabaseStore.findBy).toHaveBeenCalledWith(Folder, {name: 'inbox'})
[Model, Matchers] = DatabaseStore.count.calls[0].args
expect(Model).toBe(Thread)
expect(Matchers[0].attr.modelKey).toBe('namespaceId')
expect(Matchers[1].attr.modelKey).toBe('unread')
expect(Matchers[1].val).toBe(true)
expect(Matchers[2].attr.modelKey).toBe('folders')
expect(Matchers[2].val).toBe('inbox-category-id')
it "should create the correct query when using labels", ->
atom.testOrganizationUnit = 'label'
UnreadCountStore._fetchCount()
advanceClock()
expect(DatabaseStore.findBy).toHaveBeenCalledWith(Label, {name: 'inbox'})
[Model, Matchers] = DatabaseStore.count.calls[0].args
expect(Matchers[0].attr.modelKey).toBe('namespaceId')
expect(Matchers[1].attr.modelKey).toBe('unread')
expect(Matchers[1].val).toBe(true)
expect(Matchers[2].attr.modelKey).toBe('labels')
expect(Matchers[2].val).toBe('inbox-category-id')
it "should not trigger if the unread count is the same", ->
spyOn(UnreadCountStore, 'trigger')
UnreadCountStore._count = 100
UnreadCountStore._fetchCount()
advanceClock()
expect(UnreadCountStore.trigger).not.toHaveBeenCalled()
UnreadCountStore._count = 101
UnreadCountStore._fetchCount()
advanceClock()
expect(UnreadCountStore.trigger).toHaveBeenCalled()
it "should update the badge count", ->
UnreadCountStore._count = 101
spyOn(UnreadCountStore, '_updateBadgeForCount')
UnreadCountStore._fetchCount()
advanceClock()
expect(UnreadCountStore._updateBadgeForCount).toHaveBeenCalled()
describe "_updateBadgeForCount", ->
it "should set the badge correctly", ->
spyOn(UnreadCountStore, '_setBadge')
spyOn(atom, 'isMainWindow').andCallFake -> true
UnreadCountStore._updateBadgeForCount(0)
expect(UnreadCountStore._setBadge).toHaveBeenCalledWith("")
UnreadCountStore._updateBadgeForCount(1)
expect(UnreadCountStore._setBadge).toHaveBeenCalledWith("1")
UnreadCountStore._updateBadgeForCount(100)
expect(UnreadCountStore._setBadge).toHaveBeenCalledWith("100")
UnreadCountStore._updateBadgeForCount(1000)
expect(UnreadCountStore._setBadge).toHaveBeenCalledWith("999+")

View file

@ -2,10 +2,13 @@ Reflux = require 'reflux'
_ = require 'underscore' _ = require 'underscore'
remote = require 'remote' remote = require 'remote'
app = remote.require 'app' app = remote.require 'app'
CategoryStore = require './category-store'
NamespaceStore = require './namespace-store' NamespaceStore = require './namespace-store'
DatabaseStore = require './database-store' DatabaseStore = require './database-store'
Actions = require '../actions' Actions = require '../actions'
Thread = require '../models/thread' Thread = require '../models/thread'
Folder = require '../models/folder'
Label = require '../models/label'
### ###
Public: The UnreadCountStore exposes a simple API for getting the number of Public: The UnreadCountStore exposes a simple API for getting the number of
@ -39,24 +42,31 @@ UnreadCountStore = Reflux.createStore
namespace = NamespaceStore.current() namespace = NamespaceStore.current()
return unless namespace return unless namespace
matchers = [
Thread.attributes.namespaceId.equal(namespace.id),
Thread.attributes.unread.equal(true),
]
if namespace.usesFolders() if namespace.usesFolders()
matchers.push(Thread.attributes.folders.contains('inbox')) [CategoryClass, CategoryAttribute] = [Folder, Thread.attributes.folders]
else if namespace.usesLabels() else if namespace.usesLabels()
matchers.push(Thread.attributes.labels.contains('inbox')) [CategoryClass, CategoryAttribute] = [Label, Thread.attributes.labels]
else else
return return
DatabaseStore.count(Thread, matchers).then (count) => # Note: We can't use the convenience methods on CategoryStore to fetch the
return if @_count is count # category because it may not have been loaded yet
@_count = count DatabaseStore.findBy(CategoryClass, {name: 'inbox'}).then (category) =>
@_updateBadgeForCount(count) return unless category
@trigger()
.catch (err) => matchers = [
console.warn("Failed to fetch unread count: #{err}") Thread.attributes.namespaceId.equal(namespace.id),
Thread.attributes.unread.equal(true),
CategoryAttribute.contains(category.id)
]
DatabaseStore.count(Thread, matchers).then (count) =>
return if @_count is count
@_count = count
@_updateBadgeForCount(count)
@trigger()
.catch (err) =>
console.warn("Failed to fetch unread count: #{err}")
_updateBadgeForCount: (count) -> _updateBadgeForCount: (count) ->
return unless atom.isMainWindow() return unless atom.isMainWindow()