forked from chrisdavies/tiny-date-picker
-
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.
- Loading branch information
Kim Almasan
committed
Jun 17, 2020
1 parent
84c3183
commit 9c6bd8c
Showing
2 changed files
with
40 additions
and
57 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,54 +1,37 @@ | ||
/* global expect */ | ||
import {bufferFn} from '../src/lib/fns'; | ||
import { bufferFn } from '../src/lib/fns' | ||
|
||
const sleep = (x: number) => new Promise(resolve => setTimeout(resolve, x)) | ||
|
||
describe('bufferFn', () => { | ||
it('only runs once within a window of time', () => { | ||
return new Promise((resolve, reject) => { | ||
let count = 0; | ||
const f = bufferFn(1, () => ++count); | ||
|
||
f(); | ||
f(); | ||
f(); | ||
|
||
expect(count).toEqual(0); | ||
|
||
setTimeout(() => { | ||
try { | ||
expect(count).toEqual(1); | ||
resolve(); | ||
} catch (err) { | ||
reject(err); | ||
} | ||
}, 5); | ||
}); | ||
}); | ||
|
||
it('only runs twice if called outside of the window', () => { | ||
return new Promise((resolve, reject) => { | ||
let count = 0; | ||
const f = bufferFn(1, () => ++count); | ||
|
||
f(); | ||
f(); | ||
f(); | ||
|
||
expect(count).toEqual(0); | ||
|
||
setTimeout(() => { | ||
f(); | ||
f(); | ||
f(); | ||
}, 10); | ||
|
||
setTimeout(() => { | ||
try { | ||
expect(count).toEqual(2); | ||
resolve(); | ||
} catch (err) { | ||
reject(err); | ||
} | ||
}, 15); | ||
}); | ||
}); | ||
}); | ||
it('only runs once within a window of time', async () => { | ||
let count = 0 | ||
const f = bufferFn(100, () => ++count) | ||
|
||
f() | ||
f() | ||
f() | ||
|
||
await sleep(200) | ||
expect(count).toBe(1) | ||
}) | ||
|
||
it('only runs twice if called outside of the window', async () => { | ||
let count = 0 | ||
const f = bufferFn(100, () => ++count) | ||
|
||
f() | ||
f() | ||
f() | ||
|
||
await sleep(200) | ||
expect(count).toBe(1) | ||
|
||
f() | ||
f() | ||
f() | ||
|
||
await sleep(200) | ||
expect(count).toBe(2) | ||
}) | ||
}) |