-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(core): basic tests for websockets
- Loading branch information
1 parent
75ab5c6
commit f687a73
Showing
1 changed file
with
67 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,68 @@ | ||
import type { Store } from "../types"; | ||
import { webSocketLimiter } from "../websocket"; | ||
import { keyGenerator } from "./helpers"; | ||
|
||
// TODO: Write tests | ||
describe("websockets middleware test", () => {}); | ||
describe("websockets middleware test", () => { | ||
beforeEach(() => { | ||
vi.useFakeTimers(); | ||
}); | ||
afterEach(() => { | ||
vi.useRealTimers(); | ||
vi.restoreAllMocks(); | ||
}); | ||
|
||
class MockStore implements Store { | ||
initWasCalled = false; | ||
incrementWasCalled = false; | ||
decrementWasCalled = false; | ||
resetKeyWasCalled = false; | ||
getWasCalled = false; | ||
resetAllWasCalled = false; | ||
|
||
counter = 0; | ||
|
||
init(): void { | ||
this.initWasCalled = true; | ||
} | ||
|
||
async get() { | ||
this.getWasCalled = true; | ||
|
||
return { totalHits: this.counter, resetTime: undefined }; | ||
} | ||
|
||
async increment() { | ||
this.counter += 1; | ||
this.incrementWasCalled = true; | ||
|
||
return { totalHits: this.counter, resetTime: undefined }; | ||
} | ||
|
||
async decrement() { | ||
this.counter -= 1; | ||
this.decrementWasCalled = true; | ||
} | ||
|
||
async resetKey() { | ||
this.resetKeyWasCalled = true; | ||
} | ||
|
||
async resetAll() { | ||
this.resetAllWasCalled = true; | ||
} | ||
} | ||
|
||
it("should not modify the options object passed", () => { | ||
const options = {}; | ||
webSocketLimiter(options); | ||
expect(options).toStrictEqual({}); | ||
}); | ||
|
||
it("should call `init` even if no requests have come in", async () => { | ||
const store = new MockStore(); | ||
webSocketLimiter({ keyGenerator, store }); | ||
|
||
expect(store.initWasCalled).toEqual(true); | ||
}); | ||
}); |