Skip to content

Commit

Permalink
Improve coverage further to where it can be for now
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiefdhurst committed Jan 3, 2025
1 parent bd39395 commit 4223eda
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 41 deletions.
1 change: 1 addition & 0 deletions src/__mocks__/obsidian.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export class Modal {
this.onClose();
}
onClose() {}
open() {}
setTitle(title: string) {
return this;
};
Expand Down
104 changes: 95 additions & 9 deletions src/__tests__/notice.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import { type Moment } from 'moment';
import { moment, Notice, TFile } from 'obsidian';
import { Modal, moment, Notice, TFile } from 'obsidian';
import InboxOrganiser from '..';
import { Inbox } from '../inbox';
import { OrganiserModal } from '../modal';
import { OrganiserNotice } from '../notice';
import { DEFAULT_SETTINGS, ISettings, Period } from '../settings';

jest.mock('obsidian');

describe('OrganiserNotice', () => {

let sut: OrganiserNotice;

let plugin: InboxOrganiser;
let modal: OrganiserModal;
let modal: Modal;
let inbox: Inbox;
let now: Moment;

Expand All @@ -22,14 +19,21 @@ describe('OrganiserNotice', () => {
beforeEach(() => {
plugin = jest.fn() as unknown as InboxOrganiser;
plugin.getSettings = (jest.fn()).mockReturnValue(settings);
modal = jest.fn() as unknown as OrganiserModal;
modal = jest.fn() as unknown as Modal;
modal.open = jest.fn();
inbox = jest.fn() as unknown as Inbox;
inbox.getFiles = jest.fn();
now = moment();
now = moment('2025-01-01T00:00:00');

sut = new OrganiserNotice(plugin, modal, inbox, now);
});

it('creates using default dependencies', () => {
sut = new OrganiserNotice(plugin, modal, inbox);

expect(sut).toBeInstanceOf(OrganiserNotice);
});

it('does nothing if inbox is disabled', () => {
const inboxGetFiles = jest.spyOn(inbox, 'getFiles');
settings.inbox = false;
Expand Down Expand Up @@ -68,7 +72,7 @@ describe('OrganiserNotice', () => {
const inboxGetFiles = jest.spyOn(inbox, 'getFiles').mockReturnValue([]);
settings.inbox = true;
settings.period = 'daily_9am';
now.set({hour: now.isDST() ? 10 : 9, minute: 1, second: 1});
now.set({hour: 9, minute: 1, second: 1});

sut.display();

Expand All @@ -80,12 +84,94 @@ describe('OrganiserNotice', () => {
const inboxGetFiles = jest.spyOn(inbox, 'getFiles').mockReturnValue([new TFile()]);
settings.inbox = true;
settings.period = 'daily_9am';
now.set({hour: now.isDST() ? 10 : 9, minute: 1, second: 1});
now.set({hour: 9, minute: 1, second: 1});

sut.display();

// TODO: The mock modal connected to this is currently not able to be spied upon - need to understand why
const fragment = (Notice as jest.Mock<Notice>).mock.calls[0][0] as DocumentFragment;
fragment.children[1].dispatchEvent(new Event('click'));

expect(inboxGetFiles).toHaveBeenCalled();
expect(Notice).toHaveBeenCalled();
});

it('works correctly under DST', () => {
const inboxGetFiles = jest.spyOn(inbox, 'getFiles').mockReturnValue([new TFile()]);
settings.inbox = true;
settings.period = 'daily_9am';
now.set({date: 1, month: 6, year: 2024, hour: 9, minute: 1, second: 1});

sut.display();

expect(inboxGetFiles).toHaveBeenCalled();
expect(Notice).toHaveBeenCalled();
});

it('works correctly for Mon 9am', () => {
const inboxGetFiles = jest.spyOn(inbox, 'getFiles').mockReturnValue([]);
settings.inbox = true;
settings.period = 'weekly_mon_9am';
now.set({date: 6, month: 0, year: 2025, hour: 9, minute: 1, second: 1});

sut.display();

expect(inboxGetFiles).toHaveBeenCalled();
});

it('works correctly for Mon 5pm', () => {
const inboxGetFiles = jest.spyOn(inbox, 'getFiles').mockReturnValue([]);
settings.inbox = true;
settings.period = 'weekly_mon_5pm';
now.set({date: 6, month: 0, year: 2025, hour: 17, minute: 1, second: 1});

sut.display();

expect(inboxGetFiles).toHaveBeenCalled();
});

it('does nothing for Mon 5pm when not Monday', () => {
const inboxGetFiles = jest.spyOn(inbox, 'getFiles').mockReturnValue([]);
settings.inbox = true;
settings.period = 'weekly_mon_5pm';
now.set({date: 7, month: 0, year: 2025, hour: 17, minute: 1, second: 1});

sut.display();

expect(inboxGetFiles).not.toHaveBeenCalled();
});

it('works correctly for Fri 9am', () => {
const inboxGetFiles = jest.spyOn(inbox, 'getFiles').mockReturnValue([]);
settings.inbox = true;
settings.period = 'weekly_fri_9am';
now.set({date: 3, month: 0, year: 2025, hour: 9, minute: 1, second: 1});

sut.display();

expect(inboxGetFiles).toHaveBeenCalled();
});

it('works correctly for Fri 5pm', () => {
const inboxGetFiles = jest.spyOn(inbox, 'getFiles').mockReturnValue([]);
settings.inbox = true;
settings.period = 'weekly_fri_5pm';
now.set({date: 3, month: 0, year: 2025, hour: 17, minute: 1, second: 1});

sut.display();

expect(inboxGetFiles).toHaveBeenCalled();
});

it('does nothing for Fri 5pm when not Friday', () => {
const inboxGetFiles = jest.spyOn(inbox, 'getFiles').mockReturnValue([]);
settings.inbox = true;
settings.period = 'weekly_fri_5pm';
now.set({date: 7, month: 0, year: 2025, hour: 17, minute: 1, second: 1});

sut.display();

expect(inboxGetFiles).not.toHaveBeenCalled();
});

});
28 changes: 8 additions & 20 deletions src/modal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export class OrganiserModal extends Modal {
this.createFilter(mainContainerEl);
this.createMultiSelect(mainContainerEl);
this.createFileTable(mainContainerEl);
this.createFileTableRows();
this.handleSearch('');
this.handleToggleSelectMulti(false);
}
Expand Down Expand Up @@ -98,7 +99,9 @@ export class OrganiserModal extends Modal {
theadTrEl.createEl('th', { text: 'Name' });
theadTrEl.createEl('th', { text: 'Move to...' });
this.fileTbodyEl = tableEl.createEl('tbody');
}

createFileTableRows(): void {
for (const file of this.files) {
const fileTrEl = this.fileTbodyEl.createEl('tr');

Expand Down Expand Up @@ -134,22 +137,7 @@ export class OrganiserModal extends Modal {

return folderSelectEl;
}

getFolderPathForDisplay(folder: TFolder): string {
if (folder.parent?.path === '/') {
return folder.name;
}

const parentNames = [];
let parent: TFolder | null = folder.parent;
while (parent !== null && parent.path !== '/') {
parentNames.push(parent.name);
parent = parent.parent;
}

return `${folder.name} (${parentNames.reverse().join(' > ')})`;
}


handleSearch(query: string): void {
[...this.fileRowEls.entries()].forEach(([fileName, row]) => {
const fileNameSearch = fileName.toLowerCase();
Expand Down Expand Up @@ -199,12 +187,12 @@ export class OrganiserModal extends Modal {
await this.inbox.move(file, path);

this.files.remove(file);
this.fileRowEls.get(fileName)?.remove();
this.fileRowEls.delete(fileName);
this.fileRowSelectEls.delete(fileName);

if (this.fileRowEls.size === 0) {
if (this.files.length === 0) {
this.close();
} else {
this.fileTbodyEl.empty();
this.createFileTableRows();
}
}

Expand Down
24 changes: 12 additions & 12 deletions src/notice.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { type Moment } from 'moment';
import { moment, Notice } from 'obsidian';
import { Modal, moment, Notice } from 'obsidian';
import InboxOrganiser from '.';
import { Inbox } from './inbox';
import { OrganiserModal } from './modal';

export class OrganiserNotice {
private plugin: InboxOrganiser;
private modal: OrganiserModal;
private modal: Modal;
private inbox: Inbox;
private now: Moment;

constructor(plugin: InboxOrganiser, modal: OrganiserModal, inbox: Inbox, now?: Moment) {
constructor(plugin: InboxOrganiser, modal: Modal, inbox: Inbox, now?: Moment) {
this.plugin = plugin;
this.modal = modal;
this.inbox = inbox;
Expand All @@ -19,6 +18,7 @@ export class OrganiserNotice {

private getFragment(): DocumentFragment {
const fragment = new DocumentFragment();

fragment.createEl('span', { text: 'This is a reminder to organise all the files within your inbox folder: click ' });
fragment.createEl('a', { text: 'here' }).addEventListener('click', () => this.modal.open());
fragment.createEl('span', { text: ' to get started.' });
Expand All @@ -33,28 +33,28 @@ export class OrganiserNotice {
return;
}

if (settings.period === 'daily_9am' && !this.now.isBetween(moment('09:00:00', 'HH:mm:ss'), moment('09:09:59', 'HH:mm:ss'))) {
if (settings.period === 'daily_9am' && !this.now.isBetween(this.now.clone().set({hour: 9, minute: 0, second: 0}), this.now.clone().set({hour: 9, minute: 9, second: 59}))) {
return;
}
if (settings.period === 'daily_11am' && !this.now.isBetween(moment('11:00:00', 'HH:mm:ss'), moment('11:09:59', 'HH:mm:ss'))) {
if (settings.period === 'daily_11am' && !this.now.isBetween(this.now.clone().set({hour: 11, minute: 0, second: 0}), this.now.clone().set({hour: 11, minute: 9, second: 59}))) {
return;
}
if (settings.period === 'daily_3pm' && !this.now.isBetween(moment('15:00:00', 'HH:mm:ss'), moment('15:09:59', 'HH:mm:ss'))) {
if (settings.period === 'daily_3pm' && !this.now.isBetween(this.now.clone().set({hour: 15, minute: 0, second: 0}), this.now.clone().set({hour: 15, minute: 9, second: 59}))) {
return;
}
if (settings.period === 'daily_5pm' && !this.now.isBetween(moment('17:00:00', 'HH:mm:ss'), moment('17:09:59', 'HH:mm:ss'))) {
if (settings.period === 'daily_5pm' && !this.now.isBetween(this.now.clone().set({hour: 17, minute: 0, second: 0}), this.now.clone().set({hour: 17, minute: 9, second: 59}))) {
return;
}
if (settings.period === 'weekly_mon_9am' && (this.now.format('dd') !== 'Mo' || !this.now.isBetween(moment('09:00:00', 'HH:mm:ss'), moment('09:09:59', 'HH:mm:ss')))) {
if (settings.period === 'weekly_mon_9am' && (this.now.format('dd') !== 'Mo' || !this.now.isBetween(this.now.clone().set({hour: 9, minute: 0, second: 0}), this.now.clone().set({hour: 9, minute: 9, second: 59})))) {
return;
}
if (settings.period === 'weekly_mon_5pm' && (this.now.format('dd') !== 'Mo' || !this.now.isBetween(moment('17:00:00', 'HH:mm:ss'), moment('17:09:59', 'HH:mm:ss')))) {
if (settings.period === 'weekly_mon_5pm' && (this.now.format('dd') !== 'Mo' || !this.now.isBetween(this.now.clone().set({hour: 17, minute: 0, second: 0}), this.now.clone().set({hour: 17, minute: 9, second: 59})))) {
return;
}
if (settings.period === 'weekly_fri_9am' && (this.now.format('dd') !== 'Fr' || !this.now.isBetween(moment('09:00:00', 'HH:mm:ss'), moment('09:09:59', 'HH:mm:ss')))) {
if (settings.period === 'weekly_fri_9am' && (this.now.format('dd') !== 'Fr' || !this.now.isBetween(this.now.clone().set({hour: 9, minute: 0, second: 0}), this.now.clone().set({hour: 9, minute: 9, second: 59})))) {
return;
}
if (settings.period === 'weekly_fri_5pm' && (this.now.format('dd') !== 'Fr' || !this.now.isBetween(moment('17:00:00', 'HH:mm:ss'), moment('17:09:59', 'HH:mm:ss')))) {
if (settings.period === 'weekly_fri_5pm' && (this.now.format('dd') !== 'Fr' || !this.now.isBetween(this.now.clone().set({hour: 17, minute: 0, second: 0}), this.now.clone().set({hour: 17, minute: 9, second: 59})))) {
return;
}

Expand Down

0 comments on commit 4223eda

Please sign in to comment.