All files / test completable-promise.spec.ts

100% Statements 148/148
100% Branches 0/0
100% Functions 40/40
100% Lines 144/144

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 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 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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 2521x 1x 1x 1x   1x   1x   1x 1x     25x   5x   1x 1x 1x   1x 1x 1x 1x 1x       1x 1x 1x 1x   1x 1x 1x     1x 1x 1x 1x     1x 1x 1x   1x 1x 1x     1x 1x 1x 1x   1x 1x 1x 1x     1x 1x 1x   1x 1x 1x 1x   1x 1x 1x   1x 1x             1x 1x 1x 1x   1x 1x 1x     1x 1x 1x 1x     1x 1x 1x   1x     1x 1x     1x 1x 1x 1x     1x 1x 1x   1x 1x 1x 1x   1x 1x 1x   1x     1x         1x 1x 1x 1x   1x 1x 1x         1x 1x 1x 1x     1x 1x 1x   1x     1x 1x 1x     1x 1x 1x 1x       1x 1x 1x   1x 1x   1x 1x         1x 1x         1x     1x 1x   1x     1x 1x             1x       1x 1x 1x   1x   1x     1x 1x   1x   1x       1x 1x 1x 1x   1x 1x   1x 1x 1x            
import 'mocha';
import { expect } from 'chai';
import { CompletablePromise } from '../src/index';
import { State } from '../src/index';
 
describe('CompletablePromise', () => {
 
  const errorMessage = 'something went wrong';
 
  const throwUnreachableCodeException = (/* istanbul ignore next */ value = '') => {
    throw new Error(`code should not be reached ${value}`);
  }
 
  const expectTrue = (result: boolean) => expect(result).to.equal(true);
 
  const expectFalse = (result: boolean) => expect(result).to.equal(false);
 
  describe('#constructor', () => {
    it('should should correctly initialize the proimise state', () => {
      const promise = new CompletablePromise();
 
      expect(promise.getState()).to.equal(State.Pending);
      expectTrue(promise.isPending());
      expectFalse(promise.isFulfilled());
      expectFalse(promise.isRejected());
      expectFalse(promise.isSettled());
    });
  });
 
  describe('#resolve', () => {
    it('should return the value to `then` handler', done => {
      const number = 5;
      const promise = new CompletablePromise();
 
      promise.then(value => {
        expect(value).to.equal(number);
        done();
      });
 
      expectTrue(promise.isPending());
      promise.resolve(number);
      expectTrue(promise.isFulfilled());
      expectTrue(promise.isSettled());
    });
 
    it('should respect asynchronous behaviour', done => {
      const array = ['foo'];
      const promise = new CompletablePromise<string[]>();
 
      promise.then((value: string[]) => {
        expect(value).to.eql(['foo', 'bar', 'baz']);
        done();
      });
 
      expectTrue(promise.isPending());
      promise.resolve(array);
      expectTrue(promise.isFulfilled());
      expectTrue(promise.isSettled());
 
      array.push('bar');
      expect(array).to.eql(['foo', 'bar']);
      array.push('baz');
      expect(array).to.eql(['foo', 'bar', 'baz']);
    });
 
    it('should ignore future #resolve and #reject calls', done => {
      const number = 5;
      const promise = new CompletablePromise();
 
      expectTrue(promise.isPending());
      promise.resolve(number);
      promise.resolve(10);
      promise.reject('something went wrong');
 
      expectTrue(promise.isFulfilled());
      expectFalse(promise.isRejected());
      expectTrue(promise.isSettled());
 
      promise.then(value => {
        expect(value).to.equal(number);
      }).catch(/* istanbul ignore next */() => {
        throwUnreachableCodeException();
      }).finally(done);
    });
  });
 
  describe('#reject', () => {
    it('should return the error to `catch` handler', done => {
      const errorMessage = 'something went wrong';
      const promise = new CompletablePromise();
 
      promise.catch(reason => {
        expect(reason).to.equal(errorMessage);
        done();
      });
 
      expectTrue(promise.isPending());
      promise.reject(errorMessage);
      expectTrue(promise.isRejected());
      expectTrue(promise.isSettled());
    });
 
    it('should return the value to `catch` handler of the chained promise', done => {
      const errorMessage = 'something went wrong';
      const promise = new CompletablePromise();
 
      promise.then(/* istanbul ignore next */() => {
        throwUnreachableCodeException();
      }).catch(reason => {
        expect(reason).to.equal(errorMessage);
        done();
      });
 
      expectTrue(promise.isPending());
      promise.reject(errorMessage);
      expectTrue(promise.isRejected());
      expectTrue(promise.isSettled());
    });
 
    it('should ignore future #resolve and #reject calls', done => {
      const errorMessage = 'something went wrong';
      const promise = new CompletablePromise();
 
      expectTrue(promise.isPending());
      promise.reject('something went wrong');
      promise.resolve(errorMessage);
      promise.reject('another error');
 
      expectTrue(promise.isRejected());
      expectFalse(promise.isFulfilled());
      expectTrue(promise.isSettled());
 
      promise.then(/* istanbul ignore next */() => {
        throwUnreachableCodeException();
      }).catch(reason => {
        expect(reason).to.equal(errorMessage);
      }).finally(done);
    });
  });
 
  describe('#tryResolve', () => {
    it('should return the value to `then` handler when getValue succeeds', done => {
      const jsonString = '{"foo":"bar"}';
      const promise = new CompletablePromise();
 
      promise.then(value => {
        expect(value).to.eql({ 'foo': 'bar' });
        done();
      }).catch(/* istanbul ignore next */() => {
        throwUnreachableCodeException();
      });
 
      expectTrue(promise.isPending());
      promise.tryResolve(() => JSON.parse(jsonString));
      expectTrue(promise.isFulfilled());
      expectTrue(promise.isSettled());
    });
 
    it('should return the failure reason to `catch` handler when getValue fails', done => {
      const brokenJsonString = '{"foo":"bar"';
      const promise = new CompletablePromise();
 
      promise.then(/* istanbul ignore next */() => {
        throwUnreachableCodeException();
      }).catch(reason => {
        expect(reason.name).to.equal('SyntaxError');
        expect(reason.message).to.equal('Unexpected end of JSON input');
        done();
      });
 
      expectTrue(promise.isPending());
      promise.tryResolve(() => JSON.parse(brokenJsonString));
      expectTrue(promise.isRejected());
      expectTrue(promise.isSettled());
    });
  });
 
  describe('#then', () => {
    it('should ignore onrejected callback when using #resolve', done => {
      const promise = new CompletablePromise();
 
      promise.then(value => {
        expect(value).to.equal('foo');
        // eslint-disable-next-line @typescript-eslint/no-unused-vars
        return new Promise((resolve, reject) => {
          resolve('bar');
        });
      },/* istanbul ignore next */() => {
        throwUnreachableCodeException('in first onrejected');
      }).then(value => {
        expect(value).to.equal('bar');
        done();
      },/* istanbul ignore next */() => {
        throwUnreachableCodeException('in second onrejected');
      });
 
      promise.resolve('foo');
    });
 
    it('should ignore onfulfilled callback when using #reject', done => {
      const promise = new CompletablePromise();
 
      promise.then(/* istanbul ignore next */() => {
        throwUnreachableCodeException('in first onfulfilled');
      }, reason => {
        expect(reason).to.equal(errorMessage);
        done();
      }).then(/* istanbul ignore next */() => {
        throwUnreachableCodeException('in second onfulfilled');
      },/* istanbul ignore next */() => {
        throwUnreachableCodeException('in second onrejected');
      });
 
      promise.reject(errorMessage);
    });
  });
 
  describe('#finally', () => {
    it('should work with when the promise is resolved', done => {
      const promise = new CompletablePromise();
 
      promise.finally(done);
 
      promise.resolve('success');
    });
 
    it('should work with when the promise is rejected', done => {
      const promise = new CompletablePromise();
 
      promise.finally(done);
 
      promise.reject(errorMessage);
    });
  });
 
  describe('#get', () => {
    it('should return the inner promise, allowing using Promise static methods', done => {
      const fooPromise = new CompletablePromise();
      const barPromise = new CompletablePromise();
 
      fooPromise.resolve('foo');
      barPromise.resolve('bar');
 
      Promise.all([fooPromise.get(), barPromise.get()]).then(values => {
        expect(values).to.eql(['foo', 'bar']);
        done();
      });
    });
  });
 
});