Promise.uts 12.1 KB
Newer Older
1 2 3 4 5 6
import { describe, test, expect, Result } from './tests.uts'

export function testPromise(): Result {
    return describe("Promise", () => {
        test('constructor', () => {
          // 常规方式
lizhongyi_'s avatar
lizhongyi_ 已提交
7
           let p = new Promise<number>((resolve, reject) => {
8 9 10 11 12 13 14 15 16 17
             let success = true;
              setTimeout(()=>{
                if (success) {
                  resolve(2024)
                } else{
                  let error = new Error("something going wrong")
                  reject(error)
                }
              }, 10);
           });
lizhongyi_'s avatar
lizhongyi_ 已提交
18
           p.then((res: number) => {  
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
              expect(res).toEqual(2024) 
           })
           
           // 单个函数
           let p1 = new Promise<any | null>((resolve: (value: any | null) => void) => {
             setTimeout(function() {
               resolve(null)
             }, 10);
           })
           p1.then((res: any | null) => {
              expect(res).toEqual(null) 
           })
           
           // 指定函数类型
           let p2 = new Promise((resolve: (value: string) => void, reject: (error: any | null) => void) => {
              setTimeout(function() {
                reject(null)
              }, 10);
           })
           p2.then()
           .catch((error: any | null) => {
             expect(error).toEqual(null) 
           })
        })
        
        test('then', () => {
            let p = new Promise<string>((reslove, reject) => {
            	setTimeout(function() {
                reslove("hello world")  
            	}, 13);
            })
              
            p.then(
            	(res: string): Promise<string> => {
                expect(res).toEqual("hello world") 
            		let p0 = new Promise<string>((reslove,reject) => {
            			var success = true;  
            			setTimeout(function() {
            				success = true;
            				if (success) {
            					reslove(res + " 2024")  
            				} else{
            					reject("first then p0 reject message")
            				}
            			}, 8);
            		})
            		return p0;
            	},
            	(err: any | null): void => {  
            		throw new Error("first then throw error")
            	}
            )
            .catch(
            	(err):string => {
            		return "first catch return message"
            	}
            )   
            .then(
            	(res) => {
                console.log(res)
                expect(res).toEqual("hello world 2024") 
            	}
            )
            .catch(
            	(err) => {
            		console.log(err, "this is seconded catch")
            	}
            )  
            .finally(() => {
              console.log("finally")
              expect("finally").toEqual("finally")
            })
        })
        
        test('catch', () => {
lizhongyi_'s avatar
lizhongyi_ 已提交
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
            let p = Promise.reject()
            p.then()
            .catch((res: any | null) => {
              expect(res).toEqual(null)
            })
            
            // catch 指定返回值
            new Promise<string>((resolve, reject) => {
              setTimeout(() => {
                reject("2024")
              }, 12)
            })
            .then()
            .catch((res: any | null): string => {
              expect(res!).toEqual("2024")
              return "hello world"
            })
            .then((res) => {
              expect(res).toEqual("hello world")
            })
            
            // catch 返回Promise
             new Promise<string>((resolve, reject) => {
              setTimeout(() => {
                reject("2024")
              }, 12)
            })
            .then()
            .catch((res: any | null): Promise<number> => {
              expect(res!).toEqual("2024")
              let p = new Promise<number>((resolve, reject) => {
                resolve(1)
              })
              return p
            })
            .then((res) => {
              expect(res).toEqual(1)
            })
132 133 134
        })
        
        test('finally', () => {
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
            Promise.resolve(2)
            .finally(() => {
              console.log("finally")
            })
            .then((res) => {
              expect(res).toEqual(2)
            })
            
            Promise.reject(3)
            .finally(() => {
              console.log("finally")
            })
            .catch((res) => {
              expect(res).toEqual(3)
            })
lizhongyi_'s avatar
lizhongyi_ 已提交
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
            
            Promise.resolve(2)
            .finally((): number => {
              console.log("finally")
              return 88
            })
            .then((res) => {
            	expect(res).toEqual(2)
            })
            
            Promise.reject(3).finally(() => {
            	throw new Error("99")
            })
            .catch((res) => {
              expect((res as Error).message).toEqual("99");
            })
            
            console.log("start");
            Promise.reject(4).finally((): Promise<number> => {
            	return new Promise((resolve, reject) => {
            		setTimeout(() => {
            			resolve(20)
            		}, 1000)
            	})
            })
            .catch((res) => {
            	expect(res).toEqual(4)
            })
178 179 180
        })
        
        test('resolve', () => {
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
          // resolve array
          Promise.resolve([1,2,3])
          .then((res) => {
            expect(res).toEqual([1,2,3])
          })
          
          // resolve string
          Promise.resolve("成功")
          .then((res) => {
            expect(res).toEqual("成功");
          })
          
          // resolve null
          Promise.resolve()
          .then((res) => {
            // expect(res).toEqual(null);
          })
          
          // resolve promise
          const original = Promise.resolve(33)
          const cast = Promise.resolve(original)
          cast.then((res) => {
            expect(res).toEqual(33);
          })
          // 注:此处飘红是编译器误报,实际可以判等
          expect(original === cast).toEqual(true)
207 208 209
        })
        
        test('reject', () => {
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
          // reject array
          Promise.reject([1,2,3])
          .catch((err) => {
            expect(err).toEqual([1,2,3])
          })
          
          // reject error
          Promise.reject(new Error("this is reject error message"))
          .catch((err) => {
            expect((err as Error).message).toEqual("this is reject error message");
          })
          
          // reject null
          Promise.reject()
          .catch((err) => {
            expect(err).toEqual(null);
          })
          
          // reject promise
          const original = Promise.reject(33)
          const cast = Promise.reject(original)
          cast.catch((err) => {
            expect((err as Promise<any>) === original).toEqual(true);
          })
          expect(original === cast).toEqual(false)
          
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
        })
        
        test('all', () => {
           let p0 = new Promise<string>((resolve, reject) => {
             setTimeout(function() {
               resolve("1");
             }, 10);
           })
           
           let p1 = new Promise<string>((resolve, reject) => {
             setTimeout(function() {
               resolve("2");
             }, 10);
           })
           
           let p2 = new Promise<string>((resolve, reject) => {
             setTimeout(function() {
               resolve("3");
             }, 10);
           })
           
           let p3 = new Promise<string>((resolve, reject) => {
             setTimeout(function() {
               let error = new Error("p3 reject reason")
               reject(error);
             }, 10);
           })
           // resolve
           Promise.all([p0, p1, p2])
           .then((res) => {
             console.log(res);
             expect(res).toEqual(["1", "2", "3"]) 
           })
           
           // reject
           Promise.all([p1, p2, p3])
           .then()
           .catch((error) => {
             console.log(error);
             expect((error as Error).message).toEqual("p3 reject reason");
           })
        })
        
        test('allSettled', () => {
            let p0 = new Promise<string | null>((resolve, reject) => {
              setTimeout(function() {
                resolve("1");
              }, 10);
            })
            
            let p1 = new Promise<string | null>((resolve, reject) => {
              setTimeout(function() {
                resolve(null);
              }, 10);
            })
            
            let p2 = new Promise<string | null>((resolve, reject) => {
              setTimeout(function() {
                reject(null)
              }, 10);
            })
            
            let p3 = new Promise<string  | null>((resolve, reject) => {
              setTimeout(function() {
                let error = new Error("p3 reject reason")
                reject(error);
              }, 10);
            })
            
            Promise.allSettled([p0, p1, p2, p3])
            .then((res) => {
              console.log(res);
              let statusArr : string[] = []
lizhongyi_'s avatar
lizhongyi_ 已提交
309
              res.forEach((item, index: number) => {
310
                statusArr.add(item.status)
311 312 313 314 315 316
              })
              expect(statusArr).toEqual(["fulfilled", "fulfilled", "rejected", "rejected"]) 
            })
        })
        
        test('any', () => {
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
          let p0 = new Promise<string | null>((resolve, reject) => {
            setTimeout(function() {
              resolve("1");
            }, 10);
          })
          
          let p1 = new Promise<string | null>((resolve, reject) => {
            setTimeout(function() {
              resolve(null);
            }, 15);
          })
          
          let p2 = new Promise<string | null>((resolve, reject) => {
            setTimeout(function() {
              reject(null)
            }, 10);
          })
          
          let p3 = new Promise<string  | null>((resolve, reject) => {
            setTimeout(function() {
             reject("error");
            }, 12);
          })
          
          // test for resolve
          Promise.any([p0, p1, p2])
          .then((res) => {
            console.log(res)
            expect(res).toEqual("1");
          })
          
          // test for reject
          Promise.any([p2, p3])
          .then()
          .catch(
            (error: any | null) => {
              console.log("test for promise.any error ==> ", error);
354
              // #ifdef APP-IOS
355 356 357
              expect((error as UTSPromiseAggregateError).name).toEqual("AggregateError");
              expect((error as UTSPromiseAggregateError).message).toEqual("All promises were rejected");
              expect((error as UTSPromiseAggregateError).errors).toEqual([null, "error"]);
358 359 360 361 362 363 364
              // #endif
              
              // #ifdef APP-ANDROID
              expect((error as UTSPromiseAggregateError).name).toEqual("AggregateError");
              expect((error as UTSPromiseAggregateError).message).toEqual("All promises were rejected");
              expect((error as UTSPromiseAggregateError).errors).toEqual([null, "error"]);
              // #endif
365
          })   
366 367 368
        })
        
        test('race', () => {
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
          let p0 = new Promise<string | null>((resolve, reject) => {
            setTimeout(function() {
              resolve("fast");
            }, 10);
          })
          
          let p1 = new Promise<string | null>((resolve, reject) => {
            setTimeout(function() {
              resolve("slow");
            }, 15);
          })
          
          let p2 = new Promise<string | null>((resolve, reject) => {
            setTimeout(function() {
              reject(null)
            }, 12);
          })
          
          // test for resolve
          Promise.race([p0, p1])
          .then((res) => {
            console.log("test for race resolve ===> ", res);
            expect(res).toEqual("fast");
          })
          
          // test for reject 
          Promise.race([p1, p2])
          .then()
          .catch((error) => {
            console.log("test for race reject ===> ", error);
            expect(error).toEqual(null);
          })
401 402 403
        })
    })
}