mirror of
https://github.com/mts7/obsidian-word-frequency.git
synced 2026-07-22 05:43:07 +00:00
Display tests, increase Stryker threshold
This commit is contained in:
parent
97eb313149
commit
48091a4d27
4 changed files with 100 additions and 52 deletions
|
|
@ -8,7 +8,17 @@ export class WordFrequencyDisplay {
|
||||||
private plugin: WordFrequencyPlugin;
|
private plugin: WordFrequencyPlugin;
|
||||||
private view: WordFrequencyView;
|
private view: WordFrequencyView;
|
||||||
|
|
||||||
constructor(plugin: WordFrequencyPlugin, view: WordFrequencyView) {
|
constructor(
|
||||||
|
plugin: WordFrequencyPlugin,
|
||||||
|
view: WordFrequencyView,
|
||||||
|
private getFilter: () => string = () => this.filter,
|
||||||
|
private debouncedFilterInput = debounce((event: Event) => {
|
||||||
|
const target = event.target as HTMLInputElement;
|
||||||
|
this.filter = target.value;
|
||||||
|
|
||||||
|
this.view.updateContent();
|
||||||
|
}, 500)
|
||||||
|
) {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
this.view = view;
|
this.view = view;
|
||||||
}
|
}
|
||||||
|
|
@ -22,8 +32,7 @@ export class WordFrequencyDisplay {
|
||||||
if (
|
if (
|
||||||
blacklist.has(word) ||
|
blacklist.has(word) ||
|
||||||
count < this.plugin.settings.threshold ||
|
count < this.plugin.settings.threshold ||
|
||||||
(this.filter !== '' &&
|
!word.toLowerCase().includes(this.getFilter().toLowerCase())
|
||||||
!word.toLowerCase().contains(this.filter.toLowerCase()))
|
|
||||||
) {
|
) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -60,15 +69,8 @@ export class WordFrequencyDisplay {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const debouncedMethod = debounce((event: Event) => {
|
|
||||||
const target = event.target as HTMLInputElement;
|
|
||||||
this.filter = target.value;
|
|
||||||
|
|
||||||
this.view.updateContent();
|
|
||||||
}, 500);
|
|
||||||
|
|
||||||
this.plugin.registerDomEvent(filterInput, 'input', (event) =>
|
this.plugin.registerDomEvent(filterInput, 'input', (event) =>
|
||||||
debouncedMethod(event)
|
this.debouncedFilterInput(event)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -302,10 +302,6 @@ describe('WordFrequencyCounter tests', () => {
|
||||||
expect(mockPlugin.registerEvent).toHaveBeenCalledWith('mock-event');
|
expect(mockPlugin.registerEvent).toHaveBeenCalledWith('mock-event');
|
||||||
expect(mockDebouncedEditorChange).toHaveBeenCalledWith(editorMock);
|
expect(mockDebouncedEditorChange).toHaveBeenCalledWith(editorMock);
|
||||||
});
|
});
|
||||||
|
|
||||||
it.todo('should trigger editor-change event to call callback');
|
|
||||||
|
|
||||||
it.todo('should verify call to triggerUpdateContent after 3 seconds');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('triggerUpdateContent', () => {
|
describe('triggerUpdateContent', () => {
|
||||||
|
|
|
||||||
|
|
@ -59,10 +59,39 @@ describe('WordFrequencyDisplay', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('addWordToSidebar', () => {
|
describe('addWordToSidebar', () => {
|
||||||
it('should return early when the word is in the blacklist', () => {
|
let buttonElement: HTMLButtonElement;
|
||||||
const contentContainer = {
|
let contentContainer: HTMLDivElement;
|
||||||
createEl: jest.fn(),
|
let innerElement: HTMLDivElement;
|
||||||
|
let rowElement: HTMLDivElement;
|
||||||
|
let spanElement: HTMLSpanElement;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
spanElement = {
|
||||||
|
setText: jest.fn(),
|
||||||
|
} as unknown as HTMLSpanElement;
|
||||||
|
buttonElement = {
|
||||||
|
addEventListener: jest.fn(),
|
||||||
|
} as unknown as HTMLButtonElement;
|
||||||
|
innerElement = {
|
||||||
|
createEl: jest
|
||||||
|
.fn()
|
||||||
|
.mockReturnValueOnce(spanElement)
|
||||||
|
.mockReturnValueOnce(spanElement)
|
||||||
|
.mockReturnValueOnce(buttonElement),
|
||||||
} as unknown as HTMLDivElement;
|
} as unknown as HTMLDivElement;
|
||||||
|
rowElement = {
|
||||||
|
createEl: jest.fn().mockReturnValue(innerElement),
|
||||||
|
} as unknown as HTMLDivElement;
|
||||||
|
contentContainer = {
|
||||||
|
createEl: jest.fn().mockReturnValue(rowElement),
|
||||||
|
} as unknown as HTMLDivElement;
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
jest.clearAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return early when the word is in the blacklist', () => {
|
||||||
const word = 'the';
|
const word = 'the';
|
||||||
const count = 17;
|
const count = 17;
|
||||||
|
|
||||||
|
|
@ -72,37 +101,17 @@ describe('WordFrequencyDisplay', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return early when the word count is less than the threshold setting', () => {
|
it('should return early when the word count is less than the threshold setting', () => {
|
||||||
const contentContainer = {
|
|
||||||
createEl: jest.fn(),
|
|
||||||
} as unknown as HTMLDivElement;
|
|
||||||
const word = 'banana';
|
const word = 'banana';
|
||||||
const count = 1;
|
const count = 1;
|
||||||
|
|
||||||
display.addWordToSidebar(blacklist, word, count, contentContainer);
|
display.addWordToSidebar(blacklist, word, count, contentContainer);
|
||||||
|
|
||||||
expect(contentContainer.createEl).not.toHaveBeenCalled();
|
expect(contentContainer.createEl).not.toHaveBeenCalled();
|
||||||
|
expect(setIcon).not.toHaveBeenCalled();
|
||||||
|
expect(mockPlugin.registerDomEvent).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should add the word with count and button to the row', () => {
|
it('should add the word with count and button to the row', () => {
|
||||||
const spanElement = {
|
|
||||||
setText: jest.fn(),
|
|
||||||
} as unknown as HTMLSpanElement;
|
|
||||||
const buttonElement = {
|
|
||||||
addEventListener: jest.fn(),
|
|
||||||
} as unknown as HTMLButtonElement;
|
|
||||||
const innerElement = {
|
|
||||||
createEl: jest
|
|
||||||
.fn()
|
|
||||||
.mockReturnValueOnce(spanElement)
|
|
||||||
.mockReturnValueOnce(spanElement)
|
|
||||||
.mockReturnValueOnce(buttonElement),
|
|
||||||
} as unknown as HTMLDivElement;
|
|
||||||
const rowElement = {
|
|
||||||
createEl: jest.fn().mockReturnValue(innerElement),
|
|
||||||
} as unknown as HTMLDivElement;
|
|
||||||
const contentContainer = {
|
|
||||||
createEl: jest.fn().mockReturnValue(rowElement),
|
|
||||||
} as unknown as HTMLDivElement;
|
|
||||||
const word = 'banana';
|
const word = 'banana';
|
||||||
const count = 13;
|
const count = 13;
|
||||||
|
|
||||||
|
|
@ -132,22 +141,46 @@ describe('WordFrequencyDisplay', () => {
|
||||||
expect(setIcon).toHaveBeenCalledWith(buttonElement, 'trash-2');
|
expect(setIcon).toHaveBeenCalledWith(buttonElement, 'trash-2');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should update blacklist and call saveData and updateContent', () => {
|
it.todo('should verify the button click event handles the word');
|
||||||
const word = 'banana';
|
|
||||||
const originalBlacklist = mockPlugin.settings.blacklist;
|
|
||||||
|
|
||||||
display.saveWordToBlacklist(word);
|
it('should add word if it matches the filter and has the threshold count', () => {
|
||||||
|
display = new WordFrequencyDisplay(
|
||||||
|
mockPlugin,
|
||||||
|
mockView,
|
||||||
|
() => 'BAN'
|
||||||
|
);
|
||||||
|
|
||||||
expect(mockPlugin.settings.blacklist).toBe(
|
display.addWordToSidebar(
|
||||||
`${originalBlacklist},${word}`
|
new Set(),
|
||||||
|
'banana',
|
||||||
|
mockPlugin.settings.threshold,
|
||||||
|
contentContainer
|
||||||
);
|
);
|
||||||
expect(mockPlugin.saveData).toHaveBeenCalledWith(
|
|
||||||
mockPlugin.settings
|
expect(contentContainer.createEl).toHaveBeenCalledWith('div', {
|
||||||
|
cls: ELEMENT_CLASSES.containerRow,
|
||||||
|
});
|
||||||
|
expect(setIcon).toHaveBeenCalledWith(buttonElement, 'trash-2');
|
||||||
|
expect(mockPlugin.registerDomEvent).toHaveBeenCalledWith(
|
||||||
|
buttonElement,
|
||||||
|
'click',
|
||||||
|
expect.any(Function)
|
||||||
);
|
);
|
||||||
expect(mockView.updateContent).toHaveBeenCalled();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it.todo('should verify the button click event handles the word');
|
it('should not add word if it does not match the filter', () => {
|
||||||
|
display = new WordFrequencyDisplay(
|
||||||
|
mockPlugin,
|
||||||
|
mockView,
|
||||||
|
() => 'xyz'
|
||||||
|
);
|
||||||
|
|
||||||
|
display.addWordToSidebar(new Set(), 'banana', 10, contentContainer);
|
||||||
|
|
||||||
|
expect(contentContainer.createEl).not.toHaveBeenCalled();
|
||||||
|
expect(setIcon).not.toHaveBeenCalled();
|
||||||
|
expect(mockPlugin.registerDomEvent).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('createFilter', () => {
|
describe('createFilter', () => {
|
||||||
|
|
@ -205,4 +238,21 @@ describe('WordFrequencyDisplay', () => {
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('saveWordToBlacklist', () => {
|
||||||
|
it('should update blacklist and call saveData and updateContent', () => {
|
||||||
|
const word = 'banana';
|
||||||
|
const originalBlacklist = mockPlugin.settings.blacklist;
|
||||||
|
|
||||||
|
display.saveWordToBlacklist(word);
|
||||||
|
|
||||||
|
expect(mockPlugin.settings.blacklist).toBe(
|
||||||
|
`${originalBlacklist},${word}`
|
||||||
|
);
|
||||||
|
expect(mockPlugin.saveData).toHaveBeenCalledWith(
|
||||||
|
mockPlugin.settings
|
||||||
|
);
|
||||||
|
expect(mockView.updateContent).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@
|
||||||
"ignoreStatic": true,
|
"ignoreStatic": true,
|
||||||
"thresholds": {
|
"thresholds": {
|
||||||
"high": 90,
|
"high": 90,
|
||||||
"low": 80,
|
"low": 85,
|
||||||
"break": 75
|
"break": 85
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue