Skip to content

Commit

Permalink
chore: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
pubuzhixing8 committed Jun 6, 2023
1 parent 59f0e6c commit 05962f5
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 9 deletions.
80 changes: 80 additions & 0 deletions packages/src/components/string/string.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ComponentFixture, fakeAsync, flush, TestBed } from '@angular/core/testing';
import { AdvancedEditableComponent, TestingLeafComponent, configureBasicEditableTestingModule, dispatchFakeEvent } from '../../testing';
import { Editor, Transforms } from 'slate';

describe('Default String Render', () => {
let component: AdvancedEditableComponent;
Expand Down Expand Up @@ -49,4 +50,83 @@ describe('Default String Render', () => {
expect(editableText.getAttribute('data-slate-string')).toEqual('true');
expect(editableText.textContent).toEqual(text);
}));

it('should correctly render line break empty string', fakeAsync(() => {
const text = ``;
component.value = [
{
type: 'paragraph',
children: [{ text }],
key: 'KD'
}
];
fixture.detectChanges();
flush();
fixture.detectChanges();
const paragraphElement = document.querySelector('[data-slate-node="element"]');
const editableText = paragraphElement.querySelector('[editable-text]');
expect(editableText).toBeTruthy();
expect(editableText.childNodes.length).toEqual(2);
expect(editableText.firstChild.textContent).toEqual(`\uFEFF`);
expect(editableText.lastElementChild.tagName).toEqual(`BR`);
}));

it('should correctly render text when text transform text from empty string to non-empty string', fakeAsync(() => {
const text = ``;
component.value = [
{
type: 'paragraph',
children: [{ text }],
key: 'KD'
}
];
fixture.detectChanges();
flush();
fixture.detectChanges();
const paragraphElement = document.querySelector('[data-slate-node="element"]');
const editableText = paragraphElement.querySelector('[editable-text]');
expect(editableText).toBeTruthy();
expect(editableText.childNodes.length).toEqual(2);
expect(editableText.firstChild.textContent).toEqual(`\uFEFF`);
expect(editableText.lastElementChild.tagName).toEqual(`BR`);

Transforms.select(component.editor, Editor.end(component.editor, [0]));

const newText = 'Kevin Durant';
Transforms.insertText(component.editor, newText);
fixture.detectChanges();
flush();
fixture.detectChanges();
expect(editableText.childNodes.length).toEqual(1);
expect(editableText.firstChild.textContent).toEqual(newText);
}));

it('should correctly render text when text transform text from non-empty string empty string', fakeAsync(() => {
const text = `Kevin Durant`;
component.value = [
{
type: 'paragraph',
children: [{ text }],
key: 'KD'
}
];
fixture.detectChanges();
flush();
fixture.detectChanges();
const paragraphElement = document.querySelector('[data-slate-node="element"]');
const editableText = paragraphElement.querySelector('[editable-text]');
expect(editableText).toBeTruthy();
expect(editableText.childNodes.length).toEqual(1);
expect(editableText.firstChild.textContent).toEqual(text);

Transforms.select(component.editor, [0]);

Transforms.delete(component.editor);
fixture.detectChanges();
flush();
fixture.detectChanges();
expect(editableText.childNodes.length).toEqual(2);
expect(editableText.firstChild.textContent).toEqual(`\uFEFF`);
expect(editableText.lastElementChild.tagName).toEqual(`BR`);
}));
});
3 changes: 0 additions & 3 deletions packages/src/components/string/template.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,3 @@
<ng-template #emptyTextTemplate let-context="context" let-viewContext="viewContext">
<span editable-text data-slate-zero-width="z" data-slate-length="0">{{ '\uFEFF' }}</span>
</ng-template>
<ng-template #lineBreakEmptyStringTemplate let-context="context" let-viewContext="viewContext">
<span editable-text data-slate-zero-width="n" attr.data-slate-length="{{ context.elementStringLength }}">{{ '\uFEFF' }}<br /></span>
</ng-template>
6 changes: 0 additions & 6 deletions packages/src/components/string/template.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,4 @@ export class SlateStringTemplateComponent {

@ViewChild('emptyTextTemplate', { read: TemplateRef, static: true })
emptyTextTemplate: TemplateRef<any>;

@ViewChild('lineBreakEmptyStringTemplate', {
read: TemplateRef,
static: true
})
lineBreakEmptyStringTemplate: TemplateRef<any>;
}

0 comments on commit 05962f5

Please sign in to comment.