test-utils.spec.ts 5.18 KB
Newer Older
1 2 3 4
import { expect } from '../setup'

/* Imports: Internal */
import { expectApprox } from '../../src'
5
import { assert } from 'chai'
6

7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
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 () => {
25
    try {
26 27
      expectApprox(101, 100, {})
      assert.fail('expectApprox did not throw an error')
28 29
    } catch (error) {
      expect(error.message).to.equal(
30
        'Must define at least one parameter to limit the deviation of the actual value.'
31 32 33 34
      )
    }
  })

35 36 37 38 39 40 41 42 43 44 45 46 47
  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'
          )
        }
48
      })
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 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
      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'
          )
        }
      })
    })
150 151
  })
})