Skip to content

Commit 4346033

Browse files
committed
test(lognghtml): test logNgHtml logic
1 parent a0090ae commit 4346033

2 files changed

Lines changed: 108 additions & 9 deletions

File tree

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
});

projects/pretty-html-log/src/lib/logNgHTML.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,38 @@ export type NgHTMLElement<T> =
1616
| DebugElement
1717
| DebugElement[]
1818
| HTMLElement
19-
| HTMLElement[];
19+
| HTMLElement[]
20+
| string;
2021

2122
export const logNgHTML = <T>(
2223
ngHTMLElement: NgHTMLElement<T>,
2324
theme?: Theme
2425
) => {
2526
if (ngHTMLElement instanceof ComponentFixture) {
26-
console.log('Da');
2727
prettyPrintFixture<T>(ngHTMLElement, theme);
2828
return;
2929
}
3030

3131
if (Array.isArray(ngHTMLElement)) {
3232
if (ngHTMLElement[0] instanceof DebugElement) {
33-
prettyPrintDebugElements(ngHTMLElement as DebugElement[]);
33+
prettyPrintDebugElements(ngHTMLElement as DebugElement[], theme);
3434
return;
3535
}
3636

3737
if (ngHTMLElement[0] instanceof HTMLElement) {
38-
prettyPrintHtmlElements(ngHTMLElement as HTMLElement[]);
38+
prettyPrintHtmlElements(ngHTMLElement as HTMLElement[], theme);
3939
return;
4040
}
4141
}
4242

4343
if (ngHTMLElement instanceof DebugElement) {
44-
prettyPrintDebugElement(ngHTMLElement);
44+
prettyPrintDebugElement(ngHTMLElement, theme);
4545
return;
4646
}
4747

4848
if (ngHTMLElement instanceof HTMLElement) {
49-
prettyPrintHtmlElement(ngHTMLElement);
49+
prettyPrintHtmlElement(ngHTMLElement, theme);
5050
return;
5151
}
52-
console.log(
53-
'@angular-extension/pretty-html-log: Unknown type - try to format anyway'
54-
);
5552
console.log(highlight(ngHTMLElement as any, theme));
5653
};

0 commit comments

Comments
 (0)