ray-template/__test__/basic/isPromise.spec.ts
XiaoDaiGua-Ray 403fdd589a feat!: rebuild Ray Template as a monorepo
Share workspace packages between Ray Admin and the interactive docs.

BREAKING CHANGE: v6 is a new major line. v5 stays on legacy/v5.
2026-11-04 14:01:28 +08:00

41 lines
1.1 KiB
TypeScript

import { isPromise } from '../../apps/admin/src/utils/basic'
describe('isPromise', () => {
it('should return true if the value is a Promise', () => {
const promise = Promise.resolve('')
expect(isPromise(promise)).toBe(true)
})
it('should return false if the value is not a Promise', () => {
const notPromise = 'not a Promise'
expect(isPromise(notPromise)).toBe(false)
})
it('should return false if the value is a class', () => {
class MyClass {}
expect(isPromise(MyClass)).toBe(false)
})
it('should return false if the value is a function', () => {
const fn = () => {}
expect(isPromise(fn)).toBe(false)
})
it('should distinguishes an async function from its result', () => {
const asyncFn = async () => {}
expect(isPromise(asyncFn)).toBe(false)
expect(isPromise(asyncFn())).toBe(true)
})
})
it('recognizes thenables without accepting non-callable then properties', () => {
expect(isPromise({ then: vi.fn() })).toBe(true)
expect(isPromise({ then: true })).toBe(false)
expect(isPromise(null)).toBe(false)
})