|
| 1 | +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; |
| 2 | +import { Component, DebugElement } from '@angular/core'; |
| 3 | +import { By } from '@angular/platform-browser'; |
| 4 | +import * as prettyHTMLLog from 'pretty-html-log'; |
| 5 | +import { highlight, THEMES } from 'pretty-html-log'; |
| 6 | + |
| 7 | +import { logNgHTML } from './logNgHTML'; |
| 8 | +import * as fixturePrettier from './prettiers/fixture/pretty-fixture'; |
| 9 | +import * as debugElementPrettier from './prettiers/debugElement/pretty-debugElement'; |
| 10 | +import * as htmlElementPrettier from './prettiers/htmlElement/pretty-htmlelement'; |
| 11 | + |
| 12 | +@Component({ |
| 13 | + selector: 'lib-mock-component', |
| 14 | + template: ` |
| 15 | + <ul> |
| 16 | + <li>Foo</li> |
| 17 | + <li>Bar</li> |
| 18 | + <li>Baz</li> |
| 19 | + </ul> |
| 20 | + ` |
| 21 | +}) |
| 22 | +export class MockComponent {} |
| 23 | + |
| 24 | +describe('LogNgHTML', () => { |
| 25 | + let component: MockComponent; |
| 26 | + let fixture: ComponentFixture<MockComponent>; |
| 27 | + const theme = THEMES.DRACULA; |
| 28 | + |
| 29 | + beforeEach(async(() => { |
| 30 | + TestBed.configureTestingModule({ |
| 31 | + declarations: [MockComponent] |
| 32 | + }).compileComponents(); |
| 33 | + })); |
| 34 | + |
| 35 | + beforeEach(() => { |
| 36 | + fixture = TestBed.createComponent(MockComponent); |
| 37 | + component = fixture.componentInstance; |
| 38 | + fixture.detectChanges(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should call prettyPrintFixture incase we provide a component fixture', () => { |
| 42 | + spyOn(fixturePrettier, 'prettyPrintFixture'); |
| 43 | + logNgHTML<MockComponent>(fixture, theme); |
| 44 | + expect(fixturePrettier.prettyPrintFixture).toHaveBeenCalledWith( |
| 45 | + fixture, |
| 46 | + theme |
| 47 | + ); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should call prettyPrintDebugElements in case we provide an array of DebugElements', () => { |
| 51 | + spyOn(console, 'log'); |
| 52 | + spyOn(debugElementPrettier, 'prettyPrintDebugElements'); |
| 53 | + const debugElements = fixture.debugElement.queryAll(By.css('li')); |
| 54 | + logNgHTML(debugElements, theme); |
| 55 | + expect(debugElementPrettier.prettyPrintDebugElements).toHaveBeenCalledWith( |
| 56 | + debugElements, |
| 57 | + theme |
| 58 | + ); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should call prettyPrintHTMLElements in case we provide an array of HTMLelements', () => { |
| 62 | + spyOn(console, 'log'); |
| 63 | + spyOn(htmlElementPrettier, 'prettyPrintHtmlElements'); |
| 64 | + const htmlElements = fixture.debugElement |
| 65 | + .queryAll(By.css('li')) |
| 66 | + .map((debugElement: DebugElement) => debugElement.nativeElement); |
| 67 | + logNgHTML(htmlElements, theme); |
| 68 | + expect(htmlElementPrettier.prettyPrintHtmlElements).toHaveBeenCalledWith( |
| 69 | + htmlElements, |
| 70 | + theme |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should call prettyPrintDebugElement in case we provide a debug element', () => { |
| 75 | + spyOn(debugElementPrettier, 'prettyPrintDebugElement'); |
| 76 | + const debugElement = fixture.debugElement.queryAll(By.css('li'))[0]; |
| 77 | + logNgHTML(debugElement, theme); |
| 78 | + expect(debugElementPrettier.prettyPrintDebugElement).toHaveBeenCalledWith( |
| 79 | + debugElement, |
| 80 | + theme |
| 81 | + ); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should call prettyPrintHTMLElement in case we provide a HTMLelement', () => { |
| 85 | + spyOn(htmlElementPrettier, 'prettyPrintHtmlElement'); |
| 86 | + const htmlElement = fixture.debugElement.queryAll(By.css('li'))[0] |
| 87 | + .nativeElement; |
| 88 | + logNgHTML(htmlElement, theme); |
| 89 | + expect(htmlElementPrettier.prettyPrintHtmlElement).toHaveBeenCalledWith( |
| 90 | + htmlElement, |
| 91 | + theme |
| 92 | + ); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should print a warning if we pass in an unknown type', () => { |
| 96 | + spyOn(console, 'log'); |
| 97 | + spyOn(prettyHTMLLog, 'highlight'); |
| 98 | + const htmlString = '<h1>Foo</h1>'; |
| 99 | + logNgHTML(htmlString, theme); |
| 100 | + expect(prettyHTMLLog.highlight).toHaveBeenCalledWith(htmlString, theme); |
| 101 | + }); |
| 102 | +}); |
0 commit comments