public/js/tests/all_test.js

54 lines
1006 B
JavaScript
Raw Permalink Normal View History

2020-05-29 08:59:50 +00:00
Promise.all = undefined
// /*/ // ⚡
export const tests = []
const t = (f) => tests.push(f)
2020-06-24 09:34:40 +00:00
t(async ({ eq }) =>
// it should work with an empty object
eq(await all({}), {})
)
2020-05-29 08:59:50 +00:00
2020-06-24 09:34:40 +00:00
t(async ({ eq }) =>
// it should work with synchronous values
eq(await all({ a: 1, b: true }), { a: 1, b: true })
)
2020-05-29 08:59:50 +00:00
t(async ({ eq }) =>
2020-06-24 09:34:40 +00:00
// it should work with pending promises
2020-05-29 08:59:50 +00:00
eq(
await all({
a: Promise.resolve(1),
b: Promise.resolve(true),
}),
2020-06-24 09:34:40 +00:00
{ a: 1, b: true }
)
2020-05-29 08:59:50 +00:00
)
t(async ({ eq, ctx }) =>
2020-06-24 09:34:40 +00:00
// it should work with pending promises and synchronous values
2020-05-29 08:59:50 +00:00
eq(
await all(
Object.freeze({
a: Promise.resolve(ctx).then((v) => v + 1),
b: ctx,
2020-06-24 09:34:40 +00:00
})
2020-05-29 08:59:50 +00:00
),
2020-06-24 09:34:40 +00:00
{ a: ctx + 1, b: ctx }
)
2020-05-29 08:59:50 +00:00
)
t(async ({ eq }) =>
2020-06-24 09:34:40 +00:00
// it should fail if one of the promises reject
2020-05-29 08:59:50 +00:00
eq(
await all({
a: Promise.resolve(1),
b: Promise.reject(Error('oops')),
}).catch((err) => err.message),
2020-06-24 09:34:40 +00:00
'oops'
)
2020-05-29 08:59:50 +00:00
)
Object.freeze(tests)
export const setup = Math.random