2017-09-25 01:09:38 +08:00
|
|
|
import {
|
|
|
|
Contact,
|
|
|
|
Message,
|
|
|
|
Thread,
|
|
|
|
Folder,
|
|
|
|
CategoryStore,
|
|
|
|
DatabaseStore,
|
|
|
|
AccountStore,
|
|
|
|
SoundRegistry,
|
|
|
|
NativeNotifications,
|
|
|
|
} from 'nylas-exports';
|
|
|
|
|
2016-05-10 04:09:05 +08:00
|
|
|
import {Notifier} from '../lib/main'
|
|
|
|
|
2017-09-25 01:09:38 +08:00
|
|
|
describe("UnreadNotifications", function UnreadNotifications() {
|
2016-05-10 04:09:05 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
this.notifier = new Notifier();
|
|
|
|
|
2017-09-25 01:09:38 +08:00
|
|
|
const inbox = new Folder({id: "l1", role: "inbox", path: "Inbox"})
|
|
|
|
const archive = new Folder({id: "l2", role: "archive", path: "Archive"})
|
2016-05-10 04:09:05 +08:00
|
|
|
|
2017-06-24 09:01:14 +08:00
|
|
|
spyOn(CategoryStore, "getCategoryByRole").andReturn(inbox);
|
2016-05-10 04:09:05 +08:00
|
|
|
|
|
|
|
const account = AccountStore.accounts()[0];
|
|
|
|
|
|
|
|
this.threadA = new Thread({
|
2016-07-01 08:44:38 +08:00
|
|
|
id: 'A',
|
2017-06-22 17:37:10 +08:00
|
|
|
folders: [inbox],
|
2016-05-10 04:09:05 +08:00
|
|
|
});
|
|
|
|
this.threadB = new Thread({
|
2016-07-01 08:44:38 +08:00
|
|
|
id: 'B',
|
2017-06-22 17:37:10 +08:00
|
|
|
folders: [archive],
|
2016-05-10 04:09:05 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
this.msg1 = new Message({
|
|
|
|
unread: true,
|
|
|
|
date: new Date(),
|
|
|
|
from: [new Contact({name: 'Ben', email: 'benthis.example.com'})],
|
|
|
|
subject: "Hello World",
|
|
|
|
threadId: "A",
|
2017-09-25 01:09:38 +08:00
|
|
|
version: 1,
|
2016-05-10 04:09:05 +08:00
|
|
|
});
|
|
|
|
this.msgNoSender = new Message({
|
|
|
|
unread: true,
|
|
|
|
date: new Date(),
|
|
|
|
from: [],
|
|
|
|
subject: "Hello World",
|
|
|
|
threadId: "A",
|
2017-09-25 01:09:38 +08:00
|
|
|
version: 1,
|
2016-05-10 04:09:05 +08:00
|
|
|
});
|
|
|
|
this.msg2 = new Message({
|
|
|
|
unread: true,
|
|
|
|
date: new Date(),
|
|
|
|
from: [new Contact({name: 'Mark', email: 'markthis.example.com'})],
|
|
|
|
subject: "Hello World 2",
|
|
|
|
threadId: "A",
|
2017-09-25 01:09:38 +08:00
|
|
|
version: 1,
|
2016-05-10 04:09:05 +08:00
|
|
|
});
|
|
|
|
this.msg3 = new Message({
|
|
|
|
unread: true,
|
|
|
|
date: new Date(),
|
|
|
|
from: [new Contact({name: 'Ben', email: 'benthis.example.com'})],
|
2016-07-09 02:30:33 +08:00
|
|
|
subject: "Hello World 3",
|
2016-05-10 04:09:05 +08:00
|
|
|
threadId: "A",
|
2017-09-25 01:09:38 +08:00
|
|
|
version: 1,
|
2016-05-10 04:09:05 +08:00
|
|
|
});
|
|
|
|
this.msg4 = new Message({
|
|
|
|
unread: true,
|
|
|
|
date: new Date(),
|
|
|
|
from: [new Contact({name: 'Ben', email: 'benthis.example.com'})],
|
2016-07-09 02:30:33 +08:00
|
|
|
subject: "Hello World 4",
|
2016-05-10 04:09:05 +08:00
|
|
|
threadId: "A",
|
2017-09-25 01:09:38 +08:00
|
|
|
version: 1,
|
2016-05-10 04:09:05 +08:00
|
|
|
});
|
|
|
|
this.msg5 = new Message({
|
|
|
|
unread: true,
|
|
|
|
date: new Date(),
|
|
|
|
from: [new Contact({name: 'Ben', email: 'benthis.example.com'})],
|
2016-07-09 02:30:33 +08:00
|
|
|
subject: "Hello World 5",
|
2016-05-10 04:09:05 +08:00
|
|
|
threadId: "A",
|
2017-09-25 01:09:38 +08:00
|
|
|
version: 1,
|
2016-05-10 04:09:05 +08:00
|
|
|
});
|
|
|
|
this.msgUnreadButArchived = new Message({
|
|
|
|
unread: true,
|
|
|
|
date: new Date(),
|
|
|
|
from: [new Contact({name: 'Mark', email: 'markthis.example.com'})],
|
|
|
|
subject: "Hello World 2",
|
|
|
|
threadId: "B",
|
2017-09-25 01:09:38 +08:00
|
|
|
version: 1,
|
2016-05-10 04:09:05 +08:00
|
|
|
});
|
|
|
|
this.msgRead = new Message({
|
|
|
|
unread: false,
|
|
|
|
date: new Date(),
|
|
|
|
from: [new Contact({name: 'Mark', email: 'markthis.example.com'})],
|
|
|
|
subject: "Hello World Read Already",
|
|
|
|
threadId: "A",
|
2017-09-25 01:09:38 +08:00
|
|
|
version: 1,
|
2016-05-10 04:09:05 +08:00
|
|
|
});
|
|
|
|
this.msgOld = new Message({
|
|
|
|
unread: true,
|
|
|
|
date: new Date(2000, 1, 1),
|
|
|
|
from: [new Contact({name: 'Mark', email: 'markthis.example.com'})],
|
|
|
|
subject: "Hello World Old",
|
|
|
|
threadId: "A",
|
2017-09-25 01:09:38 +08:00
|
|
|
version: 1,
|
2016-05-10 04:09:05 +08:00
|
|
|
});
|
|
|
|
this.msgFromMe = new Message({
|
|
|
|
unread: true,
|
|
|
|
date: new Date(),
|
|
|
|
from: [account.me()],
|
|
|
|
subject: "A Sent Mail!",
|
|
|
|
threadId: "A",
|
2017-09-25 01:09:38 +08:00
|
|
|
version: 1,
|
2016-05-10 04:09:05 +08:00
|
|
|
});
|
2017-09-25 01:09:38 +08:00
|
|
|
this.msgHigherVersion = new Message({
|
|
|
|
unread: true,
|
|
|
|
date: new Date(),
|
|
|
|
from: [new Contact({name: 'Ben', email: 'benthis.example.com'})],
|
|
|
|
subject: "Hello World",
|
|
|
|
threadId: "A",
|
|
|
|
version: 2,
|
|
|
|
})
|
2016-05-10 04:09:05 +08:00
|
|
|
|
|
|
|
spyOn(DatabaseStore, 'find').andCallFake((klass, id) => {
|
|
|
|
if (id === 'A') {
|
|
|
|
return Promise.resolve(this.threadA);
|
|
|
|
}
|
|
|
|
if (id === 'B') {
|
|
|
|
return Promise.resolve(this.threadB);
|
|
|
|
}
|
|
|
|
return Promise.resolve(null);
|
|
|
|
});
|
|
|
|
|
2017-09-25 01:09:38 +08:00
|
|
|
spyOn(DatabaseStore, 'findAll').andCallFake(() => {
|
|
|
|
return Promise.resolve([this.threadA, this.threadB]);
|
|
|
|
});
|
|
|
|
|
2016-07-01 08:44:38 +08:00
|
|
|
this.notification = jasmine.createSpyObj('notification', ['close']);
|
|
|
|
spyOn(NativeNotifications, 'displayNotification').andReturn(this.notification);
|
|
|
|
|
2016-05-10 04:09:05 +08:00
|
|
|
spyOn(Promise, 'props').andCallFake((dict) => {
|
|
|
|
const dictOut = {};
|
|
|
|
for (const key of Object.keys(dict)) {
|
|
|
|
const val = dict[key];
|
|
|
|
if (val.value !== undefined) {
|
|
|
|
dictOut[key] = val.value();
|
|
|
|
} else {
|
|
|
|
dictOut[key] = val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Promise.resolve(dictOut);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
this.notifier.unlisten();
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should create a Notification if there is one unread message", () => {
|
2017-09-25 01:09:38 +08:00
|
|
|
waitsForPromise(async () => {
|
|
|
|
await this.notifier._onDatabaseChanged({
|
|
|
|
objectClass: Message.name,
|
|
|
|
objects: [this.msgRead, this.msg1],
|
|
|
|
})
|
|
|
|
advanceClock(2000)
|
|
|
|
expect(NativeNotifications.displayNotification).toHaveBeenCalled()
|
|
|
|
const options = NativeNotifications.displayNotification.mostRecentCall.args[0]
|
|
|
|
delete options.onActivate;
|
|
|
|
expect(options).toEqual({
|
|
|
|
title: 'Ben',
|
|
|
|
subtitle: 'Hello World',
|
|
|
|
body: undefined,
|
|
|
|
canReply: true,
|
|
|
|
tag: 'unread-update',
|
2016-05-10 04:09:05 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should create multiple Notifications if there is more than one but less than five unread messages", () => {
|
2017-09-25 01:09:38 +08:00
|
|
|
waitsForPromise(async () => {
|
|
|
|
await this.notifier._onDatabaseChanged({
|
|
|
|
objectClass: Message.name,
|
|
|
|
objects: [this.msg1, this.msg2, this.msg3],
|
|
|
|
})
|
|
|
|
// Need to call advance clock twice because we call setTimeout twice
|
|
|
|
advanceClock(2000)
|
|
|
|
advanceClock(2000)
|
|
|
|
expect(NativeNotifications.displayNotification.callCount).toEqual(3)
|
2016-05-10 04:09:05 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-07-09 02:30:33 +08:00
|
|
|
it("should create Notifications in the order of messages received", () => {
|
2017-09-25 01:09:38 +08:00
|
|
|
waitsForPromise(async () => {
|
|
|
|
await this.notifier._onDatabaseChanged({
|
|
|
|
objectClass: Message.name,
|
|
|
|
objects: [this.msg1, this.msg2],
|
2016-07-09 02:30:33 +08:00
|
|
|
})
|
2017-09-25 01:09:38 +08:00
|
|
|
advanceClock(2000);
|
|
|
|
await this.notifier._onDatabaseChanged({
|
|
|
|
objectClass: Message.name,
|
|
|
|
objects: [this.msg3, this.msg4],
|
|
|
|
})
|
|
|
|
advanceClock(2000);
|
|
|
|
advanceClock(2000);
|
|
|
|
expect(NativeNotifications.displayNotification.callCount).toEqual(4);
|
|
|
|
const subjects = NativeNotifications.displayNotification.calls.map((call) => {
|
|
|
|
return call.args[0].subtitle;
|
2016-07-09 02:30:33 +08:00
|
|
|
});
|
2017-09-25 01:09:38 +08:00
|
|
|
const expected = [this.msg1, this.msg2, this.msg3, this.msg4]
|
|
|
|
.map((msg) => msg.subject);
|
|
|
|
expect(subjects).toEqual(expected);
|
2016-07-09 02:30:33 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-10 04:09:05 +08:00
|
|
|
it("should create a Notification if there are five or more unread messages", () => {
|
2017-09-25 01:09:38 +08:00
|
|
|
waitsForPromise(async () => {
|
|
|
|
await this.notifier._onDatabaseChanged({
|
|
|
|
objectClass: Message.name,
|
|
|
|
objects: [this.msg1, this.msg2, this.msg3, this.msg4, this.msg5],
|
|
|
|
})
|
|
|
|
advanceClock(2000)
|
|
|
|
expect(NativeNotifications.displayNotification).toHaveBeenCalled()
|
|
|
|
expect(NativeNotifications.displayNotification.mostRecentCall.args).toEqual([{
|
|
|
|
title: '5 Unread Messages',
|
|
|
|
tag: 'unread-update',
|
|
|
|
}])
|
|
|
|
})
|
2016-05-10 04:09:05 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should create a Notification correctly, even if new mail has no sender", () => {
|
2017-09-25 01:09:38 +08:00
|
|
|
waitsForPromise(async () => {
|
|
|
|
await this.notifier._onDatabaseChanged({
|
|
|
|
objectClass: Message.name,
|
|
|
|
objects: [this.msgNoSender],
|
2016-05-10 04:09:05 +08:00
|
|
|
});
|
2017-09-25 01:09:38 +08:00
|
|
|
expect(NativeNotifications.displayNotification).toHaveBeenCalled()
|
|
|
|
|
|
|
|
const options = NativeNotifications.displayNotification.mostRecentCall.args[0]
|
|
|
|
delete options.onActivate;
|
|
|
|
expect(options).toEqual({
|
|
|
|
title: 'Unknown',
|
|
|
|
subtitle: 'Hello World',
|
|
|
|
body: undefined,
|
|
|
|
canReply: true,
|
|
|
|
tag: 'unread-update',
|
|
|
|
})
|
2016-05-10 04:09:05 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not create a Notification if there are no new messages", () => {
|
2017-09-25 01:09:38 +08:00
|
|
|
waitsForPromise(async () => {
|
|
|
|
await this.notifier._onDatabaseChanged({
|
|
|
|
objectClass: Message.name,
|
|
|
|
objects: [],
|
|
|
|
})
|
|
|
|
expect(NativeNotifications.displayNotification).not.toHaveBeenCalled()
|
|
|
|
await this.notifier._onDatabaseChanged({});
|
|
|
|
expect(NativeNotifications.displayNotification).not.toHaveBeenCalled()
|
2016-05-10 04:09:05 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not notify about unread messages that are outside the inbox", () => {
|
2017-09-25 01:09:38 +08:00
|
|
|
waitsForPromise(async () => {
|
|
|
|
await this.notifier._onDatabaseChanged({
|
|
|
|
objectClass: Message.name,
|
|
|
|
objects: [this.msgUnreadButArchived, this.msg1],
|
|
|
|
})
|
|
|
|
expect(NativeNotifications.displayNotification).toHaveBeenCalled()
|
|
|
|
const options = NativeNotifications.displayNotification.mostRecentCall.args[0]
|
|
|
|
delete options.onActivate;
|
|
|
|
expect(options).toEqual({
|
|
|
|
title: 'Ben',
|
|
|
|
subtitle: 'Hello World',
|
|
|
|
body: undefined,
|
|
|
|
canReply: true,
|
|
|
|
tag: 'unread-update',
|
|
|
|
})
|
2016-05-10 04:09:05 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-07-09 02:30:33 +08:00
|
|
|
it("should not create a Notification if the new messages are read", () => {
|
2017-09-25 01:09:38 +08:00
|
|
|
waitsForPromise(async () => {
|
|
|
|
await this.notifier._onDatabaseChanged({
|
|
|
|
objectClass: Message.name,
|
|
|
|
objects: [this.msgRead],
|
|
|
|
})
|
|
|
|
expect(NativeNotifications.displayNotification).not.toHaveBeenCalled()
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it("should not create a Notification if the message model is being updated", () => {
|
|
|
|
waitsForPromise(async () => {
|
|
|
|
await this.notifier._onDatabaseChanged({
|
|
|
|
objectClass: Message.name,
|
|
|
|
objects: [this.msgHigherVersion],
|
|
|
|
})
|
|
|
|
expect(NativeNotifications.displayNotification).not.toHaveBeenCalled()
|
2016-05-10 04:09:05 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not create a Notification if the new messages are actually old ones", () => {
|
2017-09-25 01:09:38 +08:00
|
|
|
waitsForPromise(async () => {
|
|
|
|
await this.notifier._onDatabaseChanged({
|
|
|
|
objectClass: Message.name,
|
|
|
|
objects: [this.msgOld],
|
|
|
|
})
|
|
|
|
expect(NativeNotifications.displayNotification).not.toHaveBeenCalled()
|
2016-05-10 04:09:05 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not create a Notification if the new message is one I sent", () => {
|
2017-09-25 01:09:38 +08:00
|
|
|
waitsForPromise(async () => {
|
|
|
|
await this.notifier._onDatabaseChanged({
|
|
|
|
objectClass: Message.name,
|
|
|
|
objects: [this.msgFromMe],
|
|
|
|
})
|
|
|
|
expect(NativeNotifications.displayNotification).not.toHaveBeenCalled()
|
2016-05-10 04:09:05 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-07-01 08:44:38 +08:00
|
|
|
it("clears notifications when a thread is read", () => {
|
2017-09-25 01:09:38 +08:00
|
|
|
waitsForPromise(async () => {
|
|
|
|
await this.notifier._onDatabaseChanged({
|
|
|
|
objectClass: Message.name,
|
|
|
|
objects: [this.msg1],
|
|
|
|
})
|
|
|
|
expect(NativeNotifications.displayNotification).toHaveBeenCalled();
|
|
|
|
expect(this.notification.close).not.toHaveBeenCalled();
|
|
|
|
|
|
|
|
const read = this.threadA.clone();
|
|
|
|
read.unread = false;
|
|
|
|
await this.notifier._onDatabaseChanged({
|
|
|
|
objectClass: Thread.name,
|
|
|
|
objects: [read],
|
|
|
|
})
|
|
|
|
expect(this.notification.close).toHaveBeenCalled();
|
2016-07-01 08:44:38 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-10 04:09:05 +08:00
|
|
|
it("should play a sound when it gets new mail", () => {
|
|
|
|
spyOn(NylasEnv.config, "get").andCallFake((config) => {
|
|
|
|
if (config === "core.notifications.enabled") return true
|
|
|
|
if (config === "core.notifications.sounds") return true
|
|
|
|
return undefined;
|
|
|
|
});
|
|
|
|
|
|
|
|
spyOn(SoundRegistry, "playSound");
|
2017-09-25 01:09:38 +08:00
|
|
|
waitsForPromise(async () => {
|
|
|
|
await this.notifier._onDatabaseChanged({
|
|
|
|
objectClass: Message.name,
|
|
|
|
objects: [this.msg1],
|
|
|
|
})
|
|
|
|
expect(NylasEnv.config.get.calls[1].args[0]).toBe("core.notifications.sounds");
|
|
|
|
expect(SoundRegistry.playSound).toHaveBeenCalledWith("new-mail");
|
2016-05-10 04:09:05 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not play a sound if the config is off", () => {
|
|
|
|
spyOn(NylasEnv.config, "get").andCallFake((config) => {
|
|
|
|
if (config === "core.notifications.enabled") return true;
|
|
|
|
if (config === "core.notifications.sounds") return false;
|
|
|
|
return undefined;
|
|
|
|
});
|
|
|
|
spyOn(SoundRegistry, "playSound")
|
2017-09-25 01:09:38 +08:00
|
|
|
waitsForPromise(async () => {
|
|
|
|
await this.notifier._onDatabaseChanged({
|
|
|
|
objectClass: Message.name,
|
|
|
|
objects: [this.msg1],
|
|
|
|
})
|
|
|
|
expect(NylasEnv.config.get.calls[1].args[0]).toBe("core.notifications.sounds");
|
|
|
|
expect(SoundRegistry.playSound).not.toHaveBeenCalled()
|
2016-05-10 04:09:05 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not play a sound if other notiications are still in flight", () => {
|
|
|
|
spyOn(NylasEnv.config, "get").andCallFake((config) => {
|
|
|
|
if (config === "core.notifications.enabled") return true;
|
|
|
|
if (config === "core.notifications.sounds") return true;
|
|
|
|
return undefined;
|
|
|
|
});
|
2017-09-25 01:09:38 +08:00
|
|
|
waitsForPromise(async () => {
|
2016-05-10 04:09:05 +08:00
|
|
|
spyOn(SoundRegistry, "playSound")
|
2017-09-25 01:09:38 +08:00
|
|
|
await this.notifier._onDatabaseChanged({
|
|
|
|
objectClass: Message.name,
|
|
|
|
objects: [this.msg1, this.msg2],
|
|
|
|
})
|
|
|
|
expect(SoundRegistry.playSound).toHaveBeenCalled();
|
|
|
|
SoundRegistry.playSound.reset();
|
|
|
|
await this.notifier._onDatabaseChanged({
|
|
|
|
objectClass: Message.name,
|
|
|
|
objects: [this.msg3],
|
|
|
|
})
|
|
|
|
expect(SoundRegistry.playSound).not.toHaveBeenCalled();
|
2016-05-10 04:09:05 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|