forked from jd-opensource/micro-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request jd-opensource#442 from LinFeng1997/lifecycles_even…
…t_test test: lifecycles event
- Loading branch information
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
|
||
import dispatchLifecyclesEvent, { dispatchCustomEventToMicroApp } from '../../interact/lifecycles_event' | ||
import microApp from '../../micro_app' | ||
|
||
describe('LifeCycles Event', () => { | ||
describe('dispatchCustomEventToMicroApp', () => { | ||
test('使用 dispatchCustomEventToMicroApp 方法发送事件,window 可以接收到事件', () => { | ||
const callback = jest.fn() | ||
window.addEventListener('event-app', callback) | ||
|
||
dispatchCustomEventToMicroApp('event', 'app') | ||
|
||
expect(callback).toBeCalled() | ||
}) | ||
}) | ||
|
||
describe('dispatchLifecyclesEvent', () => { | ||
const appName = 'app' | ||
beforeAll(() => { | ||
console.error = jest.fn() | ||
}) | ||
|
||
test('element 为空,报错', () => { | ||
// @ts-ignore | ||
dispatchLifecyclesEvent(undefined, appName, 'mount') | ||
|
||
expect(console.error).toBeCalledWith(`[micro-app] app ${appName}: element does not exist in lifecycle mount`) | ||
}) | ||
|
||
test('发送 mount 事件,接收成功', () => { | ||
const container = document.createElement('div') | ||
container.addEventListener('mount', (e) => { | ||
const { name, container: detailContainer } = (e as CustomEvent).detail | ||
expect(name).toEqual(appName) | ||
expect(detailContainer).toEqual(container) | ||
expect(e.target).toEqual(container) | ||
expect(e.currentTarget).toEqual(container) | ||
}) | ||
dispatchLifecyclesEvent(container, appName, 'mount') | ||
}) | ||
|
||
test('发送 created 事件,主应用监听了这个事件可以被触发', () => { | ||
const container = document.createElement('div') | ||
const onCreated = jest.fn() | ||
|
||
microApp.lifeCycles = { created: onCreated } | ||
dispatchLifecyclesEvent(container, appName, 'created') | ||
|
||
expect(onCreated).toBeCalled() | ||
microApp.lifeCycles = undefined | ||
}) | ||
}) | ||
}) |