-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
unit tests for notesSortOrderUtils and PerFolderSortOrderService
- Loading branch information
Showing
4 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
packages/app-desktop/services/sortOrder/PerFolderSortOrderService.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import PerFolderSortOrderService from './PerFolderSortOrderService'; | ||
import { setNotesSortOrder } from './notesSortOrderUtils'; | ||
import Setting from '@joplin/lib/models/Setting'; | ||
const { shimInit } = require('@joplin/lib/shim-init-node.js'); | ||
|
||
const folderId1 = 'aa012345678901234567890123456789'; | ||
const folderId2 = 'bb012345678901234567890123456789'; | ||
|
||
beforeAll(async (done) => { | ||
shimInit(); | ||
Setting.autoSaveEnabled = false; | ||
PerFolderSortOrderService.initialize(); | ||
Setting.setValue('notes.perFolderSortOrderEnabled', true); | ||
done(); | ||
}); | ||
|
||
describe('PerFolderSortOrderService', () => { | ||
|
||
test('get(), isSet() and set()', async (done) => { | ||
// Clear all per-folder sort order | ||
expect(PerFolderSortOrderService.isSet(folderId1)).toBe(false); | ||
expect(PerFolderSortOrderService.isSet(folderId2)).toBe(false); | ||
|
||
// Set shared sort order | ||
setNotesSortOrder('user_created_time', false); | ||
expect(Setting.value('notes.sortOrder.field')).toBe('user_created_time'); | ||
expect(Setting.value('notes.sortOrder.reverse')).toBe(false); | ||
|
||
// Manipulate per-folder sort order | ||
PerFolderSortOrderService.set(folderId1, true); | ||
expect(PerFolderSortOrderService.isSet(folderId1)).toBe(true); | ||
PerFolderSortOrderService.set(folderId1, false); | ||
expect(PerFolderSortOrderService.isSet(folderId1)).toBe(false); | ||
PerFolderSortOrderService.set(folderId1); | ||
expect(PerFolderSortOrderService.isSet(folderId1)).toBe(true); | ||
|
||
// Get per-folder sort order from a folder with per-folder sort order | ||
expect(PerFolderSortOrderService.get(folderId1)).toBeDefined(); | ||
|
||
// Folder without per-folder sort order has no per-folder sort order | ||
expect(PerFolderSortOrderService.get(folderId2)).toBeUndefined(); | ||
|
||
done(); | ||
}); | ||
}); |
85 changes: 85 additions & 0 deletions
85
packages/app-desktop/services/sortOrder/notesSortOrderUtils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { notesSortOrderFieldArray, notesSortOrderNextField, setNotesSortOrder } from './notesSortOrderUtils'; | ||
import Setting from '@joplin/lib/models/Setting'; | ||
const { shimInit } = require('@joplin/lib/shim-init-node.js'); | ||
|
||
beforeAll(() => { | ||
shimInit(); | ||
Setting.autoSaveEnabled = false; | ||
}); | ||
|
||
describe('notesSortOrderUtils', () => { | ||
|
||
it('should always provide the same ordered fields', async () => { | ||
const expected = ['user_updated_time', 'user_created_time', 'title', 'order']; | ||
expect(notesSortOrderFieldArray()).toStrictEqual(expected); | ||
expect(notesSortOrderFieldArray()).toStrictEqual(expected); | ||
}); | ||
|
||
it('should provide the next field cyclicly', async () => { | ||
expect(notesSortOrderNextField('user_updated_time')).toBe('user_created_time'); | ||
expect(notesSortOrderNextField('order')).toBe('user_updated_time'); | ||
}); | ||
|
||
test('setNoteSortOrder(), when perFieldReversalEnabled is false', async () => { | ||
Setting.setValue('notes.perFieldReversalEnabled', false); | ||
|
||
// It should set field and reverse of sort order. | ||
setNotesSortOrder('user_created_time', false); | ||
expect(Setting.value('notes.sortOrder.field')).toBe('user_created_time'); | ||
expect(Setting.value('notes.sortOrder.reverse')).toBe(false); | ||
setNotesSortOrder('user_updated_time', true); | ||
expect(Setting.value('notes.sortOrder.field')).toBe('user_updated_time'); | ||
expect(Setting.value('notes.sortOrder.reverse')).toBe(true); | ||
setNotesSortOrder('title', true); | ||
expect(Setting.value('notes.sortOrder.field')).toBe('title'); | ||
expect(Setting.value('notes.sortOrder.reverse')).toBe(true); | ||
|
||
// It should affect the current field of sort order, if arg1 is undefined. | ||
setNotesSortOrder(undefined, false); | ||
expect(Setting.value('notes.sortOrder.field')).toBe('title'); | ||
expect(Setting.value('notes.sortOrder.reverse')).toBe(false); | ||
|
||
// it should only set field of sort order, if arg2 is undefined. | ||
setNotesSortOrder('user_updated_time'); | ||
expect(Setting.value('notes.sortOrder.field')).toBe('user_updated_time'); | ||
expect(Setting.value('notes.sortOrder.reverse')).toBe(false); | ||
|
||
// It should select the next field, if arg1 and arg2 are undefined. | ||
setNotesSortOrder(); | ||
expect(Setting.value('notes.sortOrder.field')).toBe('user_created_time'); | ||
expect(Setting.value('notes.sortOrder.reverse')).toBe(false); | ||
}); | ||
|
||
test('setNoteSortOrder(), when perFieldReversalEnabled is true', async () => { | ||
Setting.setValue('notes.perFieldReversalEnabled', true); | ||
// It should set field and reverse of sort order. | ||
setNotesSortOrder('user_created_time', false); | ||
expect(Setting.value('notes.sortOrder.field')).toBe('user_created_time'); | ||
expect(Setting.value('notes.sortOrder.reverse')).toBe(false); | ||
setNotesSortOrder('user_updated_time', true); | ||
expect(Setting.value('notes.sortOrder.field')).toBe('user_updated_time'); | ||
expect(Setting.value('notes.sortOrder.reverse')).toBe(true); | ||
setNotesSortOrder('title', true); | ||
expect(Setting.value('notes.sortOrder.field')).toBe('title'); | ||
expect(Setting.value('notes.sortOrder.reverse')).toBe(true); | ||
|
||
// it should affect the current field of sort order, if arg1 is undefined. | ||
setNotesSortOrder(undefined, false); | ||
expect(Setting.value('notes.sortOrder.field')).toBe('title'); | ||
expect(Setting.value('notes.sortOrder.reverse')).toBe(false); | ||
|
||
// It should remember a reverse state, if arg2 is undefined. | ||
setNotesSortOrder('user_updated_time'); | ||
expect(Setting.value('notes.sortOrder.field')).toBe('user_updated_time'); | ||
expect(Setting.value('notes.sortOrder.reverse')).toBe(true); | ||
|
||
// It should select the next field and remember a reverse state, if arg1 and arg2 are undefined. | ||
setNotesSortOrder(); | ||
expect(Setting.value('notes.sortOrder.field')).toBe('user_created_time'); | ||
expect(Setting.value('notes.sortOrder.reverse')).toBe(false); | ||
}); | ||
|
||
it('should not accept an invalid field name', async () => { | ||
expect(() => setNotesSortOrder('hoge', true)).toThrow(); | ||
}); | ||
}); |