test-utils.spec.ts 5.79 KB
Newer Older
1
import { assert } from 'chai'
2 3

/* Imports: Internal */
4
import { expect } from './setup'
5
import { expectApprox, awaitCondition } from '../src'
6

7 8 9 10 11 12 13 14
describe('awaitCondition', () => {
  it('should try the condition fn until it returns true', async () => {
    let i = 0
    const condFn = async () => {
      i++
      return Promise.resolve(i === 2)
    }

15
    await awaitCondition(condFn, 50, 3)
16 17 18 19 20 21 22 23 24 25 26
    expect(i).to.equal(2)
  })

  it('should only try the configured number of attempts', async () => {
    let i = 0
    const condFn = async () => {
      i++
      return Promise.resolve(i === 2)
    }

    try {
27
      await awaitCondition(condFn, 50, 1)
28
    } catch (e) {
29
      return
30 31
    }

32
    assert.fail('Condition never failed, but it should have.')
33 34 35
  })
})

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
describe('expectApprox', () => {
  it('should pass when the actual number is higher, but within the expected range of the target', async () => {
    expectApprox(119, 100, {
      percentUpperDeviation: 20,
      percentLowerDeviation: 20,
      absoluteUpperDeviation: 20,
      absoluteLowerDeviation: 20,
    })
  })
  it('should pass when the actual number is lower, but within the expected range of the target', async () => {
    expectApprox(81, 100, {
      percentUpperDeviation: 20,
      percentLowerDeviation: 20,
      absoluteUpperDeviation: 20,
      absoluteLowerDeviation: 20,
    })
  })
  it('should throw an error when no deviation values are given', async () => {
54
    try {
55 56
      expectApprox(101, 100, {})
      assert.fail('expectApprox did not throw an error')
57 58
    } catch (error) {
      expect(error.message).to.equal(
59
        'Must define at least one parameter to limit the deviation of the actual value.'
60 61 62 63
      )
    }
  })

64 65 66 67 68 69 70 71 72 73 74 75 76
  describe('should throw an error if the actual value is higher than expected', () => {
    describe('... when only one upper bound value is defined', () => {
      it('... and percentUpperDeviation sets the upper bound', async () => {
        try {
          expectApprox(121, 100, {
            percentUpperDeviation: 20,
          })
          assert.fail('expectApprox did not throw an error')
        } catch (error) {
          expect(error.message).to.equal(
            'Actual value (121) is greater than the calculated upper bound of (120): expected false to be true'
          )
        }
77
      })
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
      it('... and absoluteUpperDeviation sets the upper bound', async () => {
        try {
          expectApprox(121, 100, {
            absoluteUpperDeviation: 20,
          })
          assert.fail('expectApprox did not throw an error')
        } catch (error) {
          expect(error.message).to.equal(
            'Actual value (121) is greater than the calculated upper bound of (120): expected false to be true'
          )
        }
      })
    })
    describe('... when both values are defined', () => {
      it('... and percentUpperDeviation sets the upper bound', async () => {
        try {
          expectApprox(121, 100, {
            percentUpperDeviation: 20,
            absoluteUpperDeviation: 30,
          })
          assert.fail('expectApprox did not throw an error')
        } catch (error) {
          expect(error.message).to.equal(
            'Actual value (121) is greater than the calculated upper bound of (120): expected false to be true'
          )
        }
      })
      it('... and absoluteUpperDeviation sets the upper bound', async () => {
        try {
          expectApprox(121, 100, {
            percentUpperDeviation: 30,
            absoluteUpperDeviation: 20,
          })
          assert.fail('expectApprox did not throw an error')
        } catch (error) {
          expect(error.message).to.equal(
            'Actual value (121) is greater than the calculated upper bound of (120): expected false to be true'
          )
        }
      })
    })
  })

  describe('should throw an error if the actual value is lower than expected', () => {
    describe('... when only one lower bound value is defined', () => {
      it('... and percentLowerDeviation sets the lower bound', async () => {
        try {
          expectApprox(79, 100, {
            percentLowerDeviation: 20,
          })
          assert.fail('expectApprox did not throw an error')
        } catch (error) {
          expect(error.message).to.equal(
            'Actual value (79) is less than the calculated lower bound of (80): expected false to be true'
          )
        }
      })

      it('... and absoluteLowerDeviation sets the lower bound', async () => {
        try {
          expectApprox(79, 100, {
            absoluteLowerDeviation: 20,
          })
          assert.fail('expectApprox did not throw an error')
        } catch (error) {
          expect(error.message).to.equal(
            'Actual value (79) is less than the calculated lower bound of (80): expected false to be true'
          )
        }
      })
    })

    describe('... when both values are defined', () => {
      it('... and percentLowerDeviation sets the lower bound', async () => {
        try {
          expectApprox(79, 100, {
            percentLowerDeviation: 20,
            absoluteLowerDeviation: 30,
          })
          assert.fail('expectApprox did not throw an error')
        } catch (error) {
          expect(error.message).to.equal(
            'Actual value (79) is less than the calculated lower bound of (80): expected false to be true'
          )
        }
      })

      it('... and absoluteLowerDeviation sets the lower bound', async () => {
        try {
          expectApprox(79, 100, {
            percentLowerDeviation: 30,
            absoluteLowerDeviation: 20,
          })
          assert.fail('expectApprox did not throw an error')
        } catch (error) {
          expect(error.message).to.equal(
            'Actual value (79) is less than the calculated lower bound of (80): expected false to be true'
          )
        }
      })
    })
179 180
  })
})