Skip to content

Commit

Permalink
test(code): optimize test case of core/lifecycle (#2874)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zardddddd60 authored Feb 27, 2022
1 parent 78b5caa commit f1766ec
Showing 1 changed file with 50 additions and 14 deletions.
64 changes: 50 additions & 14 deletions packages/core/src/__tests__/lifecycle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,56 @@ import { LifeCycle } from '../models'

test('create lifecycle', () => {
const handler1 = jest.fn()
const handler2 = jest.fn()
const c1 = new LifeCycle(handler1)
const c2 = new LifeCycle({
event: handler2,
})
const c3 = new LifeCycle({
yyy: null,
xxx: () => {},
})
c1.notify('event')
c2.notify('event')
c3.notify(null)
c3.notify('xxx', { type: 'xxx' })
c3.notify('ooo', { type: 'ooo' })
const lifecycle1 = new LifeCycle(handler1)
lifecycle1.notify('event1')
expect(handler1).toBeCalledTimes(1)
expect(handler1).toBeCalledWith(
{
type: 'event1',
payload: undefined,
},
undefined
)
lifecycle1.notify('event11', 'payload1')
expect(handler1).toBeCalledTimes(2)
expect(handler1).toBeCalledWith(
{
type: 'event11',
payload: 'payload1',
},
undefined
)
const context: any = {}
lifecycle1.notify('event12', 'payload11', context)
expect(handler1).toBeCalledTimes(3)
expect(handler1).toBeCalledWith(
{
type: 'event12',
payload: 'payload11',
},
context
)

const handler2 = jest.fn()
const lifecycle2 = new LifeCycle('event2', handler2)
lifecycle2.notify('event1')
expect(handler2).not.toBeCalled()
lifecycle2.notify('event2')
expect(handler2).toBeCalledTimes(1)

const handler31 = jest.fn()
const handler32 = jest.fn()
const lifecycle3 = new LifeCycle({
event31: handler31,
event32: handler32,
})
lifecycle3.notify('event3')
expect(handler31).not.toBeCalled()
expect(handler32).not.toBeCalled()
lifecycle3.notify('event31')
expect(handler31).toBeCalledTimes(1)
expect(handler32).not.toBeCalled()
lifecycle3.notify('event32')
expect(handler31).toBeCalledTimes(1)
expect(handler32).toBeCalledTimes(1)
})

0 comments on commit f1766ec

Please sign in to comment.