Promise.uts 9.8 KB
Newer Older
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
import { describe, test, expect, Result } from './tests.uts'

export function testPromise(): Result {
    return describe("Promise", () => {
        // #ifdef APP-IOS
        test('constructor', () => {
          // 常规方式
           let p = new Promise<Int>((resolve, reject) => {
             let success = true;
              setTimeout(()=>{
                if (success) {
                  resolve(2024)
                } else{
                  let error = new Error("something going wrong")
                  reject(error)
                }
              }, 10);
           });
           p.then((res: Int) => {  
              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', () => {
        
        })
        
        test('finally', () => {
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
            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)
            })
114 115 116
        })
        
        test('resolve', () => {
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
          // 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)
143 144 145
        })
        
        test('reject', () => {
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
          // 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)
          
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 252
        })
        
        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[] = []
              res.forEach((item, index) => {
                statusArr.append(item.status)
              })
              expect(statusArr).toEqual(["fulfilled", "fulfilled", "rejected", "rejected"]) 
            })
        })
        
        test('any', () => {
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
          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);
              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"]);
          })   
294 295 296
        })
        
        test('race', () => {
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
          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);
          })
329 330 331 332
        })
        // #endif
    })
}