-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(decorate): add decorate unit test
- Loading branch information
1 parent
1e220fa
commit 28ec1c5
Showing
6 changed files
with
150 additions
and
21 deletions.
There are no files selected for viewing
54 changes: 38 additions & 16 deletions
54
packages/src/components/editable/editable.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,41 @@ | ||
// import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; | ||
// import { BasicEditableComponent, configureBasicEditableTestingModule } from '../../testing'; | ||
import { ComponentFixture, fakeAsync, flush, TestBed, waitForAsync } from '@angular/core/testing'; | ||
import { By } from '@angular/platform-browser'; | ||
import { BasicEditableComponent, AdvancedEditableComponent, TestingLeafComponent, configureBasicEditableTestingModule } from '../../testing'; | ||
|
||
// describe('Editable Component', () => { | ||
// let component: BasicEditableComponent; | ||
// let fixture: ComponentFixture<BasicEditableComponent>; | ||
describe('Editable Component', () => { | ||
let component: AdvancedEditableComponent; | ||
let fixture: ComponentFixture<AdvancedEditableComponent>; | ||
|
||
// beforeEach(waitForAsync(() => { | ||
// configureBasicEditableTestingModule([BasicEditableComponent]); | ||
// fixture = TestBed.createComponent(BasicEditableComponent); | ||
// component = fixture.componentInstance; | ||
// fixture.detectChanges(); | ||
// })); | ||
beforeEach(fakeAsync(() => { | ||
configureBasicEditableTestingModule([BasicEditableComponent, AdvancedEditableComponent, TestingLeafComponent], [TestingLeafComponent]); | ||
fixture = TestBed.createComponent(AdvancedEditableComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
flush(); | ||
fixture.detectChanges(); | ||
})); | ||
|
||
// it('should create editable component', () => { | ||
// expect(component).toBeTruthy(); | ||
// expect(component.editableComponent).toBeTruthy(); | ||
// }); | ||
// }); | ||
it('decorate', fakeAsync(() => { | ||
let testingLeaf; | ||
testingLeaf= fixture.debugElement.query(By.css('.testing-leaf')); | ||
expect(testingLeaf).toBeFalsy(); | ||
|
||
const keywords1 = 'text'; | ||
component.generateDcoreate(keywords1); | ||
fixture.detectChanges(); | ||
flush(); | ||
fixture.detectChanges(); | ||
testingLeaf = fixture.debugElement.query(By.css('.testing-leaf')).nativeElement; | ||
expect(testingLeaf).toBeTruthy(); | ||
expect(testingLeaf.textContent).toEqual(keywords1); | ||
|
||
const keywords2 = 'text'; | ||
component.generateDcoreate(keywords2); | ||
fixture.detectChanges(); | ||
flush(); | ||
fixture.detectChanges(); | ||
testingLeaf = fixture.debugElement.query(By.css('.testing-leaf')).nativeElement;; | ||
expect(testingLeaf).toBeTruthy(); | ||
expect(testingLeaf.textContent).toEqual(keywords2); | ||
})); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Component, OnInit, ViewChild } from "@angular/core"; | ||
import { createEditor, NodeEntry, Text } from "slate"; | ||
import { SlateEditableComponent } from "../components/editable/editable.component"; | ||
import { withAngular } from "../plugins/with-angular"; | ||
import { createDefaultDocument } from "./create-document"; | ||
import { TestingLeafComponent } from "./leaf.component"; | ||
|
||
@Component({ | ||
selector: 'basic-editable', | ||
template: ` | ||
<slate-editable | ||
[editor]="editor" | ||
[ngModel]="value" | ||
[decorate]="decorate" | ||
[renderLeaf]="renderLeaf" | ||
></slate-editable> | ||
` | ||
}) | ||
export class AdvancedEditableComponent implements OnInit { | ||
editor = withAngular(createEditor()); | ||
|
||
value = createDefaultDocument(); | ||
|
||
decorate = (nodeEntry: NodeEntry) => []; | ||
|
||
@ViewChild(SlateEditableComponent, { static: true }) | ||
editableComponent: SlateEditableComponent; | ||
|
||
generateDcoreate(keywords: string) { | ||
this.decorate = ([node, path]) => { | ||
const ranges = []; | ||
|
||
if (keywords && Text.isText(node)) { | ||
const { text } = node; | ||
const parts = text.split(keywords); | ||
let offset = 0; | ||
|
||
parts.forEach((part, i) => { | ||
if (i !== 0) { | ||
ranges.push({ | ||
anchor: { path, offset: offset - keywords.length }, | ||
focus: { path, offset }, | ||
highlight: true, | ||
}); | ||
} | ||
|
||
offset = offset + part.length + keywords.length; | ||
}); | ||
} | ||
|
||
return ranges; | ||
}; | ||
} | ||
|
||
renderLeaf = (text: Text) => { | ||
if (text['highlight']) { | ||
return TestingLeafComponent; | ||
} | ||
return null; | ||
} | ||
|
||
ngOnInit() { | ||
} | ||
|
||
constructor() { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Component, ElementRef, Renderer2, ChangeDetectorRef } from '@angular/core'; | ||
import { BaseLeafComponent } from 'slate-angular'; | ||
|
||
@Component({ | ||
selector: 'span[testingLeaf]', | ||
template: ` | ||
<span slateString [context]="context" [viewContext]="viewContext" | ||
><span></span | ||
></span> | ||
`, | ||
host: { | ||
'class': 'testing-leaf' | ||
} | ||
}) | ||
export class TestingLeafComponent extends BaseLeafComponent { | ||
constructor( | ||
public elementRef: ElementRef, | ||
public cdr: ChangeDetectorRef, | ||
private renderer: Renderer2 | ||
) { | ||
super(elementRef, cdr); | ||
} | ||
|
||
onContextChange() { | ||
super.onContextChange(); | ||
this.changeStyle(); | ||
} | ||
|
||
changeStyle() { | ||
const backgroundColor = this.leaf['highlight'] ? '#ffeeba' : null; | ||
this.renderer.setStyle(this.nativeElement, 'backgroundColor', backgroundColor); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters