Skip to content

Commit

Permalink
feat: delete emit logic on emitter
Browse files Browse the repository at this point in the history
  • Loading branch information
cuixiaorui committed Sep 16, 2020
1 parent 98b0702 commit d488100
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 34 deletions.
7 changes: 0 additions & 7 deletions src/use/emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,6 @@ export function useEmitter() {
emitter.off(type, handler[wrapper])
}

function emit(type, ...args) {
emitter.emit(type, {
value: args
})
}

function once(type, handler) {
const handleOn = (...args) => {
handler && handler(...args)
Expand All @@ -71,7 +65,6 @@ export function useEmitter() {
broadcast,
dispatch,
off,
emit,
once
}
}
Expand Down
40 changes: 13 additions & 27 deletions test/unit/tests/use/emitter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,42 +143,28 @@ describe('useEmitter', () => {
expect(count).toBe(1)
})

it('使用 emit 可以发送事件', () => {
const handleEmit = jest.fn()
const Comp = {
it('once ', () => {
const handleFoo = jest.fn()
const Foo = {
template: '<div></div>',
setup() {
const { on, emit } = useEmitter()

on('emit', handleEmit)

emit('emit', 1, 2)
emit('emit', { test: 'emit' })
const { dispatch } = useEmitter()
dispatch('foo')
dispatch('foo')
}
}

mount(Comp)

expect(handleEmit).toHaveBeenNthCalledWith(1, 1, 2)
expect(handleEmit).toHaveBeenNthCalledWith(2, { test: 'emit' })
})

it('once ', () => {
const handleEmit = jest.fn()
const Comp = {
template: '<div></div>',
components: {
Foo
},
template: '<div><Foo></Foo></div>',
setup() {
const { once, emit } = useEmitter()

once('emit', handleEmit)

emit('emit', 1)
emit('emit', 2)
const { once } = useEmitter()
once('foo', handleFoo)
}
}

mount(Comp)

expect(handleEmit).toBeCalledTimes(1)
expect(handleFoo).toHaveBeenCalledTimes(1)
})
})

1 comment on commit d488100

@vercel
Copy link

@vercel vercel bot commented on d488100 Sep 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.