Skip to content

Commit

Permalink
test(core): basic tests for websockets
Browse files Browse the repository at this point in the history
  • Loading branch information
MathurAditya724 committed Jun 24, 2024
1 parent 75ab5c6 commit f687a73
Showing 1 changed file with 67 additions and 1 deletion.
68 changes: 67 additions & 1 deletion packages/core/src/__tests__/websockets.spec.ts
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);
});
});

0 comments on commit f687a73

Please sign in to comment.