Skip to content

Commit

Permalink
Tools: Fixed TypeScript-based test tools.
Browse files Browse the repository at this point in the history
  • Loading branch information
bre1470 committed Apr 15, 2019
1 parent e1672a4 commit 8d689e4
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 78 deletions.
5 changes: 5 additions & 0 deletions test/test-controller.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*!*
*
* Copyright (c) Highsoft AS. All rights reserved.
*
*!*/
declare type HighchartsElement = (Highcharts.HTMLDOMElement | Highcharts.SVGDOMElement);
/**
* Contains x and y position relative to the chart.
Expand Down
4 changes: 2 additions & 2 deletions test/test-controller.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* *
/*!*
*
* Copyright (c) Highsoft AS. All rights reserved.
*
* */
*!*/

/* *
*
Expand Down
32 changes: 18 additions & 14 deletions test/test-template.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/*!*
*
* Copyright (c) Highsoft AS. All rights reserved.
*
*!*/
/**
* Constructor for a test chart
*/
declare type TestChartConstructor = (container: (string | Highcharts.HTMLDOMElement), options: Highcharts.Options) => Highcharts.Chart;
/**
* Callback for one test
*/
Expand All @@ -6,13 +15,16 @@ declare type TestTemplateCallback = (testTemplate: TestTemplate) => void;
* Initializer for each test
*/
declare type TestTemplateInitializer = (this: Highcharts.Chart, chartOptions: Highcharts.Options) => void;
interface TestChart extends Highcharts.Chart {
template?: string;
}
interface TestTemplateCase {
chartOptions: Highcharts.Options;
testCallback: TestTemplateCallback;
}
interface TestTemplateRegistry {
name: string;
chartConstructor: Function;
chartConstructor: TestChartConstructor;
chartOptions: Highcharts.Options;
testInitializer: TestTemplateInitializer;
}
Expand Down Expand Up @@ -43,10 +55,8 @@ declare class TestTemplate {
*
* @param testInitializer
* The initializer function for a test case. (optional)
*
* @return {void}
*/
static register: (name: string, chartConstructor: Function, chartOptions: Highcharts.Options, testInitializer?: Function) => void;
static register: (name: string, chartConstructor: TestChartConstructor, chartOptions: Highcharts.Options, testInitializer?: TestTemplateInitializer) => void;
/**
* Prepares a chart template for a test. This function works asynchronously.
*
Expand All @@ -59,10 +69,8 @@ declare class TestTemplate {
* @param testCallback
* The callback with the prepared chart template as the first
* argument.
*
* @return {void}
*/
static test(name: string, chartOptions: Highcharts.Options, testCallback: TestTemplateCallback): void;
static test(name: string, chartOptions?: Highcharts.Options, testCallback?: TestTemplateCallback): void;
/**
* Creates a deep copy of entries and properties.
*
Expand Down Expand Up @@ -97,19 +105,15 @@ declare class TestTemplate {
* @param testInitializer
* The initializer function for a test case. (optional)
*/
constructor(name: string, chartConstructor: Function, chartOptions: Highcharts.Options, testInitializer?: TestTemplateInitializer);
/**
* The chart template registry
*/
private templates;
constructor(name: string, chartConstructor: TestChartConstructor, chartOptions: Highcharts.Options, testInitializer?: TestTemplateInitializer);
/**
* The name of the chart template
*/
name: string;
/**
* The chart instance of the chart template
*/
chart: Highcharts.Chart;
chart: TestChart;
/**
* The state of the chart template
*/
Expand All @@ -132,5 +136,5 @@ declare class TestTemplate {
* @param testCallback
* The callback to test the chart
*/
test(chartOptions: Highcharts.Options, testCallback: TestTemplateCallback): void;
test(chartOptions?: Highcharts.Options, testCallback?: TestTemplateCallback): void;
}
112 changes: 52 additions & 60 deletions test/test-template.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
/*!*
*
* Copyright (c) Highsoft AS. All rights reserved.
*
*!*/

/* *
*
* Types
*
* */

/**
* Constructor for a test chart
*/
type TestChartConstructor = (
container: (string|Highcharts.HTMLDOMElement),
options: Highcharts.Options
) => Highcharts.Chart;

/**
* Callback for one test
*/
Expand All @@ -12,23 +26,29 @@ type TestTemplateCallback = (testTemplate: TestTemplate) => void;
/**
* Initializer for each test
*/
type TestTemplateInitializer =
(this: Highcharts.Chart, chartOptions: Highcharts.Options) => void;
type TestTemplateInitializer =(
this: Highcharts.Chart,
chartOptions: Highcharts.Options
) => void;

/* *
*
* Interfaces
*
* */

interface TestChart extends Highcharts.Chart {
template?: string;
}

interface TestTemplateCase {
chartOptions: Highcharts.Options;
testCallback: TestTemplateCallback;
}

interface TestTemplateRegistry {
name: string;
chartConstructor: Function;
chartConstructor: TestChartConstructor;
chartOptions: Highcharts.Options;
testInitializer: TestTemplateInitializer;
}
Expand Down Expand Up @@ -93,25 +113,14 @@ class TestTemplate {
*
* @param testInitializer
* The initializer function for a test case. (optional)
*
* @return {void}
*/
public static register = function (
name: string,
chartConstructor: Function,
chartConstructor: TestChartConstructor,
chartOptions: Highcharts.Options,
testInitializer?: Function
testInitializer: TestTemplateInitializer = undefined
) {

if (typeof name !== 'string' ||
typeof chartConstructor !== 'function' ||
typeof chartOptions !== 'object' ||
(typeof testInitializer !== 'undefined' &&
typeof testInitializer !== 'function')
) {
throw new Error('Arguments are invalid');
}

if (TestTemplate.templates[name]) {
throw new Error('Chart template already registered');
}
Expand All @@ -121,7 +130,7 @@ class TestTemplate {
chartConstructor: chartConstructor,
chartOptions: chartOptions,
testInitializer: testInitializer
} as TestTemplateRegistry;
};
}

/**
Expand All @@ -136,30 +145,19 @@ class TestTemplate {
* @param testCallback
* The callback with the prepared chart template as the first
* argument.
*
* @return {void}
*/
public static test (
name: string,
chartOptions: Highcharts.Options,
testCallback: TestTemplateCallback
chartOptions: Highcharts.Options = {},
testCallback: TestTemplateCallback = undefined
) {

if (typeof name !== 'string' ||
typeof chartOptions !== 'object' ||
typeof testCallback !== 'function'
) {
throw new Error('Arguments are invalid');
}

let template = TestTemplate.templates[name];

if (!template) {
throw new Error('Template "' + name + '" is not registered');
}

chartOptions = (chartOptions || {});

if (!(template instanceof TestTemplate)) {
TestTemplate.templates[name] = template = new TestTemplate(
template.name,
Expand Down Expand Up @@ -189,9 +187,7 @@ class TestTemplate {
return source;
}

if (source instanceof Array) {
return JSON.parse(JSON.stringify(source));
}
// @todo handle arrays (peek into karma-setup how it is handled there)

var copy = {} as any;

Expand Down Expand Up @@ -236,11 +232,11 @@ class TestTemplate {

removeEvent();

var undoOption;
while (typeof (undoOption = undoStack.pop()) !== 'undefined') {
let undoOption;

while (!!(undoOption = undoStack.pop())) {
chart.update(undoOption, false, true, false);
}

};
}

Expand Down Expand Up @@ -268,34 +264,33 @@ class TestTemplate {
*/
constructor (
name: string,
chartConstructor: Function,
chartConstructor: TestChartConstructor,
chartOptions: Highcharts.Options,
testInitializer?: TestTemplateInitializer
) {

this.name = name;
this.ready = true;
this.testCases = [];
this.testInitializer = testInitializer;
if (!(this instanceof TestTemplate)) {
return new TestTemplate(
name, chartConstructor, chartOptions, testInitializer
);
}

this.chart = chartConstructor(
TestTemplate.createContainer(), chartOptions
);
(this.chart as any).template = this.name;
this.chart.template = name;
this.name = name;
this.ready = true;
this.testCases = [];
this.testInitializer = testInitializer;
}


/* *
*
* Properties
*
* */

/**
* The chart template registry
*/
private templates = {};

/**
* The name of the chart template
*/
Expand All @@ -304,7 +299,7 @@ class TestTemplate {
/**
* The chart instance of the chart template
*/
public chart: Highcharts.Chart;
public chart: TestChart;

/**
* The state of the chart template
Expand Down Expand Up @@ -338,13 +333,12 @@ class TestTemplate {
* The callback to test the chart
*/
public test (
chartOptions: Highcharts.Options, testCallback: TestTemplateCallback
chartOptions: Highcharts.Options = {},
testCallback: TestTemplateCallback = undefined
) {

chartOptions = (chartOptions || {});

var chart = this.chart,
testInitializer = this.testInitializer;
const chart = this.chart;
const testInitializer = this.testInitializer;

this.testCases.push({
chartOptions: chartOptions,
Expand Down Expand Up @@ -375,22 +369,20 @@ class TestTemplate {
chart.update(testCase.chartOptions, true, true, false);
chart.container.style.zIndex = '9999';
testCase.testCallback(this);

} finally {
}
finally {

if (typeof undoUpdates === 'function') {
undoUpdates();
}

chart.container.style.zIndex = '';

}
}

} finally {
}
finally {

this.ready = true;

}
}
}
10 changes: 9 additions & 1 deletion test/test-utilities.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
/*!*
*
* Copyright (c) Highsoft AS. All rights reserved.
*
*!*/
/**
* Useful functions for test purposes.
*/
declare class TestUtilities {
private static readonly timeString;
/**
* A string representation for the current browser. Possible values are
* `Chrome`, `Edge`, `Firefox`, `MSIE`, `Netscape`, `Opera`, `PhantomJS`,
* `Safari`, and an empty string for unknown browsers.
*/
private static readonly browser;
static readonly browser: string;
/**
* Indiciates, if system time runs in CET timezone.
*/
Expand Down
11 changes: 10 additions & 1 deletion test/test-utilities.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/*!*
*
* Copyright (c) Highsoft AS. All rights reserved.
*
*!*/

/**
* Useful functions for test purposes.
*/
class TestUtilities {

/* *
Expand All @@ -13,7 +22,7 @@ class TestUtilities {
* `Chrome`, `Edge`, `Firefox`, `MSIE`, `Netscape`, `Opera`, `PhantomJS`,
* `Safari`, and an empty string for unknown browsers.
*/
private static readonly browser: string = (function () {
public static readonly browser: string = (function () {
var userAgent = window.navigator.userAgent;
if ((new RegExp('MSIE|Trident', 'i')).test(userAgent) &&
!(new RegExp('Opera', 'i')).test(userAgent)
Expand Down

0 comments on commit 8d689e4

Please sign in to comment.