-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Roy Razon
committed
Sep 27, 2023
1 parent
c0e3517
commit 89bd119
Showing
3 changed files
with
79 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { afterAll, beforeAll, beforeEach, describe, it, expect, jest } from '@jest/globals' | ||
import { memoizeForDuration } from './memoize' | ||
|
||
describe('memoizeForDuration', () => { | ||
beforeAll(() => { | ||
jest.useFakeTimers() | ||
}) | ||
afterAll(() => { | ||
jest.useRealTimers() | ||
}) | ||
|
||
let fn: jest.Mock<() => number> | ||
let memoized: () => number | ||
|
||
beforeEach(() => { | ||
fn = jest.fn(() => 12) | ||
memoized = memoizeForDuration(fn, 1000) | ||
}) | ||
|
||
describe('before the first call', () => { | ||
it('does not call the specified function', () => { | ||
expect(fn).not.toHaveBeenCalled() | ||
}) | ||
}) | ||
|
||
describe('on the first call', () => { | ||
let v: number | ||
beforeEach(() => { | ||
v = memoized() | ||
}) | ||
it('calls the specified function', () => { | ||
expect(fn).toHaveBeenCalledTimes(1) | ||
}) | ||
it('returns the memoized value', () => { | ||
expect(v).toBe(12) | ||
}) | ||
|
||
describe('on the second call, when the expiry duration has not passed', () => { | ||
beforeEach(() => { | ||
jest.advanceTimersByTime(999) | ||
v = memoized() | ||
}) | ||
it('does not call the specified function again', () => { | ||
expect(fn).toHaveBeenCalledTimes(1) | ||
}) | ||
it('returns the memoized value', () => { | ||
expect(v).toBe(12) | ||
}) | ||
}) | ||
|
||
describe('on the second call, when the expiry duration has passed', () => { | ||
beforeEach(() => { | ||
jest.advanceTimersByTime(1000) | ||
v = memoized() | ||
}) | ||
it('calls the specified function again', () => { | ||
expect(fn).toHaveBeenCalledTimes(2) | ||
}) | ||
it('returns the memoized value', () => { | ||
expect(v).toBe(12) | ||
}) | ||
}) | ||
}) | ||
}) |
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,9 @@ | ||
export const memoizeForDuration = <T>(f: () => T, milliseconds: number) => { | ||
let cache: { value: T; expiry: number } | undefined | ||
return () => { | ||
if (!cache || cache.expiry <= Date.now()) { | ||
cache = { value: f(), expiry: Date.now() + milliseconds } | ||
} | ||
return cache.value | ||
} | ||
} |
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