-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathFormControlTest.ts
More file actions
132 lines (111 loc) · 4.06 KB
/
FormControlTest.ts
File metadata and controls
132 lines (111 loc) · 4.06 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
import '../alien/InitTestEnvironment';
import { Component } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { FormControl, ReactiveFormsModule } from '@angular/forms';
import { By } from '@angular/platform-browser';
import { Assertions, Chain, Log, Pipeline } from '@ephox/agar';
import { UnitTest } from '@ephox/bedrock-client';
import { VersionLoader } from '@tinymce/miniature';
import { Editor } from 'tinymce';
import { EditorComponent, EditorModule } from '../../../main/ts/public_api';
import { Version } from '../../../main/ts/editor/editor.component';
UnitTest.asynctest('FormControlTest', (success, failure) => {
@Component({
template: `<editor [formControl]="control"></editor>`,
})
class EditorWithFormControl {
public control = new FormControl();
}
interface TestContext {
testComponent: EditorWithFormControl;
fixture: ComponentFixture<EditorWithFormControl>;
editor: Editor;
}
const cSetupEditorWithFormControl = Chain.async<void, TestContext>((_, next) => {
TestBed.configureTestingModule({
imports: [ EditorModule, ReactiveFormsModule ],
declarations: [ EditorWithFormControl ]
}).compileComponents();
const fixture = TestBed.createComponent(EditorWithFormControl);
fixture.detectChanges();
const editorDebugElement = fixture.debugElement.query(By.directive(EditorComponent));
const editorComponent = editorDebugElement.componentInstance;
editorComponent.onInit.subscribe(() => {
editorComponent.editor.on('SkinLoaded', () => {
setTimeout(() => {
next({
fixture,
testComponent: fixture.componentInstance,
editor: editorComponent.editor
});
}, 0);
});
});
});
const cTeardown = Chain.op(() => {
TestBed.resetTestingModule();
});
const setFormValueAndReset = Chain.op((context: TestContext) => {
Assertions.assertEq('Expect editor to have no initial value', '', context.editor.getContent());
context.testComponent.control.setValue('<p>Some Value</p>');
context.fixture.detectChanges();
Assertions.assertEq(
'Expect editor to have a value',
'<p>Some Value</p>',
context.editor.getContent()
);
context.testComponent.control.reset();
context.fixture.detectChanges();
Assertions.assertEq('Expect editor to be empty after reset', '', context.editor.getContent());
});
/** Sets content and set cursor at the end, just like when a real user types */
const doFakeType = (str: string) =>
Chain.op((context: TestContext) => {
context.testComponent.control.patchValue(`${str}`);
context.fixture.detectChanges();
const rng = context.editor.dom.createRng();
const firstChild = context.editor.getBody().firstChild?.firstChild as Text;
rng.setStart(firstChild, str.length);
rng.setEnd(firstChild, str.length);
context.editor.selection.setRng(rng);
context.fixture.detectChanges();
});
const checkCursor = Chain.op((context: TestContext) => {
const cursorStartPosition = context.editor.selection.getRng().startOffset;
const cursorEndPosition = context.editor.selection.getRng().endOffset;
const content = context.editor.getContent();
const strippedContent = content.replace(/<\/?[^>]+(>|$)/g, '');
Assertions.assertEq(
'Expect cursor start position to be at the end of the content',
cursorStartPosition,
strippedContent.length
);
Assertions.assertEq(
'Expect cursor end position to be at the end of the content',
cursorEndPosition,
strippedContent.length
);
});
const sTestVersion = (version: Version) =>
VersionLoader.sWithVersion(
version,
Log.chainsAsStep('', 'FormControl interaction ', [
cSetupEditorWithFormControl,
setFormValueAndReset,
doFakeType('test'),
checkCursor,
cTeardown,
])
);
Pipeline.async(
{},
[
sTestVersion('4'),
sTestVersion('5'),
sTestVersion('6'),
sTestVersion('7'),
],
success,
failure
);
});