-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f838900
commit 40a9504
Showing
10 changed files
with
198 additions
and
789 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
89 changes: 89 additions & 0 deletions
89
packages/roosterjs-editor-core/lib/test/coreApi/attachDomEventTest.ts
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,89 @@ | ||
import attachDomEvent from '../../coreAPI/attachDomEvent'; | ||
import createEditorCore from '../../editor/createEditorCore'; | ||
import Editor from '../../editor/Editor'; | ||
import EditorCore from '../../editor/EditorCore'; | ||
import EditorPlugin from '../../editor/EditorPlugin'; | ||
import { PluginEvent, PluginEventType, PluginKeyboardEvent } from 'roosterjs-editor-types'; | ||
|
||
class MockPlugin implements EditorPlugin { | ||
lastEvent: PluginEvent = null; | ||
initialize(editor: Editor) {} | ||
dispose() {} | ||
onPluginEvent(e: PluginEvent) { | ||
this.lastEvent = e; | ||
} | ||
} | ||
|
||
describe('attachDomEvent', () => { | ||
let div: HTMLDivElement; | ||
let core: EditorCore; | ||
beforeEach(() => { | ||
div = document.createElement('div'); | ||
document.body.appendChild(div); | ||
core = createEditorCore(div, {}); | ||
(<any>core).plugins = core.plugins.filter( | ||
plugin => plugin != core.undo && plugin != core.corePlugin | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
document.body.removeChild(div); | ||
core = null; | ||
div = null; | ||
}); | ||
|
||
it('Check return value to be a function', () => { | ||
let disposer = attachDomEvent(core, 'click'); | ||
expect(typeof disposer).toBe('function'); | ||
|
||
disposer(); | ||
}); | ||
|
||
it('Check event is fired', () => { | ||
let called = false; | ||
let disposer = attachDomEvent(core, 'keydown', null, () => { | ||
called = true; | ||
}); | ||
|
||
let event = document.createEvent('KeyboardEvent'); | ||
event.initEvent('keydown'); | ||
div.dispatchEvent(event); | ||
|
||
expect(called).toBe(true); | ||
disposer(); | ||
}); | ||
|
||
it('Check event dispatched to plugin', () => { | ||
let mockPlugin = new MockPlugin(); | ||
core.plugins.push(mockPlugin); | ||
expect(mockPlugin.lastEvent).toBeNull(); | ||
|
||
let disposer = attachDomEvent(core, 'keydown', PluginEventType.KeyDown); | ||
|
||
let event = document.createEvent('KeyboardEvent'); | ||
event.initEvent('keydown'); | ||
div.dispatchEvent(event); | ||
|
||
expect(mockPlugin.lastEvent).not.toBeNull(); | ||
expect(mockPlugin.lastEvent.eventType).toBe(PluginEventType.KeyDown); | ||
expect((<PluginKeyboardEvent>mockPlugin.lastEvent).rawEvent).toBe(event); | ||
disposer(); | ||
}); | ||
|
||
it('Check event not dispatched to plugin after dispose', () => { | ||
let mockPlugin = new MockPlugin(); | ||
core.plugins.push(mockPlugin); | ||
expect(mockPlugin.lastEvent).toBeNull(); | ||
|
||
let disposer = attachDomEvent(core, 'keydown', PluginEventType.KeyDown); | ||
|
||
let event = document.createEvent('KeyboardEvent'); | ||
event.initEvent('keydown'); | ||
|
||
disposer(); | ||
|
||
div.dispatchEvent(event); | ||
|
||
expect(mockPlugin.lastEvent).toBeNull(); | ||
}); | ||
}); |
50 changes: 50 additions & 0 deletions
50
packages/roosterjs-editor-core/lib/test/coreApi/hasFocusTest.ts
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,50 @@ | ||
import createEditorCore from '../../editor/createEditorCore'; | ||
import EditorCore from '../../editor/EditorCore'; | ||
import hasFocus from '../../coreAPI/hasFocus'; | ||
|
||
describe('hasFocus', () => { | ||
let div: HTMLDivElement; | ||
let core: EditorCore; | ||
beforeEach(() => { | ||
div = document.createElement('div'); | ||
div.contentEditable = 'true'; | ||
document.body.appendChild(div); | ||
core = createEditorCore(div, {}); | ||
(<any>core).plugins = core.plugins.filter( | ||
plugin => plugin != core.undo && plugin != core.corePlugin | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
document.body.removeChild(div); | ||
core = null; | ||
div = null; | ||
}); | ||
|
||
it('Check editor has focus after set focus', () => { | ||
core.api.focus(core); | ||
let result = hasFocus(core); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('Check editor does not have focus after set focus away', () => { | ||
let button = document.createElement('button'); | ||
document.body.appendChild(button); | ||
core.api.focus(core); | ||
expect(hasFocus(core)).toBe(true); | ||
|
||
button.focus(); | ||
expect(hasFocus(core)).toBe(false); | ||
|
||
document.body.removeChild(button); | ||
}); | ||
|
||
it('Check editor has focus after select', () => { | ||
core.contentDiv.innerHTML = '<span>test</span>'; | ||
expect(hasFocus(core)).toBe(false); | ||
|
||
let span = core.contentDiv.querySelector('span'); | ||
core.api.select(core, span.firstChild, 1); | ||
expect(hasFocus(core)).toBe(true); | ||
}); | ||
}); |
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
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
Oops, something went wrong.