-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from ugurcanatas/feat/add-jest-conf
feat: add jest and test file
- Loading branch information
Showing
7 changed files
with
2,345 additions
and
92 deletions.
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
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,4 +1,6 @@ | ||
node_modules | ||
tsconfig.json | ||
src | ||
.gitignore | ||
.gitignore | ||
.github | ||
__tests__ |
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { TinyProxy } from "../src/index"; | ||
|
||
describe("Timeout Proxy", () => { | ||
beforeEach(() => { | ||
jest.useFakeTimers({ | ||
advanceTimers: true, | ||
}); | ||
}); | ||
afterEach(() => { | ||
jest.useRealTimers(); | ||
}); | ||
test("should confirm if setTimeout is being called", () => { | ||
const p = new TinyProxy({ | ||
shouldLog: true, | ||
}); | ||
p.setupTimerProxy(); | ||
const spySetTimeout = jest.spyOn(global, "setTimeout"); | ||
|
||
const timerId = setTimeout(() => {}, 100); | ||
|
||
// Check if setTimeout was called and got the correct ID | ||
expect(spySetTimeout).toHaveBeenCalledTimes(1); | ||
expect(spySetTimeout).toHaveBeenCalledWith(expect.any(Function), 100); | ||
|
||
// Advance timers | ||
jest.advanceTimersByTime(100); | ||
|
||
// Verify the timer is in the activeTimeouts | ||
expect(p.activeTimeouts.has(timerId)).toBe(true); | ||
expect(p.activeTimeouts.size).toBe(1); | ||
}); | ||
|
||
test("should confirm if clearTimeout is being called", () => { | ||
const p = new TinyProxy({ | ||
shouldLog: true, | ||
}); | ||
p.setupTimerProxy(); | ||
const spyClearTimeout = jest.spyOn(global, "clearTimeout"); | ||
|
||
const timerId = setTimeout(() => {}, 100); | ||
|
||
expect(spyClearTimeout).toHaveBeenCalledTimes(0); | ||
|
||
// Advance timers | ||
jest.advanceTimersByTime(100); | ||
|
||
// Verify the timer is in the activeTimeouts | ||
expect(p.activeTimeouts.has(timerId)).toBe(true); | ||
expect(p.activeTimeouts.size).toBe(1); | ||
|
||
clearTimeout(timerId); | ||
|
||
expect(p.activeTimeouts.has(timerId)).toBe(false); | ||
expect(p.activeTimeouts.size).toBe(0); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** @type {import('ts-jest').JestConfigWithTsJest} **/ | ||
module.exports = { | ||
testEnvironment: "node", | ||
transform: { | ||
"^.+.tsx?$": ["ts-jest",{}], | ||
}, | ||
}; |
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
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
Oops, something went wrong.