-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✅ rewrite tests to be a bit prettier
- Loading branch information
1 parent
d312809
commit e5c4773
Showing
2 changed files
with
63 additions
and
53 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 |
---|---|---|
@@ -1,8 +1,10 @@ | ||
module.exports = { | ||
'roots': [ | ||
roots: [ | ||
'<rootDir>/src' | ||
], | ||
'transform': { | ||
transform: { | ||
'^.+\\.tsx?$': 'ts-jest' | ||
} | ||
}, | ||
verbose: true, | ||
testEnvironment: 'node' | ||
} |
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,66 +1,74 @@ | ||
import { WaitGroup } from './' | ||
|
||
test('exports class constructor', () => { | ||
expect(typeof WaitGroup).toBe('function') | ||
}) | ||
describe('WaitGroup', () => { | ||
test('exports class constructor', () => { | ||
expect(typeof WaitGroup).toBe('function') | ||
}) | ||
|
||
test('creates a class instance when run', () => { | ||
const wg = new WaitGroup() | ||
expect(wg).toBeInstanceOf(WaitGroup) | ||
}) | ||
test('creates a class instance when run', () => { | ||
const wg = new WaitGroup() | ||
expect(wg).toBeInstanceOf(WaitGroup) | ||
}) | ||
|
||
test('wait() instantly returns if no actions taken yet', () => { | ||
const wg = new WaitGroup() | ||
const wait = wg.wait() | ||
describe('wait()', () => { | ||
test('instantly returns if no actions taken yet', () => { | ||
const wg = new WaitGroup() | ||
const wait = wg.wait() | ||
|
||
return expect(wait).resolves.toEqual(undefined) | ||
}) | ||
return expect(wait).resolves.toEqual(undefined) | ||
}) | ||
|
||
test('add() increments internal counter by default of 1', () => { | ||
const wg = new WaitGroup() | ||
wg.add() | ||
expect(wg._current).toEqual(1) | ||
}) | ||
test('waits for internal counter to reach 0', async () => { | ||
const wg = new WaitGroup() | ||
let isResolved = false | ||
wg.add(2) | ||
|
||
test('add() increments internal counter given value', () => { | ||
const wg = new WaitGroup() | ||
wg.add(3) | ||
expect(wg._current).toEqual(3) | ||
}) | ||
const waiting = wg.wait().then(() => { | ||
isResolved = true | ||
}) | ||
|
||
test('done() decrements internal counter by 1', () => { | ||
const wg = new WaitGroup() | ||
wg.add(3) | ||
expect(wg._current).toEqual(3) | ||
wg.done() | ||
expect(wg._current).toEqual(2) | ||
}) | ||
expect(isResolved).toEqual(false) | ||
|
||
test('throws if add() results in a negative counter', () => { | ||
const wg = new WaitGroup() | ||
expect(wg.add.bind(wg, -1)).toThrow('Negative WaitGroup counter') | ||
}) | ||
wg.done() | ||
expect(isResolved).toEqual(false) | ||
|
||
test('throws if done() results in a negative counter', () => { | ||
const wg = new WaitGroup() | ||
expect(wg.done.bind(wg)).toThrow('Negative WaitGroup counter') | ||
}) | ||
wg.done() | ||
await expect(waiting).resolves.toEqual(undefined) | ||
expect(isResolved).toEqual(true) | ||
}) | ||
}) | ||
|
||
test('wait() waits for internal counter to reach 0', async () => { | ||
const wg = new WaitGroup() | ||
let isResolved = false | ||
wg.add(2) | ||
describe('add()', () => { | ||
test('increments internal counter by default of 1', () => { | ||
const wg = new WaitGroup() | ||
wg.add() | ||
expect(wg['_current']).toEqual(1) | ||
}) | ||
|
||
const waiting = wg.wait().then(() => { | ||
isResolved = true | ||
}) | ||
test('increments internal counter given value', () => { | ||
const wg = new WaitGroup() | ||
wg.add(3) | ||
expect(wg['_current']).toEqual(3) | ||
}) | ||
|
||
expect(isResolved).toEqual(false) | ||
test('throws if add() results in a negative counter', () => { | ||
const wg = new WaitGroup() | ||
expect(wg.add.bind(wg, -1)).toThrow('Negative WaitGroup counter') | ||
}) | ||
}) | ||
|
||
wg.done() | ||
expect(isResolved).toEqual(false) | ||
describe('done()', () => { | ||
test('decrements internal counter by 1', () => { | ||
const wg = new WaitGroup() | ||
wg.add(3) | ||
expect(wg['_current']).toEqual(3) | ||
wg.done() | ||
expect(wg['_current']).toEqual(2) | ||
}) | ||
|
||
wg.done() | ||
await expect(waiting).resolves.toEqual(undefined) | ||
expect(isResolved).toEqual(true) | ||
test('throws if done() results in a negative counter', () => { | ||
const wg = new WaitGroup() | ||
expect(wg.done.bind(wg)).toThrow('Negative WaitGroup counter') | ||
}) | ||
}) | ||
}) |