Skip to content

Commit

Permalink
Unit test code coverage now at almost 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
doberkofler committed Mar 8, 2020
1 parent addef8b commit ec0ce10
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/__tests__/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module.exports = {
env: {
jest: true
},
rules: {
'jest/no-hooks': 'off',
}
};
2 changes: 2 additions & 0 deletions src/__tests__/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ describe('createElement', () => {
it('should create an element in the DOM', () => {
expect.hasAssertions();

createElement(document.body, 'div');

createElement(document.body, 'div', 'big', {color: 'red'}, 'text', {id: '1'});
expect(document.getElementById('1')).not.toBeNull();

Expand Down
19 changes: 14 additions & 5 deletions src/__tests__/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
/* eslint-disable @typescript-eslint/ban-ts-comment, @typescript-eslint/no-explicit-any */

import {GanttChart} from '../index';
//import {getElementByClassName} from '../dom/dom';
import {getElementByClassName} from '../dom/dom';

describe('the class GanttChart', () => {
beforeEach(() => {
//@ts-ignore
jest.spyOn(window, 'requestAnimationFrame').mockImplementation(cb => cb());
});

afterEach(() => {
(window.requestAnimationFrame as any).mockRestore();
});

it('should render a gantt chart', () => {
expect.hasAssertions();

Expand All @@ -26,10 +37,8 @@ describe('the class GanttChart', () => {
expect(container.innerHTML).toMatch(/^<div id=".*" class="gc_root_container_view".*>.*<\/div>$/);

// let's test scrolling
/*
const scrollbarElement = getElementByClassName('gc_timeline_scrollbar_cell');
console.log('scrollbarElement', scrollbarElement, scrollbarElement.innerHTML);
scrollbarElement.scrollTo(10, 10);
*/
scrollbarElement.scrollLeft = 10;
scrollbarElement.dispatchEvent(new Event('scroll'));
});
});
2 changes: 2 additions & 0 deletions src/__tests__/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('tags', () => {
it('getContainerOpen', () => {
expect.hasAssertions();

expect(getContainerOpen([])).toStrictEqual('<div>');
expect(getContainerOpen([], {})).toStrictEqual('<div>');
expect(getContainerOpen('big', {color: 'red', top: 0}, {id: 'content'})).toStrictEqual('<div id="content" class="big" style="color:red;top:0;">');
expect(getContainerOpen(['big', 'small'], {color: 'red', top: 0}, {id: 'content'})).toStrictEqual('<div id="content" class="big small" style="color:red;top:0;">');
Expand All @@ -26,6 +27,7 @@ describe('tags', () => {
it('getContainer', () => {
expect.hasAssertions();

expect(getContainer([])).toStrictEqual('<div></div>');
expect(getContainer(['big', 'small'], {color: 'red', top: 0}, 'text', {id: 'content'})).toStrictEqual('<div id="content" class="big small" style="color:red;top:0;">text</div>');
});
});
3 changes: 3 additions & 0 deletions src/components/Link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ function getArrowMarkup(point: pointType, direction: Direction, lineHeight: numb
break;

default:
/* istanbul ignore next */
throw new Error(`Invalid direction "${direction}"`);
}

Expand Down Expand Up @@ -174,6 +175,7 @@ function getLineSize(vector: vectorType, lineWidth: number, containerWidth: numb
};

default:
/* istanbul ignore next */
throw new Error(`Invalid direction "${vector.direction}"`);
}
}
Expand Down Expand Up @@ -219,6 +221,7 @@ function getLineContainerSize(vector: vectorType, cellHeight: number, wrapperWid
};

default:
/* istanbul ignore next */
throw new TypeError(`Invalid direction "${vector.direction}"`);
}
}
1 change: 1 addition & 0 deletions src/components/TimelineScrollbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export class TimelineScrollbar {
}

function getScrollLeft(event: Event): number|null {
/* istanbul ignore if */
if (event === null || event.target === null) {
return null;
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/dom/HtmlStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class HtmlStyle {
* @param {stylesType} styles - A object where each property represents a style component.
* @example style.merge({'color': black, 'background-color': 'white'});
*/
public merge(styles: stylesType = {}): void {
public merge(styles: stylesType): void {
for (const e in styles) {
const name = e.toLocaleLowerCase();
this._styles[name] = styles[e];
Expand Down
4 changes: 2 additions & 2 deletions src/dom/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@ export function addCustomEventListener(selector: string, event: string, handler:
*
* @param {Node} container - The parent node.
* @param {string} tag - The tag name.
* @param {string|Array<string>} classes - The string in the class attribute.
* @param {string|Array<string>} [classes=[]] - The string in the class attribute.
* @param {HtmlStyle|stylesType} [styles={}] - The styles in the style attribute.
* @param {string} [content] - The content of the div.
* @param {HtmlAttributes|attributesType} [attributes={}] - The optional attributes.
* @returns {HTMLElement} - The html markup.
* @example const element = createElement(document, 'div', 'cell', {width: 0}, 'hello world', {id: 'id'});
*/
export function createElement(container: Node, tag: string, classes: string|Array<string>, styles: HtmlStyle|stylesType = {}, content = '', attributes: HtmlAttributes|attributesType = {}): HTMLElement {
export function createElement(container: Node, tag: string, classes: string|Array<string> = [], styles: HtmlStyle|stylesType = {}, content = '', attributes: HtmlAttributes|attributesType = {}): HTMLElement {
const htmlAttributes = new HtmlAttributes(attributes);
const htmlStyle = new HtmlStyle(styles).toString();

Expand Down

0 comments on commit ec0ce10

Please sign in to comment.