2016-03-08 10:13:53 +08:00
|
|
|
import {
|
|
|
|
TaskFactory,
|
|
|
|
AccountStore,
|
|
|
|
CategoryStore,
|
|
|
|
Category,
|
|
|
|
Thread,
|
|
|
|
ChangeFolderTask,
|
|
|
|
ChangeLabelsTask,
|
|
|
|
} from 'nylas-exports'
|
|
|
|
|
|
|
|
|
2016-05-05 05:03:15 +08:00
|
|
|
describe('TaskFactory', function taskFactory() {
|
2016-05-06 13:30:34 +08:00
|
|
|
beforeEach(() => {
|
2016-03-08 10:13:53 +08:00
|
|
|
this.categories = {
|
|
|
|
'ac-1': {
|
2016-05-07 05:10:28 +08:00
|
|
|
archive: new Category({name: 'archive'}),
|
|
|
|
inbox: new Category({name: 'inbox1'}),
|
|
|
|
trash: new Category({name: 'trash1'}),
|
2016-03-08 10:13:53 +08:00
|
|
|
},
|
|
|
|
'ac-2': {
|
2016-05-07 05:10:28 +08:00
|
|
|
archive: new Category({name: 'all'}),
|
|
|
|
inbox: new Category({name: 'inbox2'}),
|
|
|
|
trash: new Category({name: 'trash2'}),
|
2016-03-08 10:13:53 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
this.accounts = {
|
|
|
|
'ac-1': {
|
|
|
|
id: 'ac-1',
|
2016-05-06 13:30:34 +08:00
|
|
|
usesFolders: () => true,
|
|
|
|
defaultFinishedCategory: () => this.categories['ac-1'].archive,
|
2016-03-08 10:13:53 +08:00
|
|
|
},
|
|
|
|
'ac-2': {
|
|
|
|
id: 'ac-2',
|
2016-05-06 13:30:34 +08:00
|
|
|
usesFolders: () => false,
|
|
|
|
defaultFinishedCategory: () => this.categories['ac-2'].trash,
|
2016-03-08 10:13:53 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
this.threads = [
|
|
|
|
new Thread({accountId: 'ac-1'}),
|
|
|
|
new Thread({accountId: 'ac-2'}),
|
|
|
|
]
|
|
|
|
|
2016-05-06 13:30:34 +08:00
|
|
|
spyOn(CategoryStore, 'getArchiveCategory').andCallFake((acc) => {
|
2016-03-08 10:13:53 +08:00
|
|
|
return this.categories[acc.id].archive
|
|
|
|
})
|
2016-05-06 13:30:34 +08:00
|
|
|
spyOn(CategoryStore, 'getInboxCategory').andCallFake((acc) => {
|
2016-03-08 10:13:53 +08:00
|
|
|
return this.categories[acc.id].inbox
|
|
|
|
})
|
2016-05-06 13:30:34 +08:00
|
|
|
spyOn(CategoryStore, 'getTrashCategory').andCallFake((acc) => {
|
2016-03-08 10:13:53 +08:00
|
|
|
return this.categories[acc.id].trash
|
|
|
|
})
|
2016-05-06 13:30:34 +08:00
|
|
|
spyOn(AccountStore, 'accountForId').andCallFake((accId) => {
|
2016-03-08 10:13:53 +08:00
|
|
|
return this.accounts[accId];
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2016-05-06 13:30:34 +08:00
|
|
|
describe('tasksForApplyingCategories', () => {
|
|
|
|
it('creates the correct tasks', () => {
|
|
|
|
const categoriesToRemove = (accId) => {
|
2016-03-08 10:13:53 +08:00
|
|
|
if (accId === 'ac-1') {
|
|
|
|
return [new Category({displayName: 'folder1', accountId: 'ac-1'})]
|
|
|
|
}
|
|
|
|
return [new Category({displayName: 'label2', accountId: 'ac-2'})]
|
|
|
|
}
|
2016-05-06 13:30:34 +08:00
|
|
|
const categoriesToAdd = (accId) => [this.categories[accId].inbox]
|
2016-03-08 10:13:53 +08:00
|
|
|
const taskDescription = 'dope'
|
|
|
|
|
|
|
|
const tasks = TaskFactory.tasksForApplyingCategories({
|
|
|
|
threads: this.threads,
|
|
|
|
categoriesToAdd,
|
|
|
|
categoriesToRemove,
|
|
|
|
taskDescription,
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(tasks.length).toEqual(2)
|
|
|
|
const taskExchange = tasks[0]
|
|
|
|
const taskGmail = tasks[1]
|
|
|
|
|
|
|
|
expect(taskExchange instanceof ChangeFolderTask).toBe(true)
|
|
|
|
expect(taskExchange.folder.name).toEqual('inbox1')
|
|
|
|
expect(taskExchange.taskDescription).toEqual(taskDescription)
|
|
|
|
|
|
|
|
expect(taskGmail instanceof ChangeLabelsTask).toBe(true)
|
|
|
|
expect(taskGmail.labelsToAdd.length).toEqual(1)
|
|
|
|
expect(taskGmail.labelsToAdd[0].name).toEqual('inbox2')
|
|
|
|
expect(taskGmail.labelsToRemove.length).toEqual(1)
|
|
|
|
expect(taskGmail.labelsToRemove[0].displayName).toEqual('label2')
|
|
|
|
expect(taskGmail.taskDescription).toEqual(taskDescription)
|
|
|
|
});
|
|
|
|
|
2016-05-06 13:30:34 +08:00
|
|
|
it('throws if threads are not instances of Thread', () => {
|
2016-03-08 10:13:53 +08:00
|
|
|
const threads = [
|
|
|
|
{accountId: 'ac-1'},
|
|
|
|
{accountId: 'ac-2'},
|
|
|
|
]
|
2016-05-06 13:30:34 +08:00
|
|
|
expect(() => {
|
2016-03-08 10:13:53 +08:00
|
|
|
TaskFactory.tasksForApplyingCategories({threads})
|
|
|
|
}).toThrow()
|
|
|
|
});
|
|
|
|
|
2016-05-06 13:30:34 +08:00
|
|
|
it('throws if categoriesToAdd does not return an array', () => {
|
|
|
|
expect(() => {
|
2016-03-08 10:13:53 +08:00
|
|
|
TaskFactory.tasksForApplyingCategories({
|
|
|
|
threads: this.threads,
|
|
|
|
categoriesToAdd: {displayName: 'cat1'},
|
|
|
|
})
|
|
|
|
}).toThrow()
|
|
|
|
});
|
|
|
|
|
2016-05-06 13:30:34 +08:00
|
|
|
it('throws if categoriesToAdd does not return an array', () => {
|
|
|
|
expect(() => {
|
2016-03-08 10:13:53 +08:00
|
|
|
TaskFactory.tasksForApplyingCategories({
|
|
|
|
threads: this.threads,
|
|
|
|
categoriesToRemove: {displayName: 'cat1'},
|
|
|
|
})
|
|
|
|
}).toThrow()
|
|
|
|
});
|
|
|
|
|
2016-05-06 13:30:34 +08:00
|
|
|
it('does not create folder tasks if categoriesToAdd not present', () => {
|
|
|
|
const categoriesToRemove = (accId) => {
|
2016-03-08 10:13:53 +08:00
|
|
|
if (accId === 'ac-1') {
|
|
|
|
return [new Category({displayName: 'folder1', accountId: 'ac-1'})]
|
|
|
|
}
|
|
|
|
return [new Category({displayName: 'label2', accountId: 'ac-2'})]
|
|
|
|
}
|
|
|
|
const taskDescription = 'dope'
|
|
|
|
|
|
|
|
const tasks = TaskFactory.tasksForApplyingCategories({
|
|
|
|
threads: this.threads,
|
|
|
|
categoriesToRemove,
|
|
|
|
taskDescription,
|
|
|
|
})
|
|
|
|
expect(tasks.length).toEqual(1)
|
|
|
|
const taskGmail = tasks[0]
|
|
|
|
expect(taskGmail instanceof ChangeLabelsTask).toBe(true)
|
|
|
|
expect(taskGmail.labelsToRemove.length).toEqual(1)
|
|
|
|
});
|
|
|
|
|
2016-05-06 13:30:34 +08:00
|
|
|
it('does not create label tasks if both categoriesToAdd and categoriesToRemove return empty', () => {
|
|
|
|
const categoriesToAdd = (accId) => {
|
2016-03-08 10:13:53 +08:00
|
|
|
return accId === 'ac-1' ? [this.categories[accId].inbox] : [];
|
|
|
|
}
|
|
|
|
const taskDescription = 'dope'
|
|
|
|
|
|
|
|
const tasks = TaskFactory.tasksForApplyingCategories({
|
|
|
|
threads: this.threads,
|
|
|
|
categoriesToAdd,
|
|
|
|
taskDescription,
|
|
|
|
})
|
|
|
|
expect(tasks.length).toEqual(1)
|
|
|
|
const taskExchange = tasks[0]
|
|
|
|
|
|
|
|
expect(taskExchange instanceof ChangeFolderTask).toBe(true)
|
|
|
|
expect(taskExchange.folder.name).toEqual('inbox1')
|
|
|
|
});
|
|
|
|
|
2016-05-06 13:30:34 +08:00
|
|
|
describe('exchange accounts', () => {
|
|
|
|
it('throws if folder is not a category', () => {
|
|
|
|
expect(() => {
|
2016-03-08 10:13:53 +08:00
|
|
|
TaskFactory.tasksForApplyingCategories({
|
|
|
|
threads: this.threads,
|
|
|
|
categoriesToAdd: () => [{accountId: 'ac-1', name: 'inbox'}],
|
|
|
|
})
|
|
|
|
}).toThrow()
|
|
|
|
});
|
|
|
|
|
2016-05-06 13:30:34 +08:00
|
|
|
it('throws if attempting to add more than one folder', () => {
|
|
|
|
expect(() => {
|
2016-03-08 10:13:53 +08:00
|
|
|
TaskFactory.tasksForApplyingCategories({
|
|
|
|
threads: this.threads,
|
|
|
|
categoriesToAdd: () => [{accountId: 'ac-1', name: 'inbox'}, {}],
|
|
|
|
})
|
|
|
|
}).toThrow()
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-06 13:30:34 +08:00
|
|
|
describe('taskForInvertingUnread', () => {
|
2016-03-08 10:13:53 +08:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2016-05-06 13:30:34 +08:00
|
|
|
describe('taskForInvertingStarred', () => {
|
2016-03-08 10:13:53 +08:00
|
|
|
|
|
|
|
});
|
|
|
|
});
|