2015-02-21 04:19:34 +08:00
|
|
|
_ = require 'underscore-plus'
|
|
|
|
Contact = require '../../../src/flux/models/contact'
|
|
|
|
Message = require '../../../src/flux/models/message'
|
2015-03-28 07:37:13 +08:00
|
|
|
Thread = require '../../../src/flux/models/thread'
|
|
|
|
Tag = require '../../../src/flux/models/tag'
|
|
|
|
DatabaseStore = require '../../../src/flux/stores/database-store'
|
2015-02-21 04:19:34 +08:00
|
|
|
Main = require '../lib/main'
|
|
|
|
|
|
|
|
describe "UnreadNotifications", ->
|
|
|
|
beforeEach ->
|
2015-03-11 07:08:07 +08:00
|
|
|
Main.activate()
|
2015-03-28 07:37:13 +08:00
|
|
|
|
|
|
|
@threadA = new Thread
|
|
|
|
tags: [new Tag(id: 'inbox')]
|
|
|
|
@threadB = new Thread
|
|
|
|
tags: [new Tag(id: 'archive')]
|
|
|
|
|
2015-02-21 04:19:34 +08:00
|
|
|
@msg1 = new Message
|
|
|
|
unread: true
|
2015-03-11 07:08:07 +08:00
|
|
|
date: new Date()
|
2015-02-21 04:19:34 +08:00
|
|
|
from: [new Contact(name: 'Ben', email: 'ben@example.com')]
|
|
|
|
subject: "Hello World"
|
2015-03-28 07:37:13 +08:00
|
|
|
threadId: "A"
|
2015-02-21 04:19:34 +08:00
|
|
|
@msg2 = new Message
|
|
|
|
unread: true
|
2015-03-11 07:08:07 +08:00
|
|
|
date: new Date()
|
2015-02-21 04:19:34 +08:00
|
|
|
from: [new Contact(name: 'Mark', email: 'mark@example.com')]
|
|
|
|
subject: "Hello World 2"
|
2015-03-28 07:37:13 +08:00
|
|
|
threadId: "A"
|
|
|
|
@msgUnreadButArchived = new Message
|
|
|
|
unread: true
|
|
|
|
date: new Date()
|
|
|
|
from: [new Contact(name: 'Mark', email: 'mark@example.com')]
|
|
|
|
subject: "Hello World 2"
|
|
|
|
threadId: "B"
|
2015-02-21 04:19:34 +08:00
|
|
|
@msgRead = new Message
|
|
|
|
unread: false
|
2015-03-11 07:08:07 +08:00
|
|
|
date: new Date()
|
2015-02-21 04:19:34 +08:00
|
|
|
from: [new Contact(name: 'Mark', email: 'mark@example.com')]
|
|
|
|
subject: "Hello World Read Already"
|
2015-03-28 07:37:13 +08:00
|
|
|
threadId: "A"
|
2015-03-11 07:08:07 +08:00
|
|
|
@msgOld = new Message
|
|
|
|
unread: true
|
|
|
|
date: new Date(2000,1,1)
|
|
|
|
from: [new Contact(name: 'Mark', email: 'mark@example.com')]
|
|
|
|
subject: "Hello World Old"
|
2015-03-28 07:37:13 +08:00
|
|
|
threadId: "A"
|
|
|
|
|
|
|
|
spyOn(DatabaseStore, 'find').andCallFake (klass, id) =>
|
|
|
|
return Promise.resolve(@threadA) if id is 'A'
|
|
|
|
return Promise.resolve(@threadB) if id is 'B'
|
|
|
|
return Promise.resolve(null)
|
|
|
|
|
|
|
|
spyOn(window, 'Notification').andCallFake ->
|
|
|
|
spyOn(Promise, 'props').andCallFake (dict) ->
|
|
|
|
dictOut = {}
|
|
|
|
for key, val of dict
|
|
|
|
if val.value?
|
|
|
|
dictOut[key] = val.value()
|
|
|
|
else
|
|
|
|
dictOut[key] = val
|
|
|
|
Promise.resolve(dictOut)
|
2015-03-11 07:08:07 +08:00
|
|
|
|
|
|
|
afterEach ->
|
|
|
|
Main.deactivate()
|
2015-02-21 04:19:34 +08:00
|
|
|
|
|
|
|
it "should create a Notification if there is one unread message", ->
|
2015-03-28 07:37:13 +08:00
|
|
|
waitsForPromise =>
|
|
|
|
Main._onNewMailReceived({message: [@msgRead, @msg1]})
|
|
|
|
.then ->
|
|
|
|
expect(window.Notification).toHaveBeenCalled()
|
|
|
|
expect(window.Notification.mostRecentCall.args).toEqual([ 'Ben', { body : 'Hello World', tag : 'unread-update' } ])
|
2015-02-21 04:19:34 +08:00
|
|
|
|
|
|
|
it "should create a Notification if there is more than one unread message", ->
|
2015-03-28 07:37:13 +08:00
|
|
|
waitsForPromise =>
|
|
|
|
Main._onNewMailReceived({message: [@msg1, @msg2, @msgRead]})
|
|
|
|
.then ->
|
|
|
|
expect(window.Notification).toHaveBeenCalled()
|
|
|
|
expect(window.Notification.mostRecentCall.args).toEqual([ '2 Unread Messages', { tag : 'unread-update' } ])
|
2015-02-21 04:19:34 +08:00
|
|
|
|
|
|
|
it "should not create a Notification if there are no new messages", ->
|
2015-03-28 07:37:13 +08:00
|
|
|
waitsForPromise ->
|
|
|
|
Main._onNewMailReceived({message: []})
|
|
|
|
.then ->
|
|
|
|
expect(window.Notification).not.toHaveBeenCalled()
|
|
|
|
|
|
|
|
waitsForPromise ->
|
|
|
|
Main._onNewMailReceived({})
|
|
|
|
.then ->
|
|
|
|
expect(window.Notification).not.toHaveBeenCalled()
|
|
|
|
|
|
|
|
it "should not notify about unread messages that are outside the inbox", ->
|
|
|
|
waitsForPromise =>
|
|
|
|
Main._onNewMailReceived({message: [@msgUnreadButArchived, @msg1]})
|
|
|
|
.then ->
|
|
|
|
expect(window.Notification).toHaveBeenCalled()
|
|
|
|
expect(window.Notification.mostRecentCall.args).toEqual([ 'Ben', { body : 'Hello World', tag : 'unread-update' } ])
|
2015-02-21 04:19:34 +08:00
|
|
|
|
|
|
|
it "should not create a Notification if the new messages are not unread", ->
|
2015-03-28 07:37:13 +08:00
|
|
|
waitsForPromise =>
|
|
|
|
Main._onNewMailReceived({message: [@msgRead]})
|
|
|
|
.then ->
|
|
|
|
expect(window.Notification).not.toHaveBeenCalled()
|
2015-02-21 04:19:34 +08:00
|
|
|
|
2015-03-11 07:08:07 +08:00
|
|
|
it "should not create a Notification if the new messages are actually old ones", ->
|
2015-03-28 07:37:13 +08:00
|
|
|
waitsForPromise =>
|
|
|
|
Main._onNewMailReceived({message: [@msgOld]})
|
|
|
|
.then ->
|
|
|
|
expect(window.Notification).not.toHaveBeenCalled()
|
2015-03-11 07:08:07 +08:00
|
|
|
|