From 3c5045a2d413f8c3a8c671808bf3a2b4dd2dae03 Mon Sep 17 00:00:00 2001 From: mahaifeng Date: Sat, 14 Sep 2024 10:27:11 +0800 Subject: [PATCH] =?UTF-8?q?[promise,global]=E5=8E=BB=E9=99=A4=E6=96=87?= =?UTF-8?q?=E6=A1=A3=E4=B8=AD=E6=89=8B=E5=8A=A8=E7=94=9F=E6=88=90=E7=9A=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=EF=BC=8C=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- uni_modules/uts-tests/utssdk/Global.uts | 206 +++--- uni_modules/uts-tests/utssdk/Promise.uts | 793 ++++++++++++----------- 2 files changed, 517 insertions(+), 482 deletions(-) diff --git a/uni_modules/uts-tests/utssdk/Global.uts b/uni_modules/uts-tests/utssdk/Global.uts index 7e1e1f4..a4f5fd6 100644 --- a/uni_modules/uts-tests/utssdk/Global.uts +++ b/uni_modules/uts-tests/utssdk/Global.uts @@ -1,117 +1,133 @@ import { describe, test, expect, Result } from './tests.uts' -export function testGlobal(): Result { - return describe("Global", () => { - - +export function testGlobal() : Result { + return describe("Global", () => { + + test('setInterval', () => { - + // #TEST Global.setInterval // #ifdef APP-ANDROID let aCount = 0 - let taskId = setInterval(function(){ - aCount += 1 - },0) - setTimeout(function(){ - console.log(aCount) - console.log(taskId) - clearInterval(taskId) - expect(aCount > 10).toEqual(true); - },200) + let taskId = setInterval(function () { + aCount += 1 + }, 0) + setTimeout(function () { + console.log(aCount) + console.log(taskId) + clearInterval(taskId) + expect(aCount > 10).toEqual(true); + }, 200) // #endif + // #END }) - + test('btoa', () => { + // #TEST Global.btoa // #ifdef APP-ANDROID expect(btoa("")).toEqual(''); expect(btoa("hello world")).toEqual('aGVsbG8gd29ybGQ='); expect(btoa("@-1234567890qwertyuiopasdfghjklzxcvbnm-QWERTYUIOPASDFGHJKLZXCVBNM@-1234567890qwertyuiopasdfghjklzxcvbnm-QWERTYUIOPASDFGHJKLZXCVBNM@-1234567890qwertyuiopasdfghjklzxcvbnm-QWERTYUIOPASDFGHJKLZXCVBNM@-1234567890qwertyuiopasdfghjklzxcvbnm-QWERTYUIOPASDFGHJKLZXCVBNM@-1234567890qwertyuiopasdfghjklzxcvbnm-QWERTYUIOPASDFGHJKLZXCVBNM")).toEqual('QC0xMjM0NTY3ODkwcXdlcnR5dWlvcGFzZGZnaGprbHp4Y3Zibm0tUVdFUlRZVUlPUEFTREZHSEpLTFpYQ1ZCTk1ALTEyMzQ1Njc4OTBxd2VydHl1aW9wYXNkZmdoamtsenhjdmJubS1RV0VSVFlVSU9QQVNERkdISktMWlhDVkJOTUAtMTIzNDU2Nzg5MHF3ZXJ0eXVpb3Bhc2RmZ2hqa2x6eGN2Ym5tLVFXRVJUWVVJT1BBU0RGR0hKS0xaWENWQk5NQC0xMjM0NTY3ODkwcXdlcnR5dWlvcGFzZGZnaGprbHp4Y3Zibm0tUVdFUlRZVUlPUEFTREZHSEpLTFpYQ1ZCTk1ALTEyMzQ1Njc4OTBxd2VydHl1aW9wYXNkZmdoamtsenhjdmJubS1RV0VSVFlVSU9QQVNERkdISktMWlhDVkJOTQ=='); // #endif + // #END }) - + test('atob', () => { + // #TEST Global.atob // #ifdef APP-ANDROID expect(atob("")).toEqual(''); expect(atob('aGVsbG8gd29ybGQ=')).toEqual('hello world'); expect(atob("QC0xMjM0NTY3ODkwcXdlcnR5dWlvcGFzZGZnaGprbHp4Y3Zibm0tUVdFUlRZVUlPUEFTREZHSEpLTFpYQ1ZCTk1ALTEyMzQ1Njc4OTBxd2VydHl1aW9wYXNkZmdoamtsenhjdmJubS1RV0VSVFlVSU9QQVNERkdISktMWlhDVkJOTUAtMTIzNDU2Nzg5MHF3ZXJ0eXVpb3Bhc2RmZ2hqa2x6eGN2Ym5tLVFXRVJUWVVJT1BBU0RGR0hKS0xaWENWQk5NQC0xMjM0NTY3ODkwcXdlcnR5dWlvcGFzZGZnaGprbHp4Y3Zibm0tUVdFUlRZVUlPUEFTREZHSEpLTFpYQ1ZCTk1ALTEyMzQ1Njc4OTBxd2VydHl1aW9wYXNkZmdoamtsenhjdmJubS1RV0VSVFlVSU9QQVNERkdISktMWlhDVkJOTQ==")).toEqual('@-1234567890qwertyuiopasdfghjklzxcvbnm-QWERTYUIOPASDFGHJKLZXCVBNM@-1234567890qwertyuiopasdfghjklzxcvbnm-QWERTYUIOPASDFGHJKLZXCVBNM@-1234567890qwertyuiopasdfghjklzxcvbnm-QWERTYUIOPASDFGHJKLZXCVBNM@-1234567890qwertyuiopasdfghjklzxcvbnm-QWERTYUIOPASDFGHJKLZXCVBNM@-1234567890qwertyuiopasdfghjklzxcvbnm-QWERTYUIOPASDFGHJKLZXCVBNM'); // #endif + // #END }) - - test('parseInt', () => { - expect(parseInt("123.456")).toEqual(123); - expect(parseInt("123")).toEqual(123); - expect(parseInt("123", 10)).toEqual(123); - expect(parseInt(" 123", 10)).toEqual(123); - expect(parseInt("077")).toEqual(77); - expect(parseInt("1.9")).toEqual(1); - expect(parseInt("ff", 16)).toEqual(255); - expect(parseInt("0xFF", 0)).toEqual(255); - expect(parseInt("0xFF", 16)).toEqual(255); - expect(parseInt("0xFF")).toEqual(255); - expect(parseInt("0xFF", 10)).toEqual(0); - expect(parseInt("0xF", 16)).toEqual(15); - expect(parseInt("F", 16)).toEqual(15); - expect(parseInt("021", 8)).toEqual(17); - expect(parseInt("015", 10)).toEqual(15); - expect(parseInt("15,123", 10)).toEqual(15); - expect(parseInt("FXX123.99", 16)).toEqual(15); - expect(parseInt("1111", 2)).toEqual(15); - expect(parseInt("15 * 3", 10)).toEqual(15); - expect(parseInt("15e2", 10)).toEqual(15); - expect(parseInt("15px", 10)).toEqual(15); - expect(parseInt("12", 13)).toEqual(15); - expect(parseInt("17", 8)).toEqual(15); - expect(isNaN(parseInt("a"))).toEqual(true) - expect(parseInt("a", 16)).toEqual(10) - expect(isNaN(parseInt("345", 2))).toEqual(true) - expect(isNaN(parseInt("hello"))).toEqual(true) - expect(parseInt("6.022e23")).toEqual(6) - expect(parseInt("9223372036854775807")).toEqualNumber(9.223372036854778e+18) - expect(parseInt("9223372036854775807000")).toEqualNumber(9.223372036854777e+21) - expect(parseInt("922337203685477580700099999999999999999999999999999999999999999999999999999999999999999")).toEqualNumber(9.223372036854776e+86) - expect(parseInt("50") / 100).toEqualNumber(0.5); - expect(parseInt("500") / 100).toEqualNumber(5); - - - }) - - test('parseFloat', () => { - expect(parseFloat("11.20")).toEqual(11.2); - expect(parseFloat("3.14")).toEqual(3.14); - expect(parseFloat(" 3.14 ")).toEqual(3.14); - expect(parseFloat("314e-2")).toEqual(3.14); - expect(parseFloat("0.0314E+2")).toEqual(3.14); - expect(parseFloat("3.14some non-digit characters")).toEqual(3.14); - expect(parseFloat("100") / 50).toEqualNumber(2); - expect(parseFloat("105") / 50).toEqual(2.1); - }) - - test('encodeURI', () => { - expect(encodeURI("你好")).toEqual('%E4%BD%A0%E5%A5%BD') - expect(encodeURI("你好+22")).toEqual('%E4%BD%A0%E5%A5%BD+22') - expect(encodeURI("你;/=好+22")).toEqual('%E4%BD%A0;/=%E5%A5%BD+22') - expect(encodeURI("https://demo.dcloud.net.cn/mock/最新/12")).toEqual('https://demo.dcloud.net.cn/mock/%E6%9C%80%E6%96%B0/12') - }) - - test('decodeURI', () => { - expect(decodeURI('%E4%BD%A0%E5%A5%BD')).toEqual('你好') - expect(decodeURI("%E4%BD%A0%E5%A5%BD+22")).toEqual('你好+22') - expect(decodeURI("%E4%BD%A0;/=%E5%A5%BD+22")).toEqual('你;/=好+22') - expect(decodeURI("https://demo.dcloud.net.cn/mock/%E6%9C%80%E6%96%B0/12")).toEqual('https://demo.dcloud.net.cn/mock/最新/12') - }) - - test('encodeURIComponent', () => { - expect(encodeURIComponent("你好")).toEqual('%E4%BD%A0%E5%A5%BD') - expect(encodeURIComponent("你好+22")).toEqual('%E4%BD%A0%E5%A5%BD%2B22') - expect(encodeURIComponent("你;/=好+22")).toEqual('%E4%BD%A0%3B%2F%3D%E5%A5%BD%2B22') - expect(encodeURIComponent("https://demo.dcloud.net.cn/mock/最新/12")).toEqual('https%3A%2F%2Fdemo.dcloud.net.cn%2Fmock%2F%E6%9C%80%E6%96%B0%2F12') - }) - - test('decodeURIComponent', () => { - expect(decodeURIComponent('%E4%BD%A0%E5%A5%BD')).toEqual('你好') - expect(decodeURIComponent('%E4%BD%A0%E5%A5%BD%2B22')).toEqual('你好+22') - expect(decodeURIComponent('%E4%BD%A0%3B%2F%3D%E5%A5%BD%2B22')).toEqual('你;/=好+22') - expect(decodeURIComponent("https://demo.dcloud.net.cn/mock/%E6%9C%80%E6%96%B0/12")).toEqual('https://demo.dcloud.net.cn/mock/最新/12') - }) - + + test('parseInt', () => { + // #TEST Global.parseInt,Global.isNaN + expect(parseInt("123.456")).toEqual(123); + expect(parseInt("123")).toEqual(123); + expect(parseInt("123", 10)).toEqual(123); + expect(parseInt(" 123", 10)).toEqual(123); + expect(parseInt("077")).toEqual(77); + expect(parseInt("1.9")).toEqual(1); + expect(parseInt("ff", 16)).toEqual(255); + expect(parseInt("0xFF", 0)).toEqual(255); + expect(parseInt("0xFF", 16)).toEqual(255); + expect(parseInt("0xFF")).toEqual(255); + expect(parseInt("0xFF", 10)).toEqual(0); + expect(parseInt("0xF", 16)).toEqual(15); + expect(parseInt("F", 16)).toEqual(15); + expect(parseInt("021", 8)).toEqual(17); + expect(parseInt("015", 10)).toEqual(15); + expect(parseInt("15,123", 10)).toEqual(15); + expect(parseInt("FXX123.99", 16)).toEqual(15); + expect(parseInt("1111", 2)).toEqual(15); + expect(parseInt("15 * 3", 10)).toEqual(15); + expect(parseInt("15e2", 10)).toEqual(15); + expect(parseInt("15px", 10)).toEqual(15); + expect(parseInt("12", 13)).toEqual(15); + expect(parseInt("17", 8)).toEqual(15); + expect(isNaN(parseInt("a"))).toEqual(true) + expect(parseInt("a", 16)).toEqual(10) + expect(isNaN(parseInt("345", 2))).toEqual(true) + expect(isNaN(parseInt("hello"))).toEqual(true) + expect(parseInt("6.022e23")).toEqual(6) + expect(parseInt("9223372036854775807")).toEqualNumber(9.223372036854778e+18) + expect(parseInt("9223372036854775807000")).toEqualNumber(9.223372036854777e+21) + expect(parseInt("922337203685477580700099999999999999999999999999999999999999999999999999999999999999999")).toEqualNumber(9.223372036854776e+86) + expect(parseInt("50") / 100).toEqualNumber(0.5); + expect(parseInt("500") / 100).toEqualNumber(5); + + // #END + }) + + test('parseFloat', () => { + // #TEST Global.parseFloat + expect(parseFloat("11.20")).toEqual(11.2); + expect(parseFloat("3.14")).toEqual(3.14); + expect(parseFloat(" 3.14 ")).toEqual(3.14); + expect(parseFloat("314e-2")).toEqual(3.14); + expect(parseFloat("0.0314E+2")).toEqual(3.14); + expect(parseFloat("3.14some non-digit characters")).toEqual(3.14); + expect(parseFloat("100") / 50).toEqualNumber(2); + expect(parseFloat("105") / 50).toEqual(2.1); + // #END }) -} + + test('encodeURI', () => { + // #TEST Global.encodeURI + expect(encodeURI("你好")).toEqual('%E4%BD%A0%E5%A5%BD') + expect(encodeURI("你好+22")).toEqual('%E4%BD%A0%E5%A5%BD+22') + expect(encodeURI("你;/=好+22")).toEqual('%E4%BD%A0;/=%E5%A5%BD+22') + expect(encodeURI("https://demo.dcloud.net.cn/mock/最新/12")).toEqual('https://demo.dcloud.net.cn/mock/%E6%9C%80%E6%96%B0/12') + // #END + }) + + test('decodeURI', () => { + // #TEST Global.decodeURI + expect(decodeURI('%E4%BD%A0%E5%A5%BD')).toEqual('你好') + expect(decodeURI("%E4%BD%A0%E5%A5%BD+22")).toEqual('你好+22') + expect(decodeURI("%E4%BD%A0;/=%E5%A5%BD+22")).toEqual('你;/=好+22') + expect(decodeURI("https://demo.dcloud.net.cn/mock/%E6%9C%80%E6%96%B0/12")).toEqual('https://demo.dcloud.net.cn/mock/最新/12') + // #END + }) + + test('encodeURIComponent', () => { + // #TEST Global.encodeURIComponent + expect(encodeURIComponent("你好")).toEqual('%E4%BD%A0%E5%A5%BD') + expect(encodeURIComponent("你好+22")).toEqual('%E4%BD%A0%E5%A5%BD%2B22') + expect(encodeURIComponent("你;/=好+22")).toEqual('%E4%BD%A0%3B%2F%3D%E5%A5%BD%2B22') + expect(encodeURIComponent("https://demo.dcloud.net.cn/mock/最新/12")).toEqual('https%3A%2F%2Fdemo.dcloud.net.cn%2Fmock%2F%E6%9C%80%E6%96%B0%2F12') + // #END + }) + + test('decodeURIComponent', () => { + // #TEST Global.decodeURIComponent + expect(decodeURIComponent('%E4%BD%A0%E5%A5%BD')).toEqual('你好') + expect(decodeURIComponent('%E4%BD%A0%E5%A5%BD%2B22')).toEqual('你好+22') + expect(decodeURIComponent('%E4%BD%A0%3B%2F%3D%E5%A5%BD%2B22')).toEqual('你;/=好+22') + expect(decodeURIComponent("https://demo.dcloud.net.cn/mock/%E6%9C%80%E6%96%B0/12")).toEqual('https://demo.dcloud.net.cn/mock/最新/12') + // #END + }) + + }) +} \ No newline at end of file diff --git a/uni_modules/uts-tests/utssdk/Promise.uts b/uni_modules/uts-tests/utssdk/Promise.uts index 5cd8e61..1bf8840 100644 --- a/uni_modules/uts-tests/utssdk/Promise.uts +++ b/uni_modules/uts-tests/utssdk/Promise.uts @@ -1,403 +1,422 @@ import { describe, test, expect, Result } from './tests.uts' -export function testPromise(): Result { - return describe("Promise", () => { - test('constructor', () => { - // 常规方式 - let p = new Promise((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((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) - }) +export function testPromise() : Result { + return describe("Promise", () => { + test('constructor', () => { + // #TEST Promise.Constructor,Promise.Constructor_1 + // 常规方式 + let p = new Promise((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((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((reslove, reject) => { - setTimeout(function() { - reslove("hello world") - }, 13); - }) - - p.then( - (res: string): Promise => { - expect(res).toEqual("hello world") - let p0 = new Promise((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") - }) + // #END + }) + + test('then', () => { + // #TEST Promise.then,Promise.then_1,Promise.then_2,Promise.then_3,Promise.then_4 + let p = new Promise((reslove, reject) => { + setTimeout(function () { + reslove("hello world") + }, 13); + }) + + p.then( + (res : string) : Promise => { + expect(res).toEqual("hello world") + let p0 = new Promise((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") }) - - test('catch', () => { - let p = Promise.reject() - p.then() - .catch((res: any | null) => { - expect(res).toEqual(null) - }) - - // catch 指定返回值 - new Promise((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((resolve, reject) => { - setTimeout(() => { - reject("2024") - }, 12) - }) - .then() - .catch((res: any | null): Promise => { - expect(res!).toEqual("2024") - let p = new Promise((resolve, reject) => { - resolve(1) - }) - return p - }) - .then((res) => { - expect(res).toEqual(1) - }) + // #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) }) - - test('finally', () => { - 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) - }) - - 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 => { - return new Promise((resolve, reject) => { - setTimeout(() => { - resolve(20) - }, 1000) - }) - }) - .catch((res) => { - expect(res).toEqual(4) - }) + + // catch 指定返回值 + new Promise((resolve, reject) => { + setTimeout(() => { + reject("2024") + }, 12) + }) + .then() + .catch((res : any | null) : string => { + expect(res!).toEqual("2024") + return "hello world" }) - - test('resolve', () => { - // 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) + .then((res) => { + expect(res).toEqual("hello world") }) - - test('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) === original).toEqual(true); + + // catch 返回Promise + new Promise((resolve, reject) => { + setTimeout(() => { + reject("2024") + }, 12) + }) + .then() + .catch((res : any | null) : Promise => { + expect(res!).toEqual("2024") + let p = new Promise((resolve, reject) => { + resolve(1) }) - expect(original === cast).toEqual(false) - + return p }) - - test('all', () => { - let p0 = new Promise((resolve, reject) => { - setTimeout(function() { - resolve("1"); - }, 10); - }) - - let p1 = new Promise((resolve, reject) => { - setTimeout(function() { - resolve("2"); - }, 10); - }) - - let p2 = new Promise((resolve, reject) => { - setTimeout(function() { - resolve("3"); - }, 10); - }) - - let p3 = new Promise((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"); - }) + .then((res) => { + expect(res).toEqual(1) }) - - test('allSettled', () => { - let p0 = new Promise((resolve, reject) => { - setTimeout(function() { - resolve("1"); - }, 10); - }) - - let p1 = new Promise((resolve, reject) => { - setTimeout(function() { - resolve(null); - }, 10); - }) - - let p2 = new Promise((resolve, reject) => { - setTimeout(function() { - reject(null) - }, 10); - }) - - let p3 = new Promise((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) - }) - expect(statusArr).toEqual(["fulfilled", "fulfilled", "rejected", "rejected"]) - }) + // #END + }) + + test('finally', () => { + // #TEST Promise.finally + + Promise.resolve(2) + .finally(() => { + console.log("finally") }) - - test('any', () => { - let p0 = new Promise((resolve, reject) => { - setTimeout(function() { - resolve("1"); - }, 10); - }) - - let p1 = new Promise((resolve, reject) => { - setTimeout(function() { - resolve(null); - }, 15); - }) - - let p2 = new Promise((resolve, reject) => { - setTimeout(function() { - reject(null) - }, 10); - }) - - let p3 = new Promise((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 - }) + .then((res) => { + expect(res).toEqual(2) }) - - test('race', () => { - let p0 = new Promise((resolve, reject) => { - setTimeout(function() { - resolve("fast"); - }, 10); - }) - - let p1 = new Promise((resolve, reject) => { - setTimeout(function() { - resolve("slow"); - }, 15); - }) - - let p2 = new Promise((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"); + + 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 => { + 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) === original).toEqual(true); + }) + expect(original === cast).toEqual(false) + // #END + }) + + test('all', () => { + // #TEST Promise.all + let p0 = new Promise((resolve, reject) => { + setTimeout(function () { + resolve("1"); + }, 10); + }) + + let p1 = new Promise((resolve, reject) => { + setTimeout(function () { + resolve("2"); + }, 10); + }) + + let p2 = new Promise((resolve, reject) => { + setTimeout(function () { + resolve("3"); + }, 10); + }) + + let p3 = new Promise((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((resolve, reject) => { + setTimeout(function () { + resolve("1"); + }, 10); + }) + + let p1 = new Promise((resolve, reject) => { + setTimeout(function () { + resolve(null); + }, 10); + }) + + let p2 = new Promise((resolve, reject) => { + setTimeout(function () { + reject(null) + }, 10); + }) + + let p3 = new Promise((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) }) - - // test for reject - Promise.race([p1, p2]) - .then() - .catch((error) => { - console.log("test for race reject ===> ", error); - expect(error).toEqual(null); + expect(statusArr).toEqual(["fulfilled", "fulfilled", "rejected", "rejected"]) + }) + // #END + }) + + test('any', () => { + // #TEST Promise.any + let p0 = new Promise((resolve, reject) => { + setTimeout(function () { + resolve("1"); + }, 10); + }) + + let p1 = new Promise((resolve, reject) => { + setTimeout(function () { + resolve(null); + }, 15); + }) + + let p2 = new Promise((resolve, reject) => { + setTimeout(function () { + reject(null) + }, 10); + }) + + let p3 = new Promise((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 }) + // #END + }) + + test('race', () => { + // #TEST Promise.race + let p0 = new Promise((resolve, reject) => { + setTimeout(function () { + resolve("fast"); + }, 10); + }) + + let p1 = new Promise((resolve, reject) => { + setTimeout(function () { + resolve("slow"); + }, 15); + }) + + let p2 = new Promise((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); }) + // #END }) -} + }) +} \ No newline at end of file -- GitLab