Skip to content

Commit

Permalink
✅ rewrite tests to be a bit prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
jpwilliams committed Jun 16, 2020
1 parent d312809 commit e5c4773
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 53 deletions.
8 changes: 5 additions & 3 deletions jest.config.js
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'
}
108 changes: 58 additions & 50 deletions src/index.spec.ts
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')
})
})
})

0 comments on commit e5c4773

Please sign in to comment.