1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- // Copyright (c) Jupyter Development Team.
- // Distributed under the terms of the Modified BSD License.
- import 'jest';
- import { PromiseDelegate } from '@lumino/coreutils';
- import { Signal } from '@lumino/signaling';
- import {
- expectFailure,
- isFulfilled,
- testEmission
- } from '@jupyterlab/testutils';
- describe('test/utils', () => {
- describe('testEmission', () => {
- it('should resolve to the given value', async () => {
- const owner = {};
- const x = new Signal<{}, number>(owner);
- const emission = testEmission(x, {
- value: 'done'
- });
- x.emit(0);
- expect(await emission).toBe('done');
- });
- it('should find the given emission', async () => {
- const owner = {};
- const x = new Signal<{}, number>(owner);
- const emission = testEmission(x, {
- find: (a, b) => b === 1,
- value: 'done'
- });
- x.emit(0);
- expect(await isFulfilled(emission)).toBe(false);
- x.emit(1);
- expect(await emission).toBe('done');
- });
- it('should reject if the test throws an error', async () => {
- const owner = {};
- const x = new Signal<{}, number>(owner);
- const emission = testEmission(x, {
- find: (a, b) => b === 1,
- test: (a, b) => {
- throw new Error('my error');
- },
- value: 'done'
- });
- x.emit(0);
- expect(await isFulfilled(emission)).toBe(false);
- x.emit(1);
- await expectFailure(emission, 'my error');
- });
- it('should resolve if the test succeeds', async () => {
- const owner = {};
- const x = new Signal<{}, number>(owner);
- const emission = testEmission(x, {
- find: (a, b) => b === 1,
- test: (a, b) => {
- expect(b).toBe(1);
- },
- value: 'done'
- });
- x.emit(0);
- expect(await isFulfilled(emission)).toBe(false);
- x.emit(1);
- expect(await emission).toBe('done');
- });
- });
- describe('isFulfilled', () => {
- it('should resolve to true only after a promise is fulfilled', async () => {
- const p = new PromiseDelegate<number>();
- expect(await isFulfilled(p.promise)).toBe(false);
- p.resolve(10);
- expect(await isFulfilled(p.promise)).toBe(true);
- });
- it('should resolve to true even if the promise is rejected', async () => {
- const p = new PromiseDelegate<number>();
- expect(await isFulfilled(p.promise)).toBe(false);
- p.reject(new Error('my error'));
- expect(await isFulfilled(p.promise)).toBe(true);
- });
- });
- });
|