From 48091a4d2710cf54993268fadfef796af358f202 Mon Sep 17 00:00:00 2001 From: Mike Rodarte Date: Wed, 9 Apr 2025 16:30:48 -0400 Subject: [PATCH] Display tests, increase Stryker threshold --- src/WordFrequencyDisplay.ts | 24 +++-- src/__tests__/WordFrequencyCounter.test.ts | 4 - src/__tests__/WordFrequencyDisplay.test.ts | 120 +++++++++++++++------ stryker.conf.json | 4 +- 4 files changed, 100 insertions(+), 52 deletions(-) diff --git a/src/WordFrequencyDisplay.ts b/src/WordFrequencyDisplay.ts index 42cdd75..badd6bf 100644 --- a/src/WordFrequencyDisplay.ts +++ b/src/WordFrequencyDisplay.ts @@ -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) ); } diff --git a/src/__tests__/WordFrequencyCounter.test.ts b/src/__tests__/WordFrequencyCounter.test.ts index fb288b0..c2764e0 100644 --- a/src/__tests__/WordFrequencyCounter.test.ts +++ b/src/__tests__/WordFrequencyCounter.test.ts @@ -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', () => { diff --git a/src/__tests__/WordFrequencyDisplay.test.ts b/src/__tests__/WordFrequencyDisplay.test.ts index 50faf06..eaf11dd 100644 --- a/src/__tests__/WordFrequencyDisplay.test.ts +++ b/src/__tests__/WordFrequencyDisplay.test.ts @@ -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(); + }); + }); }); diff --git a/stryker.conf.json b/stryker.conf.json index 7f4aba1..d3fa42f 100644 --- a/stryker.conf.json +++ b/stryker.conf.json @@ -9,7 +9,7 @@ "ignoreStatic": true, "thresholds": { "high": 90, - "low": 80, - "break": 75 + "low": 85, + "break": 85 } }