Skip to content

Commit 8bce7b5

Browse files
Use a jest.fn() to track calls to the scrubber, instead of a local variable counter
1 parent 28a0075 commit 8bce7b5

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

extensions/ql-vscode/src/query-history/query-history-scrubber.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ import { getErrorMessage } from "../common/helpers-pure";
1010

1111
const LAST_SCRUB_TIME_KEY = "lastScrubTime";
1212

13-
type Counter = {
14-
increment: () => void;
15-
};
16-
1713
/**
1814
* Registers an interval timer that will periodically check for queries old enought
1915
* to be deleted.
@@ -37,8 +33,8 @@ export function registerQueryHistoryScrubber(
3733
qhm: QueryHistoryManager,
3834
ctx: ExtensionContext,
3935

40-
// optional counter to keep track of how many times the scrubber has run
41-
counter?: Counter,
36+
// optional callback to keep track of how many times the scrubber has run
37+
onScrubberRun?: () => void,
4238
): Disposable {
4339
const deregister = setInterval(
4440
scrubQueries,
@@ -48,7 +44,7 @@ export function registerQueryHistoryScrubber(
4844
queryHistoryDirs,
4945
qhm,
5046
ctx,
51-
counter,
47+
onScrubberRun,
5248
);
5349

5450
return {
@@ -64,7 +60,7 @@ async function scrubQueries(
6460
queryHistoryDirs: QueryHistoryDirs,
6561
qhm: QueryHistoryManager,
6662
ctx: ExtensionContext,
67-
counter?: Counter,
63+
onScrubberRun?: () => void,
6864
) {
6965
const lastScrubTime = ctx.globalState.get<number>(LAST_SCRUB_TIME_KEY);
7066
const now = Date.now();
@@ -76,7 +72,7 @@ async function scrubQueries(
7672

7773
let scrubCount = 0; // total number of directories deleted
7874
try {
79-
counter?.increment();
75+
onScrubberRun?.();
8076
void extLogger.log(
8177
"Cleaning up query history directories. Removing old entries.",
8278
);

extensions/ql-vscode/test/vscode-tests/no-workspace/query-history/query-history-scrubber.test.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ const LESS_THAN_ONE_DAY = ONE_DAY_IN_MS - 1000;
2121

2222
describe("query history scrubber", () => {
2323
let deregister: vscode.Disposable | undefined;
24-
let runCount = 0;
2524

2625
const tmpDir = dirSync({
2726
unsafeCleanup: true,
@@ -45,27 +44,27 @@ describe("query history scrubber", () => {
4544

4645
it("should not throw an error when the query directory does not exist", async () => {
4746
const mockCtx = createMockContext();
48-
registerScrubber("idontexist", mockCtx);
47+
const runCounter = registerScrubber("idontexist", mockCtx);
4948

5049
jest.advanceTimersByTime(ONE_HOUR_IN_MS);
5150
await wait();
5251
// "Should not have called the scrubber"
53-
expect(runCount).toBe(0);
52+
expect(runCounter).toHaveBeenCalledTimes(0);
5453

5554
jest.advanceTimersByTime(ONE_HOUR_IN_MS - 1);
5655
await wait();
5756
// "Should not have called the scrubber"
58-
expect(runCount).toBe(0);
57+
expect(runCounter).toHaveBeenCalledTimes(0);
5958

6059
jest.advanceTimersByTime(1);
6160
await wait();
6261
// "Should have called the scrubber once"
63-
expect(runCount).toBe(1);
62+
expect(runCounter).toHaveBeenCalledTimes(1);
6463

6564
jest.advanceTimersByTime(TWO_HOURS_IN_MS);
6665
await wait();
6766
// "Should have called the scrubber a second time"
68-
expect(runCount).toBe(2);
67+
expect(runCounter).toHaveBeenCalledTimes(2);
6968

7069
expect((mockCtx.globalState as any).lastScrubTime).toBe(
7170
now + TWO_HOURS_IN_MS * 2,
@@ -178,7 +177,11 @@ describe("query history scrubber", () => {
178177
} as any as vscode.ExtensionContext;
179178
}
180179

181-
function registerScrubber(dir: string, ctx: vscode.ExtensionContext) {
180+
function registerScrubber(
181+
dir: string,
182+
ctx: vscode.ExtensionContext,
183+
): jest.Mock {
184+
const onScrubberRun = jest.fn();
182185
deregister = registerQueryHistoryScrubber(
183186
ONE_HOUR_IN_MS,
184187
TWO_HOURS_IN_MS,
@@ -190,10 +193,9 @@ describe("query history scrubber", () => {
190193
},
191194
}),
192195
ctx,
193-
{
194-
increment: () => runCount++,
195-
},
196+
onScrubberRun,
196197
);
198+
return onScrubberRun;
197199
}
198200

199201
async function wait(ms = 500) {

0 commit comments

Comments
 (0)