mirror of
https://github.com/Foundry376/Mailspring.git
synced 2025-01-11 02:30:21 +08:00
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:
parent
abca6ffcea
commit
79dfd0866a
2 changed files with 98 additions and 13 deletions
75
spec-nylas/stores/unread-count-store-spec.coffee
Normal file
75
spec-nylas/stores/unread-count-store-spec.coffee
Normal 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+")
|
|
@ -2,10 +2,13 @@ Reflux = require 'reflux'
|
|||
_ = require 'underscore'
|
||||
remote = require 'remote'
|
||||
app = remote.require 'app'
|
||||
CategoryStore = require './category-store'
|
||||
NamespaceStore = require './namespace-store'
|
||||
DatabaseStore = require './database-store'
|
||||
Actions = require '../actions'
|
||||
Thread = require '../models/thread'
|
||||
Folder = require '../models/folder'
|
||||
Label = require '../models/label'
|
||||
|
||||
###
|
||||
Public: The UnreadCountStore exposes a simple API for getting the number of
|
||||
|
@ -39,24 +42,31 @@ UnreadCountStore = Reflux.createStore
|
|||
namespace = NamespaceStore.current()
|
||||
return unless namespace
|
||||
|
||||
matchers = [
|
||||
Thread.attributes.namespaceId.equal(namespace.id),
|
||||
Thread.attributes.unread.equal(true),
|
||||
]
|
||||
if namespace.usesFolders()
|
||||
matchers.push(Thread.attributes.folders.contains('inbox'))
|
||||
[CategoryClass, CategoryAttribute] = [Folder, Thread.attributes.folders]
|
||||
else if namespace.usesLabels()
|
||||
matchers.push(Thread.attributes.labels.contains('inbox'))
|
||||
[CategoryClass, CategoryAttribute] = [Label, Thread.attributes.labels]
|
||||
else
|
||||
return
|
||||
|
||||
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}")
|
||||
# Note: We can't use the convenience methods on CategoryStore to fetch the
|
||||
# category because it may not have been loaded yet
|
||||
DatabaseStore.findBy(CategoryClass, {name: 'inbox'}).then (category) =>
|
||||
return unless category
|
||||
|
||||
matchers = [
|
||||
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) ->
|
||||
return unless atom.isMainWindow()
|
||||
|
|
Loading…
Reference in a new issue