2015-06-18 10:58:20 +08:00
|
|
|
_ = require 'underscore'
|
2016-05-04 07:42:28 +08:00
|
|
|
Thread = require('../../src/flux/models/thread').default
|
2016-04-26 10:10:55 +08:00
|
|
|
Category = require '../../src/flux/models/category'
|
2016-05-05 08:00:53 +08:00
|
|
|
Message = require('../../src/flux/models/message').default
|
2015-06-18 10:58:20 +08:00
|
|
|
FocusedContentStore = require '../../src/flux/stores/focused-content-store'
|
2016-04-26 10:10:55 +08:00
|
|
|
FocusedPerspectiveStore = require '../../src/flux/stores/focused-perspective-store'
|
2015-06-18 10:58:20 +08:00
|
|
|
MessageStore = require '../../src/flux/stores/message-store'
|
|
|
|
DatabaseStore = require '../../src/flux/stores/database-store'
|
2016-05-04 07:42:28 +08:00
|
|
|
ChangeUnreadTask = require('../../src/flux/tasks/change-unread-task').default
|
2015-06-18 10:58:20 +08:00
|
|
|
Actions = require '../../src/flux/actions'
|
|
|
|
|
2016-01-29 03:13:59 +08:00
|
|
|
testThread = new Thread(id: '123', accountId: TEST_ACCOUNT_ID)
|
|
|
|
testMessage1 = new Message(id: 'a', body: '123', files: [], accountId: TEST_ACCOUNT_ID)
|
|
|
|
testMessage2 = new Message(id: 'b', body: '123', files: [], accountId: TEST_ACCOUNT_ID)
|
|
|
|
testMessage3 = new Message(id: 'c', body: '123', files: [], accountId: TEST_ACCOUNT_ID)
|
2015-06-18 10:58:20 +08:00
|
|
|
|
|
|
|
describe "MessageStore", ->
|
2015-10-30 11:20:49 +08:00
|
|
|
describe "when the receiving focus changes from the FocusedContentStore", ->
|
|
|
|
beforeEach ->
|
|
|
|
if MessageStore._onFocusChangedTimer
|
|
|
|
clearTimeout(MessageStore._onFocusChangedTimer)
|
|
|
|
MessageStore._onFocusChangedTimer = null
|
|
|
|
spyOn(MessageStore, '_onApplyFocusChange')
|
|
|
|
|
|
|
|
afterEach ->
|
|
|
|
if MessageStore._onFocusChangedTimer
|
|
|
|
clearTimeout(MessageStore._onFocusChangedTimer)
|
|
|
|
MessageStore._onFocusChangedTimer = null
|
|
|
|
|
|
|
|
describe "if no change has happened in the last 100ms", ->
|
|
|
|
it "should apply immediately", ->
|
|
|
|
FocusedContentStore.trigger(impactsCollection: (c) -> true )
|
|
|
|
expect(MessageStore._onApplyFocusChange).toHaveBeenCalled()
|
|
|
|
|
|
|
|
describe "if a change has happened in the last 100ms", ->
|
|
|
|
it "should not apply immediately", ->
|
|
|
|
noop = =>
|
|
|
|
MessageStore._onFocusChangedTimer = setTimeout(noop, 100)
|
|
|
|
FocusedContentStore.trigger(impactsCollection: (c) -> true )
|
|
|
|
expect(MessageStore._onApplyFocusChange).not.toHaveBeenCalled()
|
|
|
|
|
|
|
|
it "should apply 100ms after the last focus change and reset", ->
|
|
|
|
FocusedContentStore.trigger(impactsCollection: (c) -> true )
|
|
|
|
expect(MessageStore._onApplyFocusChange.callCount).toBe(1)
|
|
|
|
advanceClock(50)
|
|
|
|
FocusedContentStore.trigger(impactsCollection: (c) -> true )
|
|
|
|
expect(MessageStore._onApplyFocusChange.callCount).toBe(1)
|
|
|
|
advanceClock(50)
|
|
|
|
FocusedContentStore.trigger(impactsCollection: (c) -> true )
|
|
|
|
expect(MessageStore._onApplyFocusChange.callCount).toBe(1)
|
|
|
|
advanceClock(150)
|
|
|
|
FocusedContentStore.trigger(impactsCollection: (c) -> true )
|
|
|
|
expect(MessageStore._onApplyFocusChange.callCount).toBe(3)
|
|
|
|
advanceClock(150)
|
|
|
|
FocusedContentStore.trigger(impactsCollection: (c) -> true )
|
|
|
|
expect(MessageStore._onApplyFocusChange.callCount).toBe(5)
|
|
|
|
|
2016-04-26 10:10:55 +08:00
|
|
|
describe "items", ->
|
|
|
|
beforeEach ->
|
|
|
|
MessageStore._showingHiddenItems = false
|
|
|
|
MessageStore._items = [
|
|
|
|
new Message(categories: [new Category(displayName: 'bla'), new Category(name: 'trash')]),
|
|
|
|
new Message(categories: [new Category(name: 'inbox')]),
|
|
|
|
new Message(categories: [new Category(name: 'bla'), new Category(name: 'spam')]),
|
|
|
|
new Message(categories: []),
|
|
|
|
new Message(categories: [], draft: true),
|
|
|
|
]
|
|
|
|
|
|
|
|
describe "when showing hidden items", ->
|
|
|
|
it "should return the entire items array", ->
|
|
|
|
MessageStore._showingHiddenItems = true
|
|
|
|
expect(MessageStore.items().length).toBe(5)
|
|
|
|
|
|
|
|
describe "when in trash or spam", ->
|
|
|
|
it "should show only the message which are in trash or spam, and drafts", ->
|
|
|
|
spyOn(FocusedPerspectiveStore, 'current').andReturn({categoriesSharedName: => 'trash'})
|
|
|
|
expect(MessageStore.items()).toEqual([
|
|
|
|
MessageStore._items[0],
|
|
|
|
MessageStore._items[2],
|
|
|
|
MessageStore._items[4],
|
|
|
|
])
|
|
|
|
|
|
|
|
describe "when in another folder", ->
|
|
|
|
it "should hide all of the messages which are in trash or spam", ->
|
|
|
|
spyOn(FocusedPerspectiveStore, 'current').andReturn({categoriesSharedName: => 'inbox'})
|
|
|
|
expect(MessageStore.items()).toEqual([
|
|
|
|
MessageStore._items[1],
|
|
|
|
MessageStore._items[3],
|
|
|
|
MessageStore._items[4],
|
|
|
|
])
|
|
|
|
|
2015-10-30 11:20:49 +08:00
|
|
|
describe "when applying focus changes", ->
|
2015-06-18 10:58:20 +08:00
|
|
|
beforeEach ->
|
2015-10-29 05:50:07 +08:00
|
|
|
MessageStore._lastLoadedThreadId = null
|
2015-10-30 11:20:49 +08:00
|
|
|
|
2015-06-18 10:58:20 +08:00
|
|
|
@focus = null
|
|
|
|
spyOn(FocusedContentStore, 'focused').andCallFake (collection) =>
|
|
|
|
if collection is 'thread'
|
|
|
|
@focus
|
|
|
|
else
|
|
|
|
null
|
|
|
|
|
|
|
|
spyOn(FocusedContentStore, 'focusedId').andCallFake (collection) =>
|
|
|
|
if collection is 'thread'
|
|
|
|
@focus?.id
|
|
|
|
else
|
|
|
|
null
|
|
|
|
|
|
|
|
spyOn(DatabaseStore, 'findAll').andCallFake ->
|
|
|
|
include: -> @
|
2015-08-27 05:10:28 +08:00
|
|
|
where: -> @
|
2015-06-18 10:58:20 +08:00
|
|
|
then: (callback) -> callback([testMessage1, testMessage2])
|
|
|
|
|
|
|
|
it "should retrieve the focused thread", ->
|
|
|
|
@focus = testThread
|
2015-09-28 17:26:26 +08:00
|
|
|
MessageStore._thread = null
|
2015-10-30 11:20:49 +08:00
|
|
|
MessageStore._onApplyFocusChange()
|
2015-06-18 10:58:20 +08:00
|
|
|
expect(DatabaseStore.findAll).toHaveBeenCalled()
|
|
|
|
expect(DatabaseStore.findAll.mostRecentCall.args[0]).toBe(Message)
|
|
|
|
|
2015-09-28 17:26:26 +08:00
|
|
|
describe "when the thread is already focused", ->
|
|
|
|
it "should do nothing", ->
|
|
|
|
@focus = testThread
|
|
|
|
MessageStore._thread = @focus
|
2015-10-30 11:20:49 +08:00
|
|
|
MessageStore._onApplyFocusChange()
|
2015-09-28 17:26:26 +08:00
|
|
|
expect(DatabaseStore.findAll).not.toHaveBeenCalled()
|
|
|
|
|
2015-06-18 10:58:20 +08:00
|
|
|
describe "when the thread is unread", ->
|
|
|
|
beforeEach ->
|
|
|
|
@focus = null
|
2015-10-30 11:20:49 +08:00
|
|
|
MessageStore._onApplyFocusChange()
|
2015-07-16 23:54:20 +08:00
|
|
|
testThread.unread = true
|
2015-06-18 10:58:20 +08:00
|
|
|
spyOn(Actions, 'queueTask')
|
2015-11-12 02:25:11 +08:00
|
|
|
spyOn(NylasEnv.config, 'get').andCallFake (key) =>
|
2015-08-15 06:40:11 +08:00
|
|
|
if key is 'core.reading.markAsReadDelay'
|
|
|
|
return 600
|
2015-06-18 10:58:20 +08:00
|
|
|
|
|
|
|
it "should queue a task to mark the thread as read", ->
|
|
|
|
@focus = testThread
|
2015-10-30 11:20:49 +08:00
|
|
|
MessageStore._onApplyFocusChange()
|
2015-06-18 10:58:20 +08:00
|
|
|
advanceClock(500)
|
|
|
|
expect(Actions.queueTask).not.toHaveBeenCalled()
|
|
|
|
advanceClock(500)
|
|
|
|
expect(Actions.queueTask).toHaveBeenCalled()
|
2015-08-06 06:53:08 +08:00
|
|
|
expect(Actions.queueTask.mostRecentCall.args[0] instanceof ChangeUnreadTask).toBe(true)
|
2015-06-18 10:58:20 +08:00
|
|
|
|
|
|
|
it "should not queue a task to mark the thread as read if the thread is no longer selected 500msec later", ->
|
|
|
|
@focus = testThread
|
2015-10-30 11:20:49 +08:00
|
|
|
MessageStore._onApplyFocusChange()
|
2015-06-18 10:58:20 +08:00
|
|
|
advanceClock(500)
|
|
|
|
expect(Actions.queueTask).not.toHaveBeenCalled()
|
|
|
|
@focus = null
|
2015-10-30 11:20:49 +08:00
|
|
|
MessageStore._onApplyFocusChange()
|
2015-06-18 10:58:20 +08:00
|
|
|
advanceClock(500)
|
|
|
|
expect(Actions.queueTask).not.toHaveBeenCalled()
|
2015-10-29 05:50:07 +08:00
|
|
|
|
|
|
|
it "should not re-mark the thread as read when made unread", ->
|
|
|
|
@focus = testThread
|
|
|
|
testThread.unread = false
|
2015-10-30 11:20:49 +08:00
|
|
|
MessageStore._onApplyFocusChange()
|
2015-10-29 05:50:07 +08:00
|
|
|
advanceClock(500)
|
|
|
|
expect(Actions.queueTask).not.toHaveBeenCalled()
|
|
|
|
|
|
|
|
# This simulates a DB change or some attribute changing on the
|
|
|
|
# thread.
|
|
|
|
testThread.unread = true
|
|
|
|
MessageStore._fetchFromCache()
|
|
|
|
advanceClock(500)
|
|
|
|
expect(Actions.queueTask).not.toHaveBeenCalled()
|
2015-12-02 06:38:52 +08:00
|
|
|
|
|
|
|
describe "when toggling expansion of all messages", ->
|
|
|
|
beforeEach ->
|
|
|
|
MessageStore._items = [testMessage1, testMessage2, testMessage3]
|
|
|
|
spyOn(MessageStore, '_fetchExpandedAttachments')
|
|
|
|
|
|
|
|
it 'should expand all when at default state', ->
|
|
|
|
MessageStore._itemsExpanded = {c: 'default'}
|
|
|
|
Actions.toggleAllMessagesExpanded()
|
|
|
|
expect(MessageStore._itemsExpanded).toEqual a: 'explicit', b: 'explicit', c: 'explicit'
|
|
|
|
|
|
|
|
it 'should expand all when at least one item is collapsed', ->
|
|
|
|
MessageStore._itemsExpanded = {b: 'explicit', c: 'explicit'}
|
|
|
|
Actions.toggleAllMessagesExpanded()
|
|
|
|
expect(MessageStore._itemsExpanded).toEqual a: 'explicit', b: 'explicit', c: 'explicit'
|
|
|
|
|
|
|
|
it 'should collapse all except the latest message when all expanded', ->
|
|
|
|
MessageStore._itemsExpanded = {a: 'explicit', b: 'explicit', c: 'explicit'}
|
|
|
|
Actions.toggleAllMessagesExpanded()
|
|
|
|
expect(MessageStore._itemsExpanded).toEqual c: 'explicit'
|