Files
liquidjs/src/util/async.spec.ts
T
a0ea372764 docs: fix some spelling (#708)
* spelling: according

Signed-off-by: Josh Soref <[email protected]>

* spelling: asynchronously

Signed-off-by: Josh Soref <[email protected]>

* spelling: background

Signed-off-by: Josh Soref <[email protected]>

* spelling: camel

Signed-off-by: Josh Soref <[email protected]>

* spelling: cannot

Signed-off-by: Josh Soref <[email protected]>

* spelling: case-sensitive

Signed-off-by: Josh Soref <[email protected]>

* spelling: comparison

Signed-off-by: Josh Soref <[email protected]>

* spelling: demos

Signed-off-by: Josh Soref <[email protected]>

* spelling: forloop

Signed-off-by: Josh Soref <[email protected]>

* spelling: formatters

Signed-off-by: Josh Soref <[email protected]>

* spelling: github

Signed-off-by: Josh Soref <[email protected]>

* spelling: guidelines

Signed-off-by: Josh Soref <[email protected]>

* spelling: hashes

Signed-off-by: Josh Soref <[email protected]>

* spelling: https

Signed-off-by: Josh Soref <[email protected]>

* spelling: javascript

Signed-off-by: Josh Soref <[email protected]>

* spelling: keep

Signed-off-by: Josh Soref <[email protected]>

* spelling: natural

Signed-off-by: Josh Soref <[email protected]>

* spelling: neither

Signed-off-by: Josh Soref <[email protected]>

* spelling: no longer

Signed-off-by: Josh Soref <[email protected]>

* spelling: nonexistent

Signed-off-by: Josh Soref <[email protected]>

* spelling: output

Signed-off-by: Josh Soref <[email protected]>

* spelling: polymorphism

Signed-off-by: Josh Soref <[email protected]>

* spelling: precache

Signed-off-by: Josh Soref <[email protected]>

* spelling: programmatically

Signed-off-by: Josh Soref <[email protected]>

* spelling: punctuation

Signed-off-by: Josh Soref <[email protected]>

* spelling: registration

Signed-off-by: Josh Soref <[email protected]>

* spelling: rendered

Signed-off-by: Josh Soref <[email protected]>

* spelling: synchronously

Signed-off-by: Josh Soref <[email protected]>

* spelling: thrown

Signed-off-by: Josh Soref <[email protected]>

* spelling: trimmed

Signed-off-by: Josh Soref <[email protected]>

* spelling: unbalanced

Signed-off-by: Josh Soref <[email protected]>

* chore: use example.com

* chore: fix reference for sidebar.registration

---------

Signed-off-by: Josh Soref <[email protected]>
Co-authored-by: Harttle <[email protected]>
2024-06-17 17:19:46 +08:00

113 lines
3.1 KiB
TypeScript

import { toPromise, toValueSync } from './async'
describe('utils/async', () => {
describe('#toPromise()', function () {
it('should return a promise', async () => {
function * foo () {
return 'foo'
}
const result = await toPromise(foo())
expect(result).toBe('foo')
})
it('should support iterable with single return statement', async () => {
function * foo () {
return 'foo'
}
const result = await toPromise(foo())
expect(result).toBe('foo')
})
it('should support promise', async () => {
function foo () {
return Promise.resolve('foo')
}
const result = await toPromise(foo())
expect(result).toBe('foo')
})
it('should resolve dependency', async () => {
function * foo (): Generator<Generator<string>> {
return yield bar()
}
function * bar (): Generator<string> {
return 'bar'
}
const result = await toPromise(foo())
expect(result).toBe('bar')
})
it('should support promise dependency', async () => {
function * foo (): Generator<Promise<string>> {
return yield Promise.resolve('foo')
}
const result = await toPromise(foo())
expect(result).toBe('foo')
})
it('should reject Promise if dependency throws synchronously', done => {
function * foo (): Generator<Generator<never>> {
return yield bar()
}
function * bar (): Generator<never> {
throw new Error('bar')
}
toPromise(foo()).catch(err => {
expect(err.message).toBe('bar')
done()
return 0 as any
})
})
it('should resume promise after catch', async () => {
function * foo () {
let ret = ''
try {
yield bar()
} catch (e) {
ret += 'bar'
}
ret += 'foo'
return ret
}
function * bar (): Generator<never> {
throw new Error('bar')
}
const ret = await toPromise(foo())
expect(ret).toBe('barfoo')
})
})
describe('#toValueSync()', function () {
it('should throw Error if dependency throws synchronously', () => {
function * foo (): Generator<Generator<never>> {
return yield bar()
}
function * bar (): Generator<never> {
throw new Error('bar')
}
expect(() => toValueSync(foo())).toThrow('bar')
})
it('should resume yield after catch', () => {
function * foo (): Generator<unknown, never, never> {
try {
yield bar()
} catch (e) {}
return yield 'foo'
}
function * bar (): Generator<never> {
throw new Error('bar')
}
expect(toValueSync(foo())).toBe('foo')
})
it('should resume return after catch', () => {
function * foo (): Generator<Generator<never>, string> {
try {
yield bar()
} catch (e) {}
return 'foo'
}
function * bar (): Generator<never> {
throw new Error('bar')
}
expect(toValueSync(foo())).toBe('foo')
})
it('should return non iterator value as it is', () => {
expect(toValueSync('foo')).toBe('foo')
})
})
})