提交 3c5045a2 编写于 作者: M mahaifeng

[promise,global]去除文档中手动生成的代码,添加注释

上级 26658618
import { describe, test, expect, Result } from './tests.uts' import { describe, test, expect, Result } from './tests.uts'
export function testGlobal(): Result { export function testGlobal() : Result {
return describe("Global", () => { return describe("Global", () => {
test('setInterval', () => { test('setInterval', () => {
// #TEST Global.setInterval
// #ifdef APP-ANDROID // #ifdef APP-ANDROID
let aCount = 0 let aCount = 0
let taskId = setInterval(function(){ let taskId = setInterval(function () {
aCount += 1 aCount += 1
},0) }, 0)
setTimeout(function(){ setTimeout(function () {
console.log(aCount) console.log(aCount)
console.log(taskId) console.log(taskId)
clearInterval(taskId) clearInterval(taskId)
expect(aCount > 10).toEqual(true); expect(aCount > 10).toEqual(true);
},200) }, 200)
// #endif // #endif
// #END
}) })
test('btoa', () => { test('btoa', () => {
// #TEST Global.btoa
// #ifdef APP-ANDROID // #ifdef APP-ANDROID
expect(btoa("")).toEqual(''); expect(btoa("")).toEqual('');
expect(btoa("hello world")).toEqual('aGVsbG8gd29ybGQ='); expect(btoa("hello world")).toEqual('aGVsbG8gd29ybGQ=');
expect(btoa("@-1234567890qwertyuiopasdfghjklzxcvbnm-QWERTYUIOPASDFGHJKLZXCVBNM@-1234567890qwertyuiopasdfghjklzxcvbnm-QWERTYUIOPASDFGHJKLZXCVBNM@-1234567890qwertyuiopasdfghjklzxcvbnm-QWERTYUIOPASDFGHJKLZXCVBNM@-1234567890qwertyuiopasdfghjklzxcvbnm-QWERTYUIOPASDFGHJKLZXCVBNM@-1234567890qwertyuiopasdfghjklzxcvbnm-QWERTYUIOPASDFGHJKLZXCVBNM")).toEqual('QC0xMjM0NTY3ODkwcXdlcnR5dWlvcGFzZGZnaGprbHp4Y3Zibm0tUVdFUlRZVUlPUEFTREZHSEpLTFpYQ1ZCTk1ALTEyMzQ1Njc4OTBxd2VydHl1aW9wYXNkZmdoamtsenhjdmJubS1RV0VSVFlVSU9QQVNERkdISktMWlhDVkJOTUAtMTIzNDU2Nzg5MHF3ZXJ0eXVpb3Bhc2RmZ2hqa2x6eGN2Ym5tLVFXRVJUWVVJT1BBU0RGR0hKS0xaWENWQk5NQC0xMjM0NTY3ODkwcXdlcnR5dWlvcGFzZGZnaGprbHp4Y3Zibm0tUVdFUlRZVUlPUEFTREZHSEpLTFpYQ1ZCTk1ALTEyMzQ1Njc4OTBxd2VydHl1aW9wYXNkZmdoamtsenhjdmJubS1RV0VSVFlVSU9QQVNERkdISktMWlhDVkJOTQ=='); expect(btoa("@-1234567890qwertyuiopasdfghjklzxcvbnm-QWERTYUIOPASDFGHJKLZXCVBNM@-1234567890qwertyuiopasdfghjklzxcvbnm-QWERTYUIOPASDFGHJKLZXCVBNM@-1234567890qwertyuiopasdfghjklzxcvbnm-QWERTYUIOPASDFGHJKLZXCVBNM@-1234567890qwertyuiopasdfghjklzxcvbnm-QWERTYUIOPASDFGHJKLZXCVBNM@-1234567890qwertyuiopasdfghjklzxcvbnm-QWERTYUIOPASDFGHJKLZXCVBNM")).toEqual('QC0xMjM0NTY3ODkwcXdlcnR5dWlvcGFzZGZnaGprbHp4Y3Zibm0tUVdFUlRZVUlPUEFTREZHSEpLTFpYQ1ZCTk1ALTEyMzQ1Njc4OTBxd2VydHl1aW9wYXNkZmdoamtsenhjdmJubS1RV0VSVFlVSU9QQVNERkdISktMWlhDVkJOTUAtMTIzNDU2Nzg5MHF3ZXJ0eXVpb3Bhc2RmZ2hqa2x6eGN2Ym5tLVFXRVJUWVVJT1BBU0RGR0hKS0xaWENWQk5NQC0xMjM0NTY3ODkwcXdlcnR5dWlvcGFzZGZnaGprbHp4Y3Zibm0tUVdFUlRZVUlPUEFTREZHSEpLTFpYQ1ZCTk1ALTEyMzQ1Njc4OTBxd2VydHl1aW9wYXNkZmdoamtsenhjdmJubS1RV0VSVFlVSU9QQVNERkdISktMWlhDVkJOTQ==');
// #endif // #endif
// #END
}) })
test('atob', () => { test('atob', () => {
// #TEST Global.atob
// #ifdef APP-ANDROID // #ifdef APP-ANDROID
expect(atob("")).toEqual(''); expect(atob("")).toEqual('');
expect(atob('aGVsbG8gd29ybGQ=')).toEqual('hello world'); expect(atob('aGVsbG8gd29ybGQ=')).toEqual('hello world');
expect(atob("QC0xMjM0NTY3ODkwcXdlcnR5dWlvcGFzZGZnaGprbHp4Y3Zibm0tUVdFUlRZVUlPUEFTREZHSEpLTFpYQ1ZCTk1ALTEyMzQ1Njc4OTBxd2VydHl1aW9wYXNkZmdoamtsenhjdmJubS1RV0VSVFlVSU9QQVNERkdISktMWlhDVkJOTUAtMTIzNDU2Nzg5MHF3ZXJ0eXVpb3Bhc2RmZ2hqa2x6eGN2Ym5tLVFXRVJUWVVJT1BBU0RGR0hKS0xaWENWQk5NQC0xMjM0NTY3ODkwcXdlcnR5dWlvcGFzZGZnaGprbHp4Y3Zibm0tUVdFUlRZVUlPUEFTREZHSEpLTFpYQ1ZCTk1ALTEyMzQ1Njc4OTBxd2VydHl1aW9wYXNkZmdoamtsenhjdmJubS1RV0VSVFlVSU9QQVNERkdISktMWlhDVkJOTQ==")).toEqual('@-1234567890qwertyuiopasdfghjklzxcvbnm-QWERTYUIOPASDFGHJKLZXCVBNM@-1234567890qwertyuiopasdfghjklzxcvbnm-QWERTYUIOPASDFGHJKLZXCVBNM@-1234567890qwertyuiopasdfghjklzxcvbnm-QWERTYUIOPASDFGHJKLZXCVBNM@-1234567890qwertyuiopasdfghjklzxcvbnm-QWERTYUIOPASDFGHJKLZXCVBNM@-1234567890qwertyuiopasdfghjklzxcvbnm-QWERTYUIOPASDFGHJKLZXCVBNM'); expect(atob("QC0xMjM0NTY3ODkwcXdlcnR5dWlvcGFzZGZnaGprbHp4Y3Zibm0tUVdFUlRZVUlPUEFTREZHSEpLTFpYQ1ZCTk1ALTEyMzQ1Njc4OTBxd2VydHl1aW9wYXNkZmdoamtsenhjdmJubS1RV0VSVFlVSU9QQVNERkdISktMWlhDVkJOTUAtMTIzNDU2Nzg5MHF3ZXJ0eXVpb3Bhc2RmZ2hqa2x6eGN2Ym5tLVFXRVJUWVVJT1BBU0RGR0hKS0xaWENWQk5NQC0xMjM0NTY3ODkwcXdlcnR5dWlvcGFzZGZnaGprbHp4Y3Zibm0tUVdFUlRZVUlPUEFTREZHSEpLTFpYQ1ZCTk1ALTEyMzQ1Njc4OTBxd2VydHl1aW9wYXNkZmdoamtsenhjdmJubS1RV0VSVFlVSU9QQVNERkdISktMWlhDVkJOTQ==")).toEqual('@-1234567890qwertyuiopasdfghjklzxcvbnm-QWERTYUIOPASDFGHJKLZXCVBNM@-1234567890qwertyuiopasdfghjklzxcvbnm-QWERTYUIOPASDFGHJKLZXCVBNM@-1234567890qwertyuiopasdfghjklzxcvbnm-QWERTYUIOPASDFGHJKLZXCVBNM@-1234567890qwertyuiopasdfghjklzxcvbnm-QWERTYUIOPASDFGHJKLZXCVBNM@-1234567890qwertyuiopasdfghjklzxcvbnm-QWERTYUIOPASDFGHJKLZXCVBNM');
// #endif // #endif
// #END
}) })
test('parseInt', () => { test('parseInt', () => {
// #TEST Global.parseInt,Global.isNaN
expect(parseInt("123.456")).toEqual(123); expect(parseInt("123.456")).toEqual(123);
expect(parseInt("123")).toEqual(123); expect(parseInt("123")).toEqual(123);
expect(parseInt("123", 10)).toEqual(123); expect(parseInt("123", 10)).toEqual(123);
...@@ -71,10 +77,11 @@ export function testGlobal(): Result { ...@@ -71,10 +77,11 @@ export function testGlobal(): Result {
expect(parseInt("50") / 100).toEqualNumber(0.5); expect(parseInt("50") / 100).toEqualNumber(0.5);
expect(parseInt("500") / 100).toEqualNumber(5); expect(parseInt("500") / 100).toEqualNumber(5);
// #END
}) })
test('parseFloat', () => { test('parseFloat', () => {
// #TEST Global.parseFloat
expect(parseFloat("11.20")).toEqual(11.2); expect(parseFloat("11.20")).toEqual(11.2);
expect(parseFloat("3.14")).toEqual(3.14); expect(parseFloat("3.14")).toEqual(3.14);
expect(parseFloat(" 3.14 ")).toEqual(3.14); expect(parseFloat(" 3.14 ")).toEqual(3.14);
...@@ -83,34 +90,43 @@ export function testGlobal(): Result { ...@@ -83,34 +90,43 @@ export function testGlobal(): Result {
expect(parseFloat("3.14some non-digit characters")).toEqual(3.14); expect(parseFloat("3.14some non-digit characters")).toEqual(3.14);
expect(parseFloat("100") / 50).toEqualNumber(2); expect(parseFloat("100") / 50).toEqualNumber(2);
expect(parseFloat("105") / 50).toEqual(2.1); expect(parseFloat("105") / 50).toEqual(2.1);
// #END
}) })
test('encodeURI', () => { test('encodeURI', () => {
// #TEST Global.encodeURI
expect(encodeURI("你好")).toEqual('%E4%BD%A0%E5%A5%BD') 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("你;/=好+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') 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('decodeURI', () => {
// #TEST Global.decodeURI
expect(decodeURI('%E4%BD%A0%E5%A5%BD')).toEqual('你好') 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("%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') 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('encodeURIComponent', () => {
// #TEST Global.encodeURIComponent
expect(encodeURIComponent("你好")).toEqual('%E4%BD%A0%E5%A5%BD') 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%E5%A5%BD%2B22')
expect(encodeURIComponent("你;/=好+22")).toEqual('%E4%BD%A0%3B%2F%3D%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') 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('decodeURIComponent', () => {
// #TEST Global.decodeURIComponent
expect(decodeURIComponent('%E4%BD%A0%E5%A5%BD')).toEqual('你好') 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%E5%A5%BD%2B22')).toEqual('你好+22')
expect(decodeURIComponent('%E4%BD%A0%3B%2F%3D%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') expect(decodeURIComponent("https://demo.dcloud.net.cn/mock/%E6%9C%80%E6%96%B0/12")).toEqual('https://demo.dcloud.net.cn/mock/最新/12')
// #END
}) })
}) })
......
import { describe, test, expect, Result } from './tests.uts' import { describe, test, expect, Result } from './tests.uts'
export function testPromise(): Result { export function testPromise() : Result {
return describe("Promise", () => { return describe("Promise", () => {
test('constructor', () => { test('constructor', () => {
// #TEST Promise.Constructor,Promise.Constructor_1
// 常规方式 // 常规方式
let p = new Promise<number>((resolve, reject) => { let p = new Promise<number>((resolve, reject) => {
let success = true; let success = true;
setTimeout(()=>{ setTimeout(() => {
if (success) { if (success) {
resolve(2024) resolve(2024)
} else{ } else {
let error = new Error("something going wrong") let error = new Error("something going wrong")
reject(error) reject(error)
} }
}, 10); }, 10);
}); });
p.then((res: number) => { p.then((res : number) => {
expect(res).toEqual(2024) expect(res).toEqual(2024)
}) })
// 单个函数 // 单个函数
let p1 = new Promise<any | null>((resolve: (value: any | null) => void) => { let p1 = new Promise<any | null>((resolve : (value : any | null) => void) => {
setTimeout(function() { setTimeout(function () {
resolve(null) resolve(null)
}, 10); }, 10);
}) })
p1.then((res: any | null) => { p1.then((res : any | null) => {
expect(res).toEqual(null) expect(res).toEqual(null)
}) })
// 指定函数类型 // 指定函数类型
let p2 = new Promise((resolve: (value: string) => void, reject: (error: any | null) => void) => { let p2 = new Promise((resolve : (value : string) => void, reject : (error : any | null) => void) => {
setTimeout(function() { setTimeout(function () {
reject(null) reject(null)
}, 10); }, 10);
}) })
p2.then() p2.then()
.catch((error: any | null) => { .catch((error : any | null) => {
expect(error).toEqual(null) expect(error).toEqual(null)
}) })
// #END
}) })
test('then', () => { test('then', () => {
// #TEST Promise.then,Promise.then_1,Promise.then_2,Promise.then_3,Promise.then_4
let p = new Promise<string>((reslove, reject) => { let p = new Promise<string>((reslove, reject) => {
setTimeout(function() { setTimeout(function () {
reslove("hello world") reslove("hello world")
}, 13); }, 13);
}) })
p.then( p.then(
(res: string): Promise<string> => { (res : string) : Promise<string> => {
expect(res).toEqual("hello world") expect(res).toEqual("hello world")
let p0 = new Promise<string>((reslove,reject) => { let p0 = new Promise<string>((reslove, reject) => {
var success = true; var success = true;
setTimeout(function() { setTimeout(function () {
success = true; success = true;
if (success) { if (success) {
reslove(res + " 2024") reslove(res + " 2024")
} else{ } else {
reject("first then p0 reject message") reject("first then p0 reject message")
} }
}, 8); }, 8);
}) })
return p0; return p0;
}, },
(err: any | null): void => { (err : any | null) : void => {
throw new Error("first then throw error") throw new Error("first then throw error")
} }
) )
.catch( .catch(
(err):string => { (err) : string => {
return "first catch return message" return "first catch return message"
} }
) )
.then( .then(
(res) => { (res) => {
console.log(res)
expect(res).toEqual("hello world 2024") expect(res).toEqual("hello world 2024")
} }
) )
...@@ -85,15 +87,17 @@ export function testPromise(): Result { ...@@ -85,15 +87,17 @@ export function testPromise(): Result {
} }
) )
.finally(() => { .finally(() => {
console.log("finally")
expect("finally").toEqual("finally") expect("finally").toEqual("finally")
}) })
// #END
}) })
test('catch', () => { test('catch', () => {
// #TEST Promise.catch,Promise.catch_1,Promise.catch_2,Promise.catch_3,Promise.catch_4
let p = Promise.reject() let p = Promise.reject()
p.then() p.then()
.catch((res: any | null) => { .catch((res : any | null) => {
expect(res).toEqual(null) expect(res).toEqual(null)
}) })
...@@ -104,7 +108,7 @@ export function testPromise(): Result { ...@@ -104,7 +108,7 @@ export function testPromise(): Result {
}, 12) }, 12)
}) })
.then() .then()
.catch((res: any | null): string => { .catch((res : any | null) : string => {
expect(res!).toEqual("2024") expect(res!).toEqual("2024")
return "hello world" return "hello world"
}) })
...@@ -119,7 +123,7 @@ export function testPromise(): Result { ...@@ -119,7 +123,7 @@ export function testPromise(): Result {
}, 12) }, 12)
}) })
.then() .then()
.catch((res: any | null): Promise<number> => { .catch((res : any | null) : Promise<number> => {
expect(res!).toEqual("2024") expect(res!).toEqual("2024")
let p = new Promise<number>((resolve, reject) => { let p = new Promise<number>((resolve, reject) => {
resolve(1) resolve(1)
...@@ -129,9 +133,12 @@ export function testPromise(): Result { ...@@ -129,9 +133,12 @@ export function testPromise(): Result {
.then((res) => { .then((res) => {
expect(res).toEqual(1) expect(res).toEqual(1)
}) })
// #END
}) })
test('finally', () => { test('finally', () => {
// #TEST Promise.finally
Promise.resolve(2) Promise.resolve(2)
.finally(() => { .finally(() => {
console.log("finally") console.log("finally")
...@@ -149,7 +156,7 @@ export function testPromise(): Result { ...@@ -149,7 +156,7 @@ export function testPromise(): Result {
}) })
Promise.resolve(2) Promise.resolve(2)
.finally((): number => { .finally(() : number => {
console.log("finally") console.log("finally")
return 88 return 88
}) })
...@@ -165,7 +172,7 @@ export function testPromise(): Result { ...@@ -165,7 +172,7 @@ export function testPromise(): Result {
}) })
console.log("start"); console.log("start");
Promise.reject(4).finally((): Promise<number> => { Promise.reject(4).finally(() : Promise<number> => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
setTimeout(() => { setTimeout(() => {
resolve(20) resolve(20)
...@@ -175,13 +182,15 @@ export function testPromise(): Result { ...@@ -175,13 +182,15 @@ export function testPromise(): Result {
.catch((res) => { .catch((res) => {
expect(res).toEqual(4) expect(res).toEqual(4)
}) })
// #END
}) })
test('resolve', () => { test('resolve', () => {
// #TEST Promise.resolve,Promise.resolve_1,Promise.resolve_2
// resolve array // resolve array
Promise.resolve([1,2,3]) Promise.resolve([1, 2, 3])
.then((res) => { .then((res) => {
expect(res).toEqual([1,2,3]) expect(res).toEqual([1, 2, 3])
}) })
// resolve string // resolve string
...@@ -204,13 +213,16 @@ export function testPromise(): Result { ...@@ -204,13 +213,16 @@ export function testPromise(): Result {
}) })
// 注:此处飘红是编译器误报,实际可以判等 // 注:此处飘红是编译器误报,实际可以判等
expect(original === cast).toEqual(true) expect(original === cast).toEqual(true)
// #END
}) })
test('reject', () => { test('reject', () => {
// #TEST Promise.reject
// reject array // reject array
Promise.reject([1,2,3]) Promise.reject([1, 2, 3])
.catch((err) => { .catch((err) => {
expect(err).toEqual([1,2,3]) expect(err).toEqual([1, 2, 3])
}) })
// reject error // reject error
...@@ -232,30 +244,31 @@ export function testPromise(): Result { ...@@ -232,30 +244,31 @@ export function testPromise(): Result {
expect((err as Promise<any>) === original).toEqual(true); expect((err as Promise<any>) === original).toEqual(true);
}) })
expect(original === cast).toEqual(false) expect(original === cast).toEqual(false)
// #END
}) })
test('all', () => { test('all', () => {
// #TEST Promise.all
let p0 = new Promise<string>((resolve, reject) => { let p0 = new Promise<string>((resolve, reject) => {
setTimeout(function() { setTimeout(function () {
resolve("1"); resolve("1");
}, 10); }, 10);
}) })
let p1 = new Promise<string>((resolve, reject) => { let p1 = new Promise<string>((resolve, reject) => {
setTimeout(function() { setTimeout(function () {
resolve("2"); resolve("2");
}, 10); }, 10);
}) })
let p2 = new Promise<string>((resolve, reject) => { let p2 = new Promise<string>((resolve, reject) => {
setTimeout(function() { setTimeout(function () {
resolve("3"); resolve("3");
}, 10); }, 10);
}) })
let p3 = new Promise<string>((resolve, reject) => { let p3 = new Promise<string>((resolve, reject) => {
setTimeout(function() { setTimeout(function () {
let error = new Error("p3 reject reason") let error = new Error("p3 reject reason")
reject(error); reject(error);
}, 10); }, 10);
...@@ -263,7 +276,6 @@ export function testPromise(): Result { ...@@ -263,7 +276,6 @@ export function testPromise(): Result {
// resolve // resolve
Promise.all([p0, p1, p2]) Promise.all([p0, p1, p2])
.then((res) => { .then((res) => {
console.log(res);
expect(res).toEqual(["1", "2", "3"]) expect(res).toEqual(["1", "2", "3"])
}) })
...@@ -274,29 +286,31 @@ export function testPromise(): Result { ...@@ -274,29 +286,31 @@ export function testPromise(): Result {
console.log(error); console.log(error);
expect((error as Error).message).toEqual("p3 reject reason"); expect((error as Error).message).toEqual("p3 reject reason");
}) })
// #END
}) })
test('allSettled', () => { test('allSettled', () => {
// #TEST Promise.allSettled
let p0 = new Promise<string | null>((resolve, reject) => { let p0 = new Promise<string | null>((resolve, reject) => {
setTimeout(function() { setTimeout(function () {
resolve("1"); resolve("1");
}, 10); }, 10);
}) })
let p1 = new Promise<string | null>((resolve, reject) => { let p1 = new Promise<string | null>((resolve, reject) => {
setTimeout(function() { setTimeout(function () {
resolve(null); resolve(null);
}, 10); }, 10);
}) })
let p2 = new Promise<string | null>((resolve, reject) => { let p2 = new Promise<string | null>((resolve, reject) => {
setTimeout(function() { setTimeout(function () {
reject(null) reject(null)
}, 10); }, 10);
}) })
let p3 = new Promise<string | null>((resolve, reject) => { let p3 = new Promise<string | null>((resolve, reject) => {
setTimeout(function() { setTimeout(function () {
let error = new Error("p3 reject reason") let error = new Error("p3 reject reason")
reject(error); reject(error);
}, 10); }, 10);
...@@ -306,34 +320,36 @@ export function testPromise(): Result { ...@@ -306,34 +320,36 @@ export function testPromise(): Result {
.then((res) => { .then((res) => {
console.log(res); console.log(res);
let statusArr : string[] = [] let statusArr : string[] = []
res.forEach((item, index: number) => { res.forEach((item, index : number) => {
statusArr.add(item.status) statusArr.add(item.status)
}) })
expect(statusArr).toEqual(["fulfilled", "fulfilled", "rejected", "rejected"]) expect(statusArr).toEqual(["fulfilled", "fulfilled", "rejected", "rejected"])
}) })
// #END
}) })
test('any', () => { test('any', () => {
// #TEST Promise.any
let p0 = new Promise<string | null>((resolve, reject) => { let p0 = new Promise<string | null>((resolve, reject) => {
setTimeout(function() { setTimeout(function () {
resolve("1"); resolve("1");
}, 10); }, 10);
}) })
let p1 = new Promise<string | null>((resolve, reject) => { let p1 = new Promise<string | null>((resolve, reject) => {
setTimeout(function() { setTimeout(function () {
resolve(null); resolve(null);
}, 15); }, 15);
}) })
let p2 = new Promise<string | null>((resolve, reject) => { let p2 = new Promise<string | null>((resolve, reject) => {
setTimeout(function() { setTimeout(function () {
reject(null) reject(null)
}, 10); }, 10);
}) })
let p3 = new Promise<string | null>((resolve, reject) => { let p3 = new Promise<string | null>((resolve, reject) => {
setTimeout(function() { setTimeout(function () {
reject("error"); reject("error");
}, 12); }, 12);
}) })
...@@ -349,7 +365,7 @@ export function testPromise(): Result { ...@@ -349,7 +365,7 @@ export function testPromise(): Result {
Promise.any([p2, p3]) Promise.any([p2, p3])
.then() .then()
.catch( .catch(
(error: any | null) => { (error : any | null) => {
console.log("test for promise.any error ==> ", error); console.log("test for promise.any error ==> ", error);
// #ifdef APP-IOS // #ifdef APP-IOS
expect((error as UTSPromiseAggregateError).name).toEqual("AggregateError"); expect((error as UTSPromiseAggregateError).name).toEqual("AggregateError");
...@@ -363,23 +379,25 @@ export function testPromise(): Result { ...@@ -363,23 +379,25 @@ export function testPromise(): Result {
expect((error as UTSPromiseAggregateError).errors).toEqual([null, "error"]); expect((error as UTSPromiseAggregateError).errors).toEqual([null, "error"]);
// #endif // #endif
}) })
// #END
}) })
test('race', () => { test('race', () => {
// #TEST Promise.race
let p0 = new Promise<string | null>((resolve, reject) => { let p0 = new Promise<string | null>((resolve, reject) => {
setTimeout(function() { setTimeout(function () {
resolve("fast"); resolve("fast");
}, 10); }, 10);
}) })
let p1 = new Promise<string | null>((resolve, reject) => { let p1 = new Promise<string | null>((resolve, reject) => {
setTimeout(function() { setTimeout(function () {
resolve("slow"); resolve("slow");
}, 15); }, 15);
}) })
let p2 = new Promise<string | null>((resolve, reject) => { let p2 = new Promise<string | null>((resolve, reject) => {
setTimeout(function() { setTimeout(function () {
reject(null) reject(null)
}, 12); }, 12);
}) })
...@@ -398,6 +416,7 @@ export function testPromise(): Result { ...@@ -398,6 +416,7 @@ export function testPromise(): Result {
console.log("test for race reject ===> ", error); console.log("test for race reject ===> ", error);
expect(error).toEqual(null); expect(error).toEqual(null);
}) })
// #END
}) })
}) })
} }
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册