2017-03-26 13:32:29 -07:00
|
|
|
/* tslint:disable:no-unused-variable */
|
|
|
|
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
|
|
|
import { By } from '@angular/platform-browser';
|
|
|
|
import { Component, DebugElement } from '@angular/core';
|
2017-04-24 21:19:40 +01:00
|
|
|
import { MdSnackBarModule, MdSnackBar } from '@angular/material';
|
|
|
|
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
|
2017-03-26 13:32:29 -07:00
|
|
|
|
|
|
|
import { CodeComponent } from './code.component';
|
|
|
|
import { CopierService } from 'app/shared//copier.service';
|
|
|
|
import { Logger } from 'app/shared/logger.service';
|
|
|
|
import { PrettyPrinter } from './pretty-printer.service';
|
|
|
|
|
|
|
|
const oneLineCode = 'const foo = "bar";';
|
|
|
|
|
2017-04-27 22:57:34 -07:00
|
|
|
const smallMultiLineCode = `
|
2017-03-26 13:32:29 -07:00
|
|
|
<hero-details>
|
|
|
|
<h2>Bah Dah Bing</h2>
|
|
|
|
<hero-team>
|
|
|
|
<h3>NYC Team</h3>
|
|
|
|
</hero-team>
|
|
|
|
</hero-details>`;
|
|
|
|
|
2017-04-27 22:57:34 -07:00
|
|
|
const bigMultiLineCode = smallMultiLineCode + smallMultiLineCode + smallMultiLineCode;
|
|
|
|
|
2017-03-26 13:32:29 -07:00
|
|
|
describe('CodeComponent', () => {
|
|
|
|
let codeComponentDe: DebugElement;
|
|
|
|
let codeComponent: CodeComponent;
|
|
|
|
let hostComponent: HostComponent;
|
|
|
|
let fixture: ComponentFixture<HostComponent>;
|
|
|
|
|
|
|
|
|
|
|
|
// WARNING: Chance of cross-test pollution
|
|
|
|
// CodeComponent injects PrettyPrintService
|
|
|
|
// Once PrettyPrintService runs once _anywhere_, its ctor loads `prettify.js`
|
|
|
|
// which sets `window['prettyPrintOne']`
|
|
|
|
// That global survives these tests unless
|
|
|
|
// we take strict measures to wipe it out in the `afterAll`
|
|
|
|
// and make sure THAT runs after the tests by making component creation async
|
|
|
|
afterAll(() => {
|
|
|
|
delete window['prettyPrint'];
|
|
|
|
delete window['prettyPrintOne'];
|
|
|
|
});
|
|
|
|
|
2017-05-15 10:44:06 -07:00
|
|
|
beforeEach(() => {
|
2017-03-26 13:32:29 -07:00
|
|
|
TestBed.configureTestingModule({
|
2017-04-24 21:19:40 +01:00
|
|
|
imports: [ MdSnackBarModule, NoopAnimationsModule ],
|
2017-03-26 13:32:29 -07:00
|
|
|
declarations: [ CodeComponent, HostComponent ],
|
|
|
|
providers: [
|
|
|
|
PrettyPrinter,
|
2017-04-24 21:19:40 +01:00
|
|
|
CopierService,
|
2017-03-26 13:32:29 -07:00
|
|
|
{provide: Logger, useClass: TestLogger }
|
|
|
|
]
|
2017-05-15 10:44:06 -07:00
|
|
|
});
|
|
|
|
});
|
2017-03-26 13:32:29 -07:00
|
|
|
|
|
|
|
// Must be async because
|
|
|
|
// CodeComponent creates PrettyPrintService which async loads `prettify.js`.
|
|
|
|
// If not async, `afterAll` finishes before tests do!
|
|
|
|
beforeEach(async(() => {
|
|
|
|
fixture = TestBed.createComponent(HostComponent);
|
|
|
|
hostComponent = fixture.componentInstance;
|
|
|
|
codeComponentDe = fixture.debugElement.children[0];
|
|
|
|
codeComponent = codeComponentDe.componentInstance;
|
|
|
|
fixture.detectChanges();
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should create CodeComponent', () => {
|
|
|
|
expect(codeComponentDe.name).toBe('aio-code', 'selector');
|
|
|
|
expect(codeComponent).toBeTruthy('CodeComponent');
|
|
|
|
});
|
|
|
|
|
2017-04-24 21:19:40 +01:00
|
|
|
describe('pretty printing', () => {
|
|
|
|
it('should format a one-line code sample', () => {
|
|
|
|
// 'pln' spans are a tell-tale for syntax highlighing
|
|
|
|
const spans = codeComponentDe.nativeElement.querySelectorAll('span.pln');
|
|
|
|
expect(spans.length).toBeGreaterThan(0, 'formatted spans');
|
|
|
|
});
|
|
|
|
|
2017-04-27 22:57:34 -07:00
|
|
|
function hasLineNumbers() {
|
|
|
|
// presence of `<li>`s are a tell-tale for line numbers
|
|
|
|
return 0 < codeComponentDe.nativeElement.querySelectorAll('li').length;
|
|
|
|
}
|
|
|
|
|
2017-04-24 21:19:40 +01:00
|
|
|
it('should format a one-line code sample without linenums by default', () => {
|
2017-04-27 22:57:34 -07:00
|
|
|
expect(hasLineNumbers()).toBe(false);
|
2017-04-24 21:19:40 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should add line numbers to one-line code sample when linenums set true', () => {
|
|
|
|
hostComponent.linenums = 'true';
|
|
|
|
fixture.detectChanges();
|
2017-04-27 22:57:34 -07:00
|
|
|
expect(hasLineNumbers()).toBe(true);
|
2017-04-24 21:19:40 +01:00
|
|
|
});
|
|
|
|
|
2017-04-27 22:57:34 -07:00
|
|
|
it('should format a small multi-line code without linenums by default', () => {
|
|
|
|
hostComponent.code = smallMultiLineCode;
|
2017-04-24 21:19:40 +01:00
|
|
|
fixture.detectChanges();
|
2017-04-27 22:57:34 -07:00
|
|
|
expect(hasLineNumbers()).toBe(false);
|
|
|
|
});
|
2017-04-24 21:19:40 +01:00
|
|
|
|
2017-04-27 22:57:34 -07:00
|
|
|
it('should add line numbers to a big multi-line code by default', () => {
|
|
|
|
hostComponent.code = bigMultiLineCode;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(hasLineNumbers()).toBe(true);
|
2017-04-24 21:19:40 +01:00
|
|
|
});
|
|
|
|
|
2017-04-27 22:57:34 -07:00
|
|
|
it('should format big multi-line code without linenums when linenums set false', () => {
|
2017-04-24 21:19:40 +01:00
|
|
|
hostComponent.linenums = false;
|
2017-04-27 22:57:34 -07:00
|
|
|
hostComponent.code = bigMultiLineCode;
|
2017-04-24 21:19:40 +01:00
|
|
|
fixture.detectChanges();
|
2017-04-27 22:57:34 -07:00
|
|
|
expect(hasLineNumbers()).toBe(false);
|
2017-04-24 21:19:40 +01:00
|
|
|
});
|
2017-03-26 13:32:29 -07:00
|
|
|
});
|
|
|
|
|
2017-04-24 21:19:40 +01:00
|
|
|
describe('whitespace handling', () => {
|
|
|
|
it('should remove common indentation from the code before rendering', () => {
|
|
|
|
hostComponent.linenums = false;
|
|
|
|
hostComponent.code = ' abc\n let x = text.split(\'\\n\');\n ghi\n\n jkl\n';
|
|
|
|
fixture.detectChanges();
|
|
|
|
const codeContent = codeComponentDe.nativeElement.querySelector('code').innerText;
|
|
|
|
expect(codeContent).toEqual('abc\n let x = text.split(\'\\n\');\nghi\n\njkl');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should trim whitespace from the code before rendering', () => {
|
|
|
|
hostComponent.linenums = false;
|
2017-04-27 22:57:34 -07:00
|
|
|
hostComponent.code = '\n\n\n' + smallMultiLineCode + '\n\n\n';
|
2017-04-24 21:19:40 +01:00
|
|
|
fixture.detectChanges();
|
|
|
|
const codeContent = codeComponentDe.nativeElement.querySelector('code').innerText;
|
|
|
|
expect(codeContent).toEqual(codeContent.trim());
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should trim whitespace from code before computing whether to format linenums', () => {
|
|
|
|
hostComponent.code = '\n\n\n' + hostComponent.code + '\n\n\n';
|
|
|
|
fixture.detectChanges();
|
|
|
|
// `<li>`s are a tell-tale for line numbers
|
|
|
|
const lis = codeComponentDe.nativeElement.querySelectorAll('li');
|
|
|
|
expect(lis.length).toBe(0, 'should be no linenums');
|
|
|
|
});
|
2017-03-26 13:32:29 -07:00
|
|
|
});
|
|
|
|
|
2017-04-24 21:19:40 +01:00
|
|
|
describe('error message', () => {
|
2017-04-27 22:57:34 -07:00
|
|
|
|
|
|
|
function getErrorMessage() {
|
|
|
|
const missing: HTMLElement = codeComponentDe.nativeElement.querySelector('.code-missing');
|
|
|
|
return missing ? missing.innerText : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should not display "code-missing" class when there is some code', () => {
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(getErrorMessage()).toBeNull('should not have element with "code-missing" class');
|
|
|
|
});
|
|
|
|
|
2017-04-24 21:19:40 +01:00
|
|
|
it('should display error message when there is no code (after trimming)', () => {
|
|
|
|
hostComponent.code = ' \n ';
|
|
|
|
fixture.detectChanges();
|
2017-04-27 22:57:34 -07:00
|
|
|
expect(getErrorMessage()).toContain('missing');
|
2017-04-24 21:19:40 +01:00
|
|
|
});
|
|
|
|
|
2017-04-27 22:57:34 -07:00
|
|
|
it('should show path and region in missing-code error message', () => {
|
|
|
|
hostComponent.code = ' \n ';
|
|
|
|
hostComponent.path = 'fizz/buzz/foo.html';
|
|
|
|
hostComponent.region = 'something';
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(getErrorMessage()).toMatch(/for[\s\S]fizz\/buzz\/foo\.html#something$/);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should show path only in missing-code error message when no region', () => {
|
|
|
|
hostComponent.code = ' \n ';
|
|
|
|
hostComponent.path = 'fizz/buzz/foo.html';
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(getErrorMessage()).toMatch(/for[\s\S]fizz\/buzz\/foo\.html$/);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should show simple missing-code error message when no path/region', () => {
|
|
|
|
hostComponent.code = ' \n ';
|
2017-04-24 21:19:40 +01:00
|
|
|
fixture.detectChanges();
|
2017-04-27 22:57:34 -07:00
|
|
|
expect(getErrorMessage()).toMatch(/missing.$/);
|
2017-04-24 21:19:40 +01:00
|
|
|
});
|
2017-04-01 21:16:22 +01:00
|
|
|
});
|
|
|
|
|
2017-04-24 21:19:40 +01:00
|
|
|
describe('copy button', () => {
|
2017-05-02 11:57:26 +01:00
|
|
|
|
2017-05-15 18:03:56 -07:00
|
|
|
function getButton() {
|
|
|
|
const btnDe = fixture.debugElement.query(By.css('button'));
|
|
|
|
return btnDe ? btnDe.nativeElement : null;
|
|
|
|
}
|
|
|
|
|
2017-05-02 11:57:26 +01:00
|
|
|
it('should be hidden if the `hideCopy` input is true', () => {
|
|
|
|
hostComponent.hideCopy = true;
|
|
|
|
fixture.detectChanges();
|
2017-05-15 18:03:56 -07:00
|
|
|
expect(getButton()).toBe(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should have title', () => {
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(getButton().title).toBe('Copy code snippet');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should have no aria-label by default', () => {
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(getButton().getAttribute('aria-label')).toBe('');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should have aria-label explaining what is being copied when title passed in', () => {
|
|
|
|
hostComponent.title = 'a/b/c/foo.ts';
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(getButton().getAttribute('aria-label')).toContain(hostComponent.title);
|
2017-05-02 11:57:26 +01:00
|
|
|
});
|
|
|
|
|
2017-04-24 21:19:40 +01:00
|
|
|
it('should call copier service when clicked', () => {
|
|
|
|
const copierService: CopierService = TestBed.get(CopierService);
|
|
|
|
const spy = spyOn(copierService, 'copyText');
|
|
|
|
expect(spy.calls.count()).toBe(0, 'before click');
|
2017-05-15 18:03:56 -07:00
|
|
|
getButton().click();
|
2017-04-24 21:19:40 +01:00
|
|
|
expect(spy.calls.count()).toBe(1, 'after click');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should copy code text when clicked', () => {
|
|
|
|
const copierService: CopierService = TestBed.get(CopierService);
|
|
|
|
const spy = spyOn(copierService, 'copyText');
|
2017-05-15 18:03:56 -07:00
|
|
|
getButton().click();
|
2017-04-24 21:19:40 +01:00
|
|
|
expect(spy.calls.argsFor(0)[0]).toEqual(oneLineCode, 'after click');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should display a message when copy succeeds', () => {
|
|
|
|
const snackBar: MdSnackBar = TestBed.get(MdSnackBar);
|
|
|
|
const copierService: CopierService = TestBed.get(CopierService);
|
|
|
|
spyOn(snackBar, 'open');
|
|
|
|
spyOn(copierService, 'copyText').and.returnValue(true);
|
2017-05-15 18:03:56 -07:00
|
|
|
getButton().click();
|
2017-04-24 21:19:40 +01:00
|
|
|
expect(snackBar.open).toHaveBeenCalledWith('Code Copied', '', { duration: 800 });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should display an error when copy fails', () => {
|
|
|
|
const snackBar: MdSnackBar = TestBed.get(MdSnackBar);
|
|
|
|
const copierService: CopierService = TestBed.get(CopierService);
|
|
|
|
spyOn(snackBar, 'open');
|
|
|
|
spyOn(copierService, 'copyText').and.returnValue(false);
|
2017-05-15 18:03:56 -07:00
|
|
|
getButton().click();
|
2017-04-24 21:19:40 +01:00
|
|
|
expect(snackBar.open).toHaveBeenCalledWith('Copy failed. Please try again!', '', { duration: 800 });
|
|
|
|
});
|
2017-04-01 17:57:47 -07:00
|
|
|
});
|
2017-03-26 13:32:29 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
//// Test helpers ////
|
|
|
|
// tslint:disable:member-ordering
|
|
|
|
@Component({
|
|
|
|
selector: 'aio-host-comp',
|
|
|
|
template: `
|
2017-04-27 22:57:34 -07:00
|
|
|
<aio-code md-no-ink [code]="code" [language]="language"
|
2017-05-15 18:03:56 -07:00
|
|
|
[linenums]="linenums" [path]="path" [region]="region"
|
|
|
|
[hideCopy]="hideCopy" [title]="title"></aio-code>
|
2017-03-26 13:32:29 -07:00
|
|
|
`
|
|
|
|
})
|
|
|
|
class HostComponent {
|
|
|
|
code = oneLineCode;
|
2017-05-15 18:03:56 -07:00
|
|
|
hideCopy: boolean;
|
2017-03-26 13:32:29 -07:00
|
|
|
language: string;
|
|
|
|
linenums: boolean | number | string;
|
2017-04-27 22:57:34 -07:00
|
|
|
path: string;
|
|
|
|
region: string;
|
2017-05-15 18:03:56 -07:00
|
|
|
title: string;
|
2017-03-26 13:32:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
class TestLogger {
|
|
|
|
log = jasmine.createSpy('log');
|
|
|
|
error = jasmine.createSpy('error');
|
|
|
|
}
|