Promise.uts 10.8 KB
Newer Older
1 2
import { describe, test, expect, Result } from './tests.uts'

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
export function testPromise() : Result {
  return describe("Promise", () => {
    test('constructor', () => {
      // #TEST Promise.Constructor,Promise.Constructor_1
      // 常规方式
      let p = new Promise<number>((resolve, reject) => {
        let success = true;
        setTimeout(() => {
          if (success) {
            resolve(2024)
          } else {
            let error = new Error("something going wrong")
            reject(error)
          }
        }, 10);
      });
      p.then((res : number) => {
        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)
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
      // #END
    })

    test('then', () => {
      // #TEST Promise.then,Promise.then_1,Promise.then_2,Promise.then_3,Promise.then_4
      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) => {
            expect(res).toEqual("hello world 2024")
          }
        )
        .catch(
          (err) => {
            console.log(err, "this is seconded catch")
          }
        )
        .finally(() => {
          expect("finally").toEqual("finally")
91
        })
92 93 94 95 96 97 98 99 100 101
      // #END
    })

    test('catch', () => {
      // #TEST Promise.catch,Promise.catch_1,Promise.catch_2,Promise.catch_3,Promise.catch_4

      let p = Promise.reject()
      p.then()
        .catch((res : any | null) => {
          expect(res).toEqual(null)
102
        })
103 104 105 106 107 108 109 110 111 112 113

      // catch 指定返回值
      new Promise<string>((resolve, reject) => {
        setTimeout(() => {
          reject("2024")
        }, 12)
      })
        .then()
        .catch((res : any | null) : string => {
          expect(res!).toEqual("2024")
          return "hello world"
114
        })
115 116
        .then((res) => {
          expect(res).toEqual("hello world")
117
        })
118 119 120 121 122 123 124 125 126 127 128 129

      // 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)
130
          })
131
          return p
132
        })
133 134
        .then((res) => {
          expect(res).toEqual(1)
135
        })
136 137 138 139 140 141 142 143 144
      // #END
    })

    test('finally', () => {
      // #TEST Promise.finally

      Promise.resolve(2)
        .finally(() => {
          console.log("finally")
145
        })
146 147
        .then((res) => {
          expect(res).toEqual(2)
148
        })
149 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 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 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 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324

      Promise.reject(3)
        .finally(() => {
          console.log("finally")
        })
        .catch((res) => {
          expect(res).toEqual(3)
        })

      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)
        })
      // #END
    })

    test('resolve', () => {
      // #TEST Promise.resolve,Promise.resolve_1,Promise.resolve_2
      // 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)
      // #END
    })

    test('reject', () => {
      // #TEST Promise.reject

      // 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)
      // #END
    })

    test('all', () => {
      // #TEST Promise.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) => {
          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");
        })
      // #END
    })

    test('allSettled', () => {
      // #TEST Promise.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 : number) => {
            statusArr.add(item.status)
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 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
          expect(statusArr).toEqual(["fulfilled", "fulfilled", "rejected", "rejected"])
        })
      // #END
    })

    test('any', () => {
      // #TEST Promise.any
      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);
            // #ifdef APP-IOS
            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

            // #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
381
          })
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
      // #END
    })

    test('race', () => {
      // #TEST Promise.race
      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);
418
        })
419
      // #END
420
    })
421 422
  })
}