1+ //
2+ // Note: This example test is leveraging the Mocha test framework.
3+ // Please refer to their documentation on https://mochajs.org/ for help.
4+ //
5+
6+ // Place this right on top
7+ import { initialize } from './initialize' ;
8+ // The module 'assert' provides assertion methods from node
9+ import * as assert from 'assert' ;
10+
11+ // You can import and use all API from the 'vscode' module
12+ // as well as import your extension to test it
13+ import * as vscode from 'vscode' ;
14+ import { createDeferred , Deferred } from '../client/common/helpers' ;
15+
16+ // Defines a Mocha test suite to group tests of similar kind together
17+ suite ( 'Deferred' , ( ) => {
18+ test ( 'Resolve' , done => {
19+ const valueToSent = new Date ( ) . getTime ( ) ;
20+ const def = createDeferred < number > ( ) ;
21+ def . promise . then ( value => {
22+ assert . equal ( value , valueToSent ) ;
23+ assert . equal ( def . resolved , true , 'resolved property value is not `true`' ) ;
24+ done ( ) ;
25+ } ) . catch ( reason => {
26+ assert . fail ( reason , 'value' , 'Was expecting promise to resolve, however it got rejected' , '' ) ;
27+ assert . equal ( def . rejected , true , 'resolved property value is not `true`' ) ;
28+ done ( ) ;
29+ } ) ;
30+
31+ assert . equal ( def . resolved , false , 'Promise is resolved even when it should not be' ) ;
32+ assert . equal ( def . rejected , false , 'Promise is rejected even when it should not be' ) ;
33+ assert . equal ( def . completed , false , 'Promise is completed even when it should not be' ) ;
34+
35+ def . resolve ( valueToSent ) ;
36+
37+ assert . equal ( def . resolved , true , 'Promise is not resolved even when it should not be' ) ;
38+ assert . equal ( def . rejected , false , 'Promise is rejected even when it should not be' ) ;
39+ assert . equal ( def . completed , true , 'Promise is not completed even when it should not be' ) ;
40+ } ) ;
41+ test ( 'Reject' , done => {
42+ const errorToSend = new Error ( 'Something' ) ;
43+ const def = createDeferred < number > ( ) ;
44+ def . promise . then ( value => {
45+ assert . fail ( value , 'Error' , 'Was expecting promise to get rejected, however it was resolved' , '' ) ;
46+ done ( ) ;
47+ } ) . catch ( reason => {
48+ assert . equal ( reason , errorToSend , 'Error received is not the same' ) ;
49+ done ( ) ;
50+ } ) ;
51+
52+ assert . equal ( def . resolved , false , 'Promise is resolved even when it should not be' ) ;
53+ assert . equal ( def . rejected , false , 'Promise is rejected even when it should not be' ) ;
54+ assert . equal ( def . completed , false , 'Promise is completed even when it should not be' ) ;
55+
56+ def . reject ( errorToSend ) ;
57+
58+ assert . equal ( def . resolved , false , 'Promise is resolved even when it should not be' ) ;
59+ assert . equal ( def . rejected , true , 'Promise is not rejected even when it should not be' ) ;
60+ assert . equal ( def . completed , true , 'Promise is not completed even when it should not be' ) ;
61+ } ) ;
62+ } ) ;
0 commit comments