Display tests, increase Stryker threshold

This commit is contained in:
Mike Rodarte 2025-04-09 16:30:48 -04:00
parent 97eb313149
commit 48091a4d27
No known key found for this signature in database
GPG key ID: 32DCD8C789A592CC
4 changed files with 100 additions and 52 deletions

View file

@ -8,7 +8,17 @@ export class WordFrequencyDisplay {
private plugin: WordFrequencyPlugin;
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.view = view;
}
@ -22,8 +32,7 @@ export class WordFrequencyDisplay {
if (
blacklist.has(word) ||
count < this.plugin.settings.threshold ||
(this.filter !== '' &&
!word.toLowerCase().contains(this.filter.toLowerCase()))
!word.toLowerCase().includes(this.getFilter().toLowerCase())
) {
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) =>
debouncedMethod(event)
this.debouncedFilterInput(event)
);
}

View file

@ -302,10 +302,6 @@ describe('WordFrequencyCounter tests', () => {
expect(mockPlugin.registerEvent).toHaveBeenCalledWith('mock-event');
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', () => {

View file

@ -59,10 +59,39 @@ describe('WordFrequencyDisplay', () => {
});
describe('addWordToSidebar', () => {
it('should return early when the word is in the blacklist', () => {
const contentContainer = {
createEl: jest.fn(),
let buttonElement: HTMLButtonElement;
let contentContainer: HTMLDivElement;
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;
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 count = 17;
@ -72,37 +101,17 @@ describe('WordFrequencyDisplay', () => {
});
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 count = 1;
display.addWordToSidebar(blacklist, word, count, contentContainer);
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', () => {
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 count = 13;
@ -132,22 +141,46 @@ describe('WordFrequencyDisplay', () => {
expect(setIcon).toHaveBeenCalledWith(buttonElement, 'trash-2');
});
it('should update blacklist and call saveData and updateContent', () => {
const word = 'banana';
const originalBlacklist = mockPlugin.settings.blacklist;
it.todo('should verify the button click event handles the word');
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(
`${originalBlacklist},${word}`
display.addWordToSidebar(
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', () => {
@ -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();
});
});
});

View file

@ -9,7 +9,7 @@
"ignoreStatic": true,
"thresholds": {
"high": 90,
"low": 80,
"break": 75
"low": 85,
"break": 85
}
}