Skip to content

Commit

Permalink
improve HTML reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
vrozaev committed Oct 4, 2017
1 parent e3822ae commit 5dff892
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 21 deletions.
25 changes: 23 additions & 2 deletions src/lib/reporters/Html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export default class Html extends Reporter implements HtmlProperties {

document: Document;

location: Location;

protected _reportContainer: Element;

// Div element to hold buttons above the summary table
Expand Down Expand Up @@ -57,6 +59,7 @@ export default class Html extends Reporter implements HtmlProperties {
constructor(executor: Browser, options: HtmlOptions = {}) {
super(executor, options);
this.document = options.document || window.document;
this.location = options.location || window.location;
this._fragment = this.document.createDocumentFragment();
}

Expand Down Expand Up @@ -323,7 +326,7 @@ export default class Html extends Reporter implements HtmlProperties {
this._suiteCount++;

cellNode.className = 'title';
cellNode.appendChild(document.createTextNode(suite.name));
cellNode.appendChild(this.createLinkNode(suite));
rowNode.className = 'suite';
rowNode.appendChild(cellNode);
this._reportNode.appendChild(rowNode);
Expand Down Expand Up @@ -466,7 +469,7 @@ export default class Html extends Reporter implements HtmlProperties {
cellNode.className += ' indent' + this._indentLevel;
}

cellNode.appendChild(document.createTextNode(test.name));
cellNode.appendChild(this.createLinkNode(test));
rowNode.appendChild(cellNode);

cellNode = document.createElement('td');
Expand Down Expand Up @@ -499,10 +502,28 @@ export default class Html extends Reporter implements HtmlProperties {

this._reportNode.appendChild(rowNode);
}

private createLinkNode(obj: Suite | Test) {
const document = this.document;
const location = this.location;
const a = document.createElement('a');
let search = location.search;

if (search) {
search = search.slice(1).split('&').filter(el => {
return !el.startsWith('grep');
}).join('&');
}

a.href = location.origin + location.pathname + `?${search}&grep=${encodeURIComponent(obj.id)}`;
a.appendChild(document.createTextNode(obj.name));
return a;
}
}

export interface HtmlProperties extends ReporterProperties {
document: Document;
location: Location;
}

export type HtmlOptions = Partial<HtmlProperties>;
Expand Down
4 changes: 4 additions & 0 deletions tests/support/mocking.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ declare namespace mocking {
export interface DocCreator {
(): Document;
}

export interface LocCreator {
(): Location;
}
}
56 changes: 37 additions & 19 deletions tests/unit/lib/reporters/Html.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import _Html from 'src/lib/reporters/Html';
import { createMockBrowserExecutor } from '../../../support/unit/mocks';
import { createLocation } from './support/mocks';

const mockRequire = <mocking.MockRequire>intern.getPlugin('mockRequire');
const createDocument = intern.getPlugin<mocking.DocCreator>('createDocument');
Expand All @@ -9,6 +10,7 @@ const mockExecutor = createMockBrowserExecutor();
let Html: typeof _Html;
let removeMocks: () => void;
let doc: Document;
let location: Location;
let reporter: _Html;

registerSuite('intern/lib/reporters/Html', {
Expand All @@ -27,7 +29,8 @@ registerSuite('intern/lib/reporters/Html', {

beforeEach() {
doc = createDocument();
reporter = new Html(mockExecutor, { document: doc });
location = <Location>createLocation();
reporter = new Html(mockExecutor, { document: doc, location: location });
},

tests: {
Expand All @@ -46,22 +49,28 @@ registerSuite('intern/lib/reporters/Html', {
);
},

suiteStart() {
const suite: any = {
parent: {},
tests: [],
name: 'foo',
id: 'foo'
};
suiteStart: {
'root suite'() {
const suite: any = {
parent: {},
tests: [],
name: 'foo',
id: 'foo'
};

// Need to run runStart to setup doc for suiteStart
reporter.runStart();
reporter.suiteStart(suite);
assert.equal(
doc.body.innerHTML,
'',
'suiteStart should not have altered the document'
);
// Need to run runStart to setup doc for suiteStart
reporter.runStart();
reporter.suiteStart(suite);
assert.equal(
doc.body.innerHTML,
'',
'suiteStart should not have altered the document'
);
},

'regular suite'() {
// TODO
}
},

suiteEnd: {
Expand Down Expand Up @@ -187,6 +196,9 @@ registerSuite('intern/lib/reporters/Html', {

tests.forEach((test, index) => {
const row = rows[index + 1];
const link = row.querySelectorAll('a')[0];
assert.equal(link.getAttribute('href'), test.expectedLink, 'expected link with for single test');
assert.equal(link.textContent, test.name, 'link should contain the test name');
if (test.error) {
assert.isTrue(row.classList.contains('failed'));
} else if (test.skipped) {
Expand All @@ -201,7 +213,9 @@ registerSuite('intern/lib/reporters/Html', {
passed() {
doTest([
{
id: 'foo - test',
id: 'foo - test 1',
name: 'test 1',
expectedLink: '?&grep=foo%20-%20test%201',
timeElapsed: 123
}
]);
Expand All @@ -210,7 +224,9 @@ registerSuite('intern/lib/reporters/Html', {
failed() {
doTest([
{
id: 'foo - test',
id: 'foo - test 2',
name: 'test 2',
expectedLink: '?&grep=foo%20-%20test%202',
timeElapsed: 123,
error: new Error('failed')
}
Expand All @@ -220,7 +236,9 @@ registerSuite('intern/lib/reporters/Html', {
skipped() {
doTest([
{
id: 'foo - test',
id: 'foo - test 3',
name: 'test 3',
expectedLink: '?&grep=foo%20-%20test%203',
timeElapsed: 123,
skipped: 'yes'
}
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/lib/reporters/support/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,26 @@ export function createMockDocument() {
};
return doc;
}

export function createLocation() {
return {
hash: '',
host: '',
hostname: '',
href: '',
origin: '',
pathname: '',
port: '',
protocol: '',
search: '',
assign: function() {
},
reload: function() {
},
replace: function() {
},
toString: function(): string {
return '';
}
};
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"declaration": true,
"lib": [
"es5",
"ES2015.Core",
"es2015.collection",
"es2015.promise",
"es2015.symbol",
Expand Down

0 comments on commit 5dff892

Please sign in to comment.