forked from CityOfZion/neon-wallet
-
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.
[CityOfZion#1318] PrepWork 1/2: Added util/poll.js unit tests
- Loading branch information
Showing
2 changed files
with
42 additions
and
2 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,33 @@ | ||
import poll, { TimeoutError } from '../../app/util/poll' | ||
|
||
describe('poll functionality', () => { | ||
test('should stop when condition fulflled', async () => { | ||
// 10 rounds of 10ms | ||
const pollCfg = { attempts: 10, frequency: 10 } | ||
|
||
// pulfill the poll condition after 5th attempt | ||
let num = 5 | ||
const condition = jest.fn(() => { | ||
return --num === 0 ? Promise.resolve(num) : Promise.reject() | ||
}) | ||
|
||
// run poll, expect no exit by error | ||
const result = await poll(condition, pollCfg) | ||
// expect to have run 5/10 attempts | ||
expect(condition).toHaveBeenCalledTimes(5) | ||
}) | ||
|
||
test('should stop when max attempts reached', async () => { | ||
// 10 rounds of 10ms | ||
const pollCfg = { attempts: 10, frequency: 10 } | ||
// always reject means poll is continuous | ||
const condition = jest.fn(() => Promise.reject()) | ||
|
||
try { | ||
await poll(condition, pollCfg) // run poll | ||
} catch (e) { | ||
expect(e.name).toBe(TimeoutError.name) // expect it to reach max attempts | ||
expect(condition).toHaveBeenCalledTimes(11) // expect to have run 10 (+1) attempts | ||
} | ||
}) | ||
}) |
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