Skip to content

Commit

Permalink
refactor: adjust all wpm based tests to work properly
Browse files Browse the repository at this point in the history
  • Loading branch information
MhouneyLH committed Jan 21, 2024
1 parent 552b1f6 commit 492c4e5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/test/suite/keystroke_count_status_bar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ suite("KeystrokeCountStatusBar Test Suite", () => {
});

test(`For 5 keys pressed the status bar text is set to '${KEYBOARD_ICON} 5'`, () => {
TestUtils.generateIdenticalKeystrokes(repository, new Keystroke("a", 0), 5);
TestUtils.generateKeystrokesWithIncreasingTimestamps(repository, new Keystroke("a", 0), 5);

statusBar.update();

Expand Down
23 changes: 17 additions & 6 deletions src/test/suite/words_per_minute_calculator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ suite("WordsPerMinuteCalculator Test Suite", () => {
const now = Date.now();
const oneMinuteBefore: number = now - MINUTE_AS_MILLISECONDS;

TestUtils.generateIdenticalKeystrokes(repository, new Keystroke("a", oneMinuteBefore), 300);
TestUtils.generateKeystrokesWithIncreasingTimestamps(
repository,
new Keystroke("a", oneMinuteBefore),
300
);

const wpm = calculator.getAverageWordsPerMinute();
assert.strictEqual(Math.round(wpm), 60);
Expand All @@ -51,7 +55,12 @@ suite("WordsPerMinuteCalculator Test Suite", () => {
const now = Date.now();
const twoMinutesBefore: number = now - 2 * MINUTE_AS_MILLISECONDS;

TestUtils.generateIdenticalKeystrokes(repository, new Keystroke("a", twoMinutesBefore), 600);
TestUtils.generateKeystrokesWithIncreasingTimestamps(
repository,
new Keystroke("a", twoMinutesBefore),
600,
2 * MINUTE_AS_MILLISECONDS
);

const wpm = calculator.getAverageWordsPerMinute();
assert.strictEqual(Math.round(wpm), 60);
Expand All @@ -61,10 +70,11 @@ suite("WordsPerMinuteCalculator Test Suite", () => {
const now = Date.now();
const oneAndAHalfMinutesBefore: number = now - 1.5 * MINUTE_AS_MILLISECONDS;

TestUtils.generateIdenticalKeystrokes(
TestUtils.generateKeystrokesWithIncreasingTimestamps(
repository,
new Keystroke("a", oneAndAHalfMinutesBefore),
450
450,
1.5 * MINUTE_AS_MILLISECONDS
);

const wpm = calculator.getAverageWordsPerMinute();
Expand All @@ -75,10 +85,11 @@ suite("WordsPerMinuteCalculator Test Suite", () => {
const now = Date.now();
const thirtySecondsBefore: number = now - 0.5 * MINUTE_AS_MILLISECONDS;

TestUtils.generateIdenticalKeystrokes(
TestUtils.generateKeystrokesWithIncreasingTimestamps(
repository,
new Keystroke("a", thirtySecondsBefore),
150
150,
0.5 * MINUTE_AS_MILLISECONDS
);

const wpm = calculator.getAverageWordsPerMinute();
Expand Down
6 changes: 5 additions & 1 deletion src/test/suite/words_per_minute_status_bar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ suite("WordsPerMinuteStatusBar Test Suite", () => {
const now = Date.now();
const oneMinuteBefore: number = now - MINUTE_AS_MILLISECONDS;

TestUtils.generateIdenticalKeystrokes(repository, new Keystroke("a", oneMinuteBefore), 300);
TestUtils.generateKeystrokesWithIncreasingTimestamps(
repository,
new Keystroke("a", oneMinuteBefore),
300
);

statusBar.update();

Expand Down
11 changes: 8 additions & 3 deletions src/test/test_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@ import * as path from "path";

import { Keystroke } from "../libs/keystroke";
import { KeystrokeRepository } from "../libs/keystroke_repository";
import { MINUTE_AS_MILLISECONDS } from "../libs/constants";

export class TestUtils {
public static generateIdenticalKeystrokes(
public static generateKeystrokesWithIncreasingTimestamps(
repository: KeystrokeRepository,
keystroke: Keystroke,
count: number
count: number,
fullTimeInMilliseconds: number = MINUTE_AS_MILLISECONDS
): void {
let timeIterator = keystroke.timestampInMilliseconds;

for (let i = 0; i < count; i++) {
repository.addKeystroke(keystroke.key, keystroke.timestampInMilliseconds);
repository.addKeystroke(keystroke.key, timeIterator);
timeIterator += fullTimeInMilliseconds / count;
}
}

Expand Down

0 comments on commit 492c4e5

Please sign in to comment.