-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathEditorBehaviorTest.ts
More file actions
157 lines (138 loc) · 6.58 KB
/
EditorBehaviorTest.ts
File metadata and controls
157 lines (138 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { Assertions, Waiter } from '@ephox/agar';
import { describe, it } from '@ephox/bedrock-client';
import { TinyAssertions, TinySelections } from '@ephox/mcagar';
import { PlatformDetection } from '@ephox/sand';
import { EditorEvent, Events, Editor as TinyMCEEditor } from 'tinymce';
import { getTinymce } from '../../../main/ts/TinyMCE';
import * as Loader from '../alien/Loader';
import { EventStore, VERSIONS } from '../alien/TestHelpers';
type SetContentEvent = EditorEvent<Events.EditorEventMap['SetContent']>;
describe('EditorBehaviourTest', () => {
const browser = PlatformDetection.detect().browser;
if (browser.isIE()) {
// INT-2278: This test currently times out in IE so we are skipping it
return;
}
const versionRegex = /6|7|8/;
const isEditor = (val: unknown): val is TinyMCEEditor => {
const tinymce = getTinymce(window);
if (!tinymce) {
return false;
}
return val instanceof tinymce.Editor;
};
const eventStore = EventStore();
VERSIONS.forEach((version) =>
Loader.withVersion(version, (render) => {
it('Assert structure of tinymce and tinymce-react events', async () => {
using ctx = await render({
onEditorChange: eventStore.createHandler('onEditorChange'),
onSetContent: eventStore.createHandler('onSetContent'),
});
// tinymce native event
// initial content is empty as editor does not have a value or initialValue
eventStore.each<SetContentEvent>('onSetContent', (events) => {
// note that this difference in behavior in 5-6 may be a bug, the team is investigating
Assertions.assertEq(
'First arg should be event from Tiny',
versionRegex.test(version) ? '<p><br data-mce-bogus="1"></p>' : '',
events[0].editorEvent.content
);
Assertions.assertEq('Second arg should be editor', true, isEditor(events[0].editor));
});
eventStore.clearState();
ctx.editor.setContent('<p>Initial Content</p>');
// tinymce native event
eventStore.each<SetContentEvent>('onSetContent', (events) => {
Assertions.assertEq('onSetContent should have been fired once', 1, events.length);
Assertions.assertEq(
'First arg should be event from Tiny',
'<p>Initial Content</p>',
events[0].editorEvent.content
);
Assertions.assertEq('Second arg should be editor', true, isEditor(events[0].editor));
});
// tinymce-react unique event
eventStore.each<string>('onEditorChange', (events) => {
Assertions.assertEq('First arg should be new content', '<p>Initial Content</p>', events[0].editorEvent);
Assertions.assertEq('Second arg should be editor', true, isEditor(events[0].editor));
});
eventStore.clearState();
});
it('onEditorChange should only fire when the editors content changes', async () => {
using ctx = await render({
onEditorChange: eventStore.createHandler('onEditorChange'),
});
ctx.editor.setContent('<p>Initial Content</p>');
ctx.editor.setContent('<p>Initial Content</p>'); // Repeat
eventStore.each('onEditorChange', (events) => {
Assertions.assertEq('onEditorChange should have been fired once', 1, events.length);
});
eventStore.clearState();
});
it('Should be able to register an event handler after initial render', async () => {
using ctx = await render({ initialValue: '<p>Initial Content</p>' });
await ctx.reRender({ onSetContent: eventStore.createHandler('onSetContent') });
TinyAssertions.assertContent(ctx.editor, '<p>Initial Content</p>');
await Waiter.pWait(0); // Wait for React's state updates to complete before setting new content
ctx.editor.setContent('<p>New Content</p>');
eventStore.each<SetContentEvent>('onSetContent', (events) => {
Assertions.assertEq(
'Should have bound handler, hence new content',
'<p>New Content</p>',
events[0].editorEvent.content
);
});
eventStore.clearState();
});
it('Providing a new event handler and re-rendering should unbind old handler and bind new handler', async () => {
using ctx = await render({ onSetContent: eventStore.createHandler('InitialHandler') });
eventStore.each<SetContentEvent>('InitialHandler', (events) => {
Assertions.assertEq(
'Initial content is empty as editor does not have a value or initialValue',
// note that this difference in behavior in 5-6 may be a bug, the team is investigating
versionRegex.test(version) ? '<p><br data-mce-bogus="1"></p>' : '',
events[0].editorEvent.content
);
});
eventStore.clearState();
ctx.editor.setContent('<p>Initial Content</p>');
await ctx.reRender({ onSetContent: eventStore.createHandler('NewHandler') });
await Waiter.pWait(0); // Wait for React's state updates to complete before setting new content
ctx.editor.setContent('<p>New Content</p>');
eventStore.each<SetContentEvent>('InitialHandler', (events) => {
Assertions.assertEq(
'Initial handler should have been unbound, hence initial content',
'<p>Initial Content</p>',
events[0].editorEvent.content
);
});
eventStore.each<SetContentEvent>('NewHandler', (events) => {
Assertions.assertEq(
'New handler should have been bound, hence new content',
'<p>New Content</p>',
events[0].editorEvent.content
);
});
eventStore.clearState();
});
it('INT-3226: onEditorChange is triggered only once after calling insertContent', async () => {
using ctx = await render({ onEditorChange: eventStore.createHandler('onEditorChange') });
const { editor } = ctx;
editor.setContent('<p>abc</p>');
await Waiter.pTryUntilPredicate('Editor content is set to correct value', () => ctx.editor.getContent() === '<p>abc</p>');
eventStore.clearState();
TinySelections.setSelection(editor, [ 0, 0 ], 1, [ 0, 0 ], 2);
editor.insertContent('e');
await Waiter.pTryUntilPredicate('Editor content is set to correct value', () => ctx.editor.getContent() === '<p>aec</p>');
eventStore.each<string>('onEditorChange', (events) => {
Assertions.assertEq(
'onEditorChange should have been triggered once',
1,
events.length
);
});
});
})
);
});