-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathTestHooks.ts
More file actions
105 lines (93 loc) · 3.5 KB
/
TestHooks.ts
File metadata and controls
105 lines (93 loc) · 3.5 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
import { after, before, beforeEach, context } from '@ephox/bedrock-client';
import { ComponentFixture, TestBed, TestModuleMetadata } from '@angular/core/testing';
import { Type } from '@angular/core';
import { EditorComponent, Version } from '../../../main/ts/editor/editor.component';
import { firstValueFrom, map, switchMap, tap } from 'rxjs';
import { By } from '@angular/platform-browser';
import { Optional, Singleton } from '@ephox/katamari';
import { VersionLoader } from '@tinymce/miniature';
import { deleteTinymce, throwTimeout } from './TestHelpers';
import { FormsModule, ReactiveFormsModule, NgModel } from '@angular/forms';
import type { Editor } from 'tinymce';
export const fixtureHook = <T = unknown>(component: Type<T>, moduleDef: TestModuleMetadata) => {
before(async () => {
await TestBed.configureTestingModule(moduleDef).compileComponents();
});
return () => TestBed.createComponent(component);
};
export const tinymceVersionHook = (version: Version) => {
before(async () => {
await VersionLoader.pLoadVersion(version);
});
after(() => {
deleteTinymce();
});
};
export interface EditorFixture<T> extends ComponentFixture<T> {
editorComponent: EditorComponent;
editor: Editor;
ngModel: Optional<NgModel>;
}
export type CreateEditorFixture<T> = (
props?: Partial<
Omit<
EditorComponent,
`${'on' | 'ng' | 'register' | 'set' | 'write'}${string}` | 'createElement' | 'initialise' | 'editor'
>
>
) => Promise<EditorFixture<T>>;
export const editorHook = <T = unknown>(component: Type<T>, moduleDef: TestModuleMetadata = {
imports: [ component, EditorComponent, FormsModule, ReactiveFormsModule ],
}): CreateEditorFixture<T> => {
const createFixture = fixtureHook(component, moduleDef);
const editorFixture = Singleton.value<EditorFixture<T>>();
beforeEach(() => editorFixture.clear());
return async (props = {}) => {
if (editorFixture.isSet()) {
return editorFixture.get().getOrDie();
}
const fixture = createFixture();
const editorComponent =
fixture.componentInstance instanceof EditorComponent
? fixture.componentInstance
: Optional.from(fixture.debugElement.query(By.directive(EditorComponent)))
.map((v): EditorComponent => v.componentInstance)
.getOrDie('EditorComponent instance not found');
for (const [ key, value ] of Object.entries({ ...props })) {
(editorComponent as any)[key] = value;
}
fixture.detectChanges();
return firstValueFrom(
editorComponent.onInit.pipe(
throwTimeout(10000, `Timed out waiting for editor to load`),
switchMap(
({ editor }) =>
new Promise<Editor>((resolve) => {
if (editor.initialized) {
resolve(editor);
}
editor.once('SkinLoaded', () => resolve(editor));
})
),
map(
(editor): EditorFixture<T> =>
Object.assign(fixture, {
editorComponent,
editor,
ngModel: Optional.from(fixture.debugElement.query(By.directive(EditorComponent))).bind((debugEl) =>
Optional.from(debugEl.injector.get<NgModel>(NgModel, undefined, { optional: true }))
),
})
),
tap(editorFixture.set)
)
);
};
};
export const eachVersionContext = (versions: Version[], fn: (version: Version) => void) =>
versions.forEach((version) =>
context(`With version ${version}`, () => {
tinymceVersionHook(version);
fn(version);
})
);