Skip to content

Commit

Permalink
test(core): add ellipsis directive test
Browse files Browse the repository at this point in the history
  • Loading branch information
thediaval committed Nov 8, 2019
1 parent a22d7a0 commit 8ab1693
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { AfterContentInit, ChangeDetectorRef, Directive, ElementRef, HostBinding, Input } from '@angular/core';
import { AfterViewInit, ChangeDetectorRef, Directive, ElementRef, HostBinding, Input } from '@angular/core';

@Directive({
selector: '[abpEllipsis]',
})
export class EllipsisDirective implements AfterContentInit {
export class EllipsisDirective implements AfterViewInit {
@Input('abpEllipsis')
width: string;

Expand Down Expand Up @@ -31,14 +31,8 @@ export class EllipsisDirective implements AfterContentInit {

constructor(private cdRef: ChangeDetectorRef, private elRef: ElementRef) {}

ngAfterContentInit() {
setTimeout(() => {
const title = this.title;
this.title = title || (this.elRef.nativeElement as HTMLElement).innerText;

if (this.title !== title) {
this.cdRef.detectChanges();
}
}, 0);
ngAfterViewInit() {
this.title = this.title || (this.elRef.nativeElement as HTMLElement).innerText;
this.cdRef.detectChanges();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { createDirectiveFactory, SpectatorDirective } from '@ngneat/spectator/jest';
import { EllipsisDirective } from '../directives/ellipsis.directive';

describe('EllipsisDirective', () => {
let spectator: SpectatorDirective<EllipsisDirective>;
let directive: EllipsisDirective;
let el: HTMLDivElement;
const createDirective = createDirectiveFactory({
directive: EllipsisDirective,
});

beforeEach(() => {
spectator = createDirective(
'<div [abpEllipsis]="width" [abpEllipsisEnabled]="true" [title]="title">test content</div>',
{
hostProps: {
title: 'test title',
width: '100px',
},
},
);
directive = spectator.directive;
el = spectator.query('div');
});

test('should be created', () => {
expect(directive).toBeTruthy();
});

test('should have 100px ellipsis width', () => {
expect(directive.width).toBe('100px');
});

test('should be enabled if abpEllipsisEnabled input is true', () => {
expect(directive.enabled).toBe(true);
});

test('should have given title', () => {
expect(directive.title).toBe('test title');
});

test('should have element innerText as title if not specified', () => {
spectator.setHostInput({ title: undefined });
expect(directive.title).toBe(el.innerText);
});

test('should add abp-ellipsis-inline class to element if width is given', () => {
expect(el).toHaveClass('abp-ellipsis-inline');
});

test('should add abp-ellipsis class to element if width is not given', () => {
spectator.setHostInput({ width: undefined });
expect(el).toHaveClass('abp-ellipsis');
});
});

0 comments on commit 8ab1693

Please sign in to comment.