Skip to content

Commit

Permalink
chore(Link): tests converted to integration
Browse files Browse the repository at this point in the history
ISSUES CLOSED: #1285
  • Loading branch information
shani-terminus authored and benjamincharity committed Mar 26, 2019
1 parent 00ad6db commit 23298fc
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 6 deletions.
1 change: 1 addition & 0 deletions terminus-ui/link/src/link.component.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<a
class="c-link qa-link qa-link-internal"
[routerLink]="destination"
tabindex="{{ tabIndex }}"
*ngIf="!isExternal"
>
<ng-template *ngTemplateOutlet="contentTemplate"></ng-template>
Expand Down
81 changes: 75 additions & 6 deletions terminus-ui/link/src/link.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,92 @@
import { APP_BASE_HREF } from '@angular/common';
import { Component, ViewChild } from '@angular/core';
import { ComponentFixture, tick } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { RouterModule } from '@angular/router';

import { createComponent } from '@terminus/ngx-tools/testing';

import { TsLinkComponent } from './link.component';
import { TsLinkModule } from './link.module';

@Component({
template: `
<ts-link
[destination]="destination"
[isExternal]="isExternal"
[tabIndex]="tabIndex"
>
My Link Text
</ts-link>
`,
})
class TestHostComponent {
destination!: any;
isExternal!: boolean;
tabIndex!: number | undefined;

@ViewChild(TsLinkComponent)
linkComponent!: TsLinkComponent;
}


describe(`TsLinkComponent`, function() {
let component: TsLinkComponent;
let component: TestHostComponent;
let fixture: ComponentFixture<TestHostComponent>;
let link: HTMLElement;
let linkComponent: TsLinkComponent;

beforeEach(() => {
component = new TsLinkComponent();
fixture = createComponent(TestHostComponent, [{provide: APP_BASE_HREF, useValue: '/my/app'}], [TsLinkModule, RouterModule.forRoot([])]);
component = fixture.componentInstance;
linkComponent = component.linkComponent;
fixture.detectChanges();
});


it(`should exist`, () => {
expect(component).toBeTruthy();
test(`should exist`, () => {
expect(linkComponent).toBeTruthy();
});

describe(`isInternal`, () => {
test(`should default and retrieve`, () => {
link = fixture.debugElement.query(By.css('.c-link')).nativeElement as HTMLElement;
component.isExternal = false;
component.destination = ['/#'];

expect(link.classList).toContain('qa-link-internal');
});
});

describe(`isExternal`, () => {
it(`should set and retrieve`, () => {
test(`should set and retrieve`, () => {
component.destination = 'www.google.com';
component.isExternal = true;
expect(component.isExternal).toEqual(true);
fixture.detectChanges();
link = fixture.debugElement.query(By.css('.c-link')).nativeElement as HTMLElement;

expect(link.classList).toContain('qa-link-external');
expect(link.children[0].textContent).toContain('open_in_new');
});

test(`should throw error if invalid value is passed`, () => {
window.console.warn = jest.fn();
component.isExternal = 'foo' as any;
fixture.detectChanges();

expect(window.console.warn).toHaveBeenCalled();
});
});

describe(`tabIndex`, () => {
test(`should default to 0 and be set`, () => {
expect(link.tabIndex).toEqual(0);

component.tabIndex = 9;
fixture.detectChanges();
link = fixture.debugElement.query(By.css('.c-link')).nativeElement as HTMLElement;

expect(link.tabIndex).toEqual(9);
});
});
});

0 comments on commit 23298fc

Please sign in to comment.