Mailspring/app/spec/mailbox-perspective-spec.es6

235 lines
8.9 KiB
Text
Raw Normal View History

import { AccountStore, MailboxPerspective, TaskFactory, Label, CategoryStore } from 'mailspring-exports';
describe('MailboxPerspective', function mailboxPerspective() {
2016-05-06 13:30:34 +08:00
beforeEach(() => {
2017-09-27 02:33:08 +08:00
this.accountIds = ['a1', 'a2'];
this.accounts = {
2016-05-07 07:06:16 +08:00
a1: {
id: 'a1',
2017-09-27 02:33:08 +08:00
preferredRemovalDestination: () => ({ displayName: 'archive' }),
},
2016-05-07 07:06:16 +08:00
a2: {
id: 'a2',
2017-09-27 02:33:08 +08:00
preferredRemovalDestination: () => ({ displayName: 'trash2' }),
},
2017-09-27 02:33:08 +08:00
};
this.perspective = new MailboxPerspective(this.accountIds);
spyOn(AccountStore, 'accountForId').andCallFake(accId => this.accounts[accId]);
});
2016-05-06 13:30:34 +08:00
describe('isEqual', () => {
// TODO
});
2016-05-06 13:30:34 +08:00
describe('canArchiveThreads', () => {
it('returns false if the perspective is archive', () => {
2017-09-27 02:33:08 +08:00
const accounts = [{ canArchiveThreads: () => true }, { canArchiveThreads: () => true }];
spyOn(AccountStore, 'accountsForItems').andReturn(accounts);
spyOn(this.perspective, 'isArchive').andReturn(true);
expect(this.perspective.canArchiveThreads()).toBe(false);
});
2016-05-06 13:30:34 +08:00
it('returns false if one of the accounts associated with the threads cannot archive', () => {
2017-09-27 02:33:08 +08:00
const accounts = [{ canArchiveThreads: () => true }, { canArchiveThreads: () => false }];
spyOn(AccountStore, 'accountsForItems').andReturn(accounts);
spyOn(this.perspective, 'isArchive').andReturn(false);
expect(this.perspective.canArchiveThreads()).toBe(false);
});
2016-05-06 13:30:34 +08:00
it('returns true otherwise', () => {
2017-09-27 02:33:08 +08:00
const accounts = [{ canArchiveThreads: () => true }, { canArchiveThreads: () => true }];
spyOn(AccountStore, 'accountsForItems').andReturn(accounts);
spyOn(this.perspective, 'isArchive').andReturn(false);
expect(this.perspective.canArchiveThreads()).toBe(true);
});
});
2016-05-06 13:30:34 +08:00
describe('canMoveThreadsTo', () => {
it('returns false if the perspective is the target folder', () => {
2017-09-27 02:33:08 +08:00
const accounts = [{ id: 'a' }, { id: 'b' }];
spyOn(AccountStore, 'accountsForItems').andReturn(accounts);
spyOn(this.perspective, 'categoriesSharedRole').andReturn('trash');
expect(this.perspective.canMoveThreadsTo([], 'trash')).toBe(false);
});
2016-05-06 13:30:34 +08:00
it('returns false if one of the accounts associated with the threads does not have the folder', () => {
2017-09-27 02:33:08 +08:00
const accounts = [{ id: 'a' }, { id: 'b' }];
spyOn(CategoryStore, 'getCategoryByRole').andReturn(null);
spyOn(AccountStore, 'accountsForItems').andReturn(accounts);
spyOn(this.perspective, 'categoriesSharedRole').andReturn('inbox');
expect(this.perspective.canMoveThreadsTo([], 'trash')).toBe(false);
});
2016-05-06 13:30:34 +08:00
it('returns true otherwise', () => {
2017-09-27 02:33:08 +08:00
const accounts = [{ id: 'a' }, { id: 'b' }];
const category = { id: 'cat' };
spyOn(CategoryStore, 'getCategoryByRole').andReturn(category);
spyOn(AccountStore, 'accountsForItems').andReturn(accounts);
spyOn(this.perspective, 'categoriesSharedRole').andReturn('inbox');
expect(this.perspective.canMoveThreadsTo([], 'trash')).toBe(true);
});
});
2016-05-06 13:30:34 +08:00
describe('canReceiveThreadsFromAccountIds', () => {
it('returns true if the thread account ids are included in the current account ids', () => {
2017-09-27 02:33:08 +08:00
expect(this.perspective.canReceiveThreadsFromAccountIds(['a1'])).toBe(true);
});
2016-05-06 13:30:34 +08:00
it('returns false otherwise', () => {
2017-09-27 02:33:08 +08:00
expect(this.perspective.canReceiveThreadsFromAccountIds(['a4'])).toBe(false);
expect(this.perspective.canReceiveThreadsFromAccountIds([])).toBe(false);
expect(this.perspective.canReceiveThreadsFromAccountIds()).toBe(false);
});
});
// todo bg
xdescribe('tasksForRemovingItems', () => {
2016-05-06 13:30:34 +08:00
beforeEach(() => {
this.categories = {
2016-05-07 07:06:16 +08:00
a1: {
2017-09-27 02:33:08 +08:00
archive: new Label({ role: 'archive', path: 'archive', accountId: 'a1' }),
inbox: new Label({ role: 'inbox', path: 'inbox1', accountId: 'a1' }),
trash: new Label({ role: 'trash', path: 'trash1', accountId: 'a1' }),
category: new Label({ role: null, path: 'folder1', accountId: 'a1' }),
},
2016-05-07 07:06:16 +08:00
a2: {
2017-09-27 02:33:08 +08:00
archive: new Label({ role: 'all', path: 'all', accountId: 'a2' }),
inbox: new Label({ role: 'inbox', path: 'inbox2', accountId: 'a2' }),
trash: new Label({ role: 'trash', path: 'trash2', accountId: 'a2' }),
category: new Label({ role: null, path: 'label2', accountId: 'a2' }),
},
2017-09-27 02:33:08 +08:00
};
this.threads = [{ accountId: 'a1' }, { accountId: 'a2' }];
spyOn(TaskFactory, 'tasksForApplyingCategories');
spyOn(CategoryStore, 'getTrashCategory').andCallFake(accId => {
return this.categories[accId].trash;
});
});
function assertMoved(accId) {
2017-09-27 02:33:08 +08:00
expect(TaskFactory.tasksForApplyingCategories).toHaveBeenCalled();
const { args } = TaskFactory.tasksForApplyingCategories.calls[0];
const { categoriesToRemove, categoriesToAdd } = args[0];
const assertor = {
from(originName) {
2017-09-27 02:33:08 +08:00
expect(categoriesToRemove(accId)[0].displayName).toEqual(originName);
return assertor;
},
to(destinationName) {
2017-09-27 02:33:08 +08:00
expect(categoriesToAdd(accId)[0].displayName).toEqual(destinationName);
return assertor;
},
2017-09-27 02:33:08 +08:00
};
return assertor;
}
2016-05-06 13:30:34 +08:00
it('moves to finished category if viewing inbox', () => {
const perspective = MailboxPerspective.forCategories([
this.categories.a1.inbox,
this.categories.a2.inbox,
2017-09-27 02:33:08 +08:00
]);
perspective.tasksForRemovingItems(this.threads);
assertMoved('a1')
.from('inbox1')
.to('archive');
assertMoved('a2')
.from('inbox2')
.to('trash2');
});
2016-05-06 13:30:34 +08:00
it('moves to trash if viewing archive', () => {
const perspective = MailboxPerspective.forCategories([
this.categories.a1.archive,
this.categories.a2.archive,
2017-09-27 02:33:08 +08:00
]);
perspective.tasksForRemovingItems(this.threads);
assertMoved('a1')
.from('archive')
.to('trash1');
assertMoved('a2')
.from('all')
.to('trash2');
});
2016-05-06 13:30:34 +08:00
it('deletes permanently if viewing trash', () => {
// TODO
// Not currently possible
});
2016-05-06 13:30:34 +08:00
it('moves to default finished category if viewing category', () => {
const perspective = MailboxPerspective.forCategories([
this.categories.a1.category,
this.categories.a2.category,
2017-09-27 02:33:08 +08:00
]);
perspective.tasksForRemovingItems(this.threads);
assertMoved('a1')
.from('folder1')
.to('archive');
assertMoved('a2')
.from('label2')
.to('trash2');
});
2016-05-06 13:30:34 +08:00
it('unstars if viewing starred', () => {
2017-09-27 02:33:08 +08:00
spyOn(TaskFactory, 'taskForInvertingStarred').andReturn({ some: 'task' });
const perspective = MailboxPerspective.forStarred(this.accountIds);
const tasks = perspective.tasksForRemovingItems(this.threads);
expect(tasks).toEqual([{ some: 'task' }]);
});
2016-05-06 13:30:34 +08:00
it('does nothing when viewing spam or sent', () => {
2017-09-27 02:33:08 +08:00
['spam', 'sent'].forEach(invalid => {
const perspective = MailboxPerspective.forCategories([
2017-09-27 02:33:08 +08:00
new Label({ role: invalid, accountId: 'a1' }),
new Label({ role: invalid, accountId: 'a2' }),
]);
const tasks = perspective.tasksForRemovingItems(this.threads);
expect(TaskFactory.tasksForApplyingCategories).not.toHaveBeenCalled();
expect(tasks).toEqual([]);
});
});
2016-05-06 13:30:34 +08:00
describe('when perspective is category perspective', () => {
it('does not create tasks if any name in the ruleset is null', () => {
2017-09-27 02:33:08 +08:00
const perspective = MailboxPerspective.forCategories([this.categories.a1.category]);
spyOn(perspective, 'categoriesSharedRole').andReturn('all');
const tasks = perspective.tasksForRemovingItems(this.threads);
expect(tasks).toEqual([]);
});
});
});
2016-05-06 13:30:34 +08:00
describe('CategoryMailboxPerspective', () => {
beforeEach(() => {
this.categories = [
2017-09-27 02:33:08 +08:00
new Label({ path: 'c1', accountId: 'a1' }),
new Label({ path: 'c2', accountId: 'a2' }),
new Label({ path: 'c3', accountId: 'a2' }),
];
this.perspective = MailboxPerspective.forCategories(this.categories);
});
2016-05-06 13:30:34 +08:00
describe('canReceiveThreadsFromAccountIds', () => {
it('returns true if the thread account ids are included in the current account ids', () => {
2017-09-27 02:33:08 +08:00
expect(this.perspective.canReceiveThreadsFromAccountIds(['a1'])).toBe(true);
});
2016-05-06 13:30:34 +08:00
it('returns false otherwise', () => {
2017-09-27 02:33:08 +08:00
expect(this.perspective.canReceiveThreadsFromAccountIds(['a4'])).toBe(false);
expect(this.perspective.canReceiveThreadsFromAccountIds([])).toBe(false);
expect(this.perspective.canReceiveThreadsFromAccountIds()).toBe(false);
});
2016-05-06 13:30:34 +08:00
it('returns false if it is a locked category', () => {
2017-09-27 02:33:08 +08:00
this.perspective._categories.push(new Label({ role: 'sent', path: 'c4', accountId: 'a1' }));
expect(this.perspective.canReceiveThreadsFromAccountIds(['a2'])).toBe(false);
});
});
2016-05-06 13:30:34 +08:00
describe('receiveThreads', () => {
// TODO
});
});
});