1.0.8 • Published 2 years ago

hoper v1.0.8

Weekly downloads
2
License
ISC
Repository
-
Last release
2 years ago
import { hope } from '../index';

describe('hope promises', () => {
  it('should call once', async () => {
    const hopes = hope();

    let callCount = 0;

    const result = await hopes.once(async () => {
      callCount++;
      return 1;
    });

    const result2 = await hopes.once(async () => {
      callCount++;
      return 2;
    });

    const result3 = await hopes.once(async () => {
      callCount++;
      return 3;
    });

    expect(result).toBe(1);
    expect(result2).toBe(1);
    expect(result3).toBe(1);
    expect(callCount).toBe(1);
  });

  it('should reject once', async () => {
    const hopes = hope();

    let callCount = 0;

    hopes.once(async () => {
      callCount++;
      return await Promise.reject('buu');
    });

    let err = '';
    try {
      await hopes.once(async () => {
        callCount++;
        return 3;
      });
    } catch (e) {
      err = e;
    }

    expect(callCount).toBe(1);
    expect(err).toBe('buu');
  });

  it('should resolve with .resolve', async () => {
    const hopes = hope();

    hopes.resolve(1);

    const result = await hopes.promise;

    expect(result).toBe(1);
  });

  it('should reject with .reject', async () => {
    const hopes = hope();

    hopes.reject(1);

    let err = '';
    try {
      await hopes.promise;
    } catch (e) {
      err = e;
    }

    expect(err).toBe(1);
  });
});
``;