Some Value
'); - context.fixture.detectChanges(); - - Assertions.assertEq( - 'Expect editor to have a value', - 'Some Value
', - context.editor.getContent() - ); - - context.testComponent.control.reset(); - context.fixture.detectChanges(); - - Assertions.assertEq( - 'Expect editor to be empty after reset', - '', - context.editor.getContent() - ); - }), - cTeardown - ]) - ); + const setFormValueAndReset = Chain.op((context: TestContext) => { + Assertions.assertEq('Expect editor to have no initial value', '', context.editor.getContent()); + + context.testComponent.control.setValue('Some Value
'); + context.fixture.detectChanges(); + + Assertions.assertEq( + 'Expect editor to have a value', + 'Some Value
', + 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); - Pipeline.async({}, [ - sTestVersion('4'), - sTestVersion('5'), - sTestVersion('6'), - sTestVersion('7') - ], success, failure); -}); \ No newline at end of file + 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 + ); +});