Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fire component's events #390

Open
wants to merge 2 commits into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 80 additions & 1 deletion __tests__/Promised.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ const timeout = setTimeout
const tick = () => new Promise((resolve) => timeout(resolve, 0))

describe('Promised', () => {
function factory(propsData: any = undefined, slots: any = undefined) {
function factory(
propsData: any = undefined,
slots: any = undefined,
listeners: any = undefined
) {
const [promise, resolve, reject] = fakePromise<any>()
const wrapper = mount(Promised, {
propsData: { promise, pendingDelay: 0, ...propsData },
attrs: listeners,
slots: {
pending: (oldData) => h('span', 'pending: ' + oldData),
default: (data) => h('span', {}, data),
Expand Down Expand Up @@ -190,6 +195,80 @@ describe('Promised', () => {
})
})

describe("component's events", () => {
beforeEach(() => {
jest.useFakeTimers('modern')
jest.spyOn(global, 'setTimeout')
jest.spyOn(global, 'clearTimeout')
})

afterEach(() => {
// @ts-expect-error: mocked
setTimeout.mockClear()
// @ts-expect-error: mocked
clearTimeout.mockClear()
})

afterAll(() => {
jest.useRealTimers()
})

it("fires 'resolved' event", async () => {
const onResolved = jest.fn()

const { resolve } = factory(undefined, undefined, { onResolved })

resolve('fire')

await tick()

expect(onResolved).toHaveBeenCalledWith('fire')
})

it("fires 'rejected' event", async () => {
const onResolved = jest.fn()
const onRejected = jest.fn()

const { reject } = factory(undefined, undefined, {
onResolved,
onRejected,
})

reject(new Error('fire'))

await tick()

expect(onRejected).toHaveBeenCalledWith(new Error('fire'))

expect(onResolved).not.toHaveBeenCalled()
})

it("fires 'pending' event on pending delay time elapsed", async () => {
const onResolved = jest.fn()
const onRejected = jest.fn()
const onPending = jest.fn()

factory({ pendingDelay: 100 }, undefined, {
onPending,
onResolved,
onRejected,
})

expect(onResolved).not.toHaveBeenCalled()
expect(onRejected).not.toHaveBeenCalled()
expect(onPending).not.toHaveBeenCalled()

jest.runAllTimers()

await tick()

expect(onPending).toHaveBeenCalledWith(undefined)

expect(onResolved).not.toHaveBeenCalled()
expect(onRejected).not.toHaveBeenCalled()
})
})

mockWarn()

it('warns on missing slot', async () => {
Expand Down
15 changes: 13 additions & 2 deletions src/Promised.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { watch } from 'vue'
import {
defineComponent,
isVue3,
Expand All @@ -24,13 +25,23 @@ export const PromisedImpl = /*#__PURE__*/ defineComponent({
default: 200,
},
},

setup(props, { slots }) {
emits: ['resolved', 'rejected', 'pending'],
setup(props, { slots, emit, attrs }) {
const propsAsRefs = toRefs(props)
const promiseState = reactive(
usePromise(propsAsRefs.promise, propsAsRefs.pendingDelay)
)

watch(promiseState, (promiseState) =>
promiseState.isRejected
? emit('rejected', promiseState.error)
: !promiseState.isPending
? emit('resolved', promiseState.data)
: promiseState.isDelayElapsed
? emit('pending', promiseState.data)
: null
)

return () => {
if ('combined' in slots) {
return slots.combined!(promiseState)
Expand Down