Skip to content

Commit

Permalink
unit tests for notesSortOrderUtils and PerFolderSortOrderService
Browse files Browse the repository at this point in the history
  • Loading branch information
ken1kob committed Nov 2, 2021
1 parent fd2d5f9 commit 05e7846
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -727,9 +727,15 @@ packages/app-desktop/services/plugins/hooks/useWebviewToPluginMessages.js.map
packages/app-desktop/services/sortOrder/PerFolderSortOrderService.d.ts
packages/app-desktop/services/sortOrder/PerFolderSortOrderService.js
packages/app-desktop/services/sortOrder/PerFolderSortOrderService.js.map
packages/app-desktop/services/sortOrder/PerFolderSortOrderService.test.d.ts
packages/app-desktop/services/sortOrder/PerFolderSortOrderService.test.js
packages/app-desktop/services/sortOrder/PerFolderSortOrderService.test.js.map
packages/app-desktop/services/sortOrder/notesSortOrderUtils.d.ts
packages/app-desktop/services/sortOrder/notesSortOrderUtils.js
packages/app-desktop/services/sortOrder/notesSortOrderUtils.js.map
packages/app-desktop/services/sortOrder/notesSortOrderUtils.test.d.ts
packages/app-desktop/services/sortOrder/notesSortOrderUtils.test.js
packages/app-desktop/services/sortOrder/notesSortOrderUtils.test.js.map
packages/app-desktop/services/spellChecker/SpellCheckerServiceDriverNative.d.ts
packages/app-desktop/services/spellChecker/SpellCheckerServiceDriverNative.js
packages/app-desktop/services/spellChecker/SpellCheckerServiceDriverNative.js.map
Expand Down
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -712,9 +712,15 @@ packages/app-desktop/services/plugins/hooks/useWebviewToPluginMessages.js.map
packages/app-desktop/services/sortOrder/PerFolderSortOrderService.d.ts
packages/app-desktop/services/sortOrder/PerFolderSortOrderService.js
packages/app-desktop/services/sortOrder/PerFolderSortOrderService.js.map
packages/app-desktop/services/sortOrder/PerFolderSortOrderService.test.d.ts
packages/app-desktop/services/sortOrder/PerFolderSortOrderService.test.js
packages/app-desktop/services/sortOrder/PerFolderSortOrderService.test.js.map
packages/app-desktop/services/sortOrder/notesSortOrderUtils.d.ts
packages/app-desktop/services/sortOrder/notesSortOrderUtils.js
packages/app-desktop/services/sortOrder/notesSortOrderUtils.js.map
packages/app-desktop/services/sortOrder/notesSortOrderUtils.test.d.ts
packages/app-desktop/services/sortOrder/notesSortOrderUtils.test.js
packages/app-desktop/services/sortOrder/notesSortOrderUtils.test.js.map
packages/app-desktop/services/spellChecker/SpellCheckerServiceDriverNative.d.ts
packages/app-desktop/services/spellChecker/SpellCheckerServiceDriverNative.js
packages/app-desktop/services/spellChecker/SpellCheckerServiceDriverNative.js.map
Expand Down
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();
});
});
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();
});
});

0 comments on commit 05e7846

Please sign in to comment.