Skip to content

Commit

Permalink
README for unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
m1ch4 committed Oct 2, 2020
1 parent 55d6bb0 commit 826f78e
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 0 deletions.
6 changes: 6 additions & 0 deletions test/README.md
@@ -0,0 +1,6 @@
This folder is used to introduce unit testing.

A start is made with the 'www' part of Domoticz. The 'www-test'
folder contains tests for components in www. As the components in
www are written in JavaScript, so are the tests. See the README.md in
www-test for details.
61 changes: 61 additions & 0 deletions test/www-test/README.md
@@ -0,0 +1,61 @@
# WWW Unit tests
This folder contains the unit tests for components in www.

_Be warned! This is a work in progres._

# Tooling
## mocha
A tool called 'mocha' is used to drive the tests. The tests are written
specifically to be used with mocha. See: https://mochajs.org/

It is not written in stone that mocha be used for unit testing. A different
framework can be used for different kind of tests.

# Setup
## Installation
As the www-components are written in JavaScript, we need a JavaScript
engine to run on. For example, Node.js. See https://nodejs.org/en/. Make
sure you have Node.js installed. Node.js comes with npm, which is the
package manager that allows you to install different kind of packages
to use on your Node.js engine. For example, the mocha package.

In short, follow the installation instructions on the mocha website. But
what it says basically is:

`npm install --global mocha`

# Usage
## Structure
main.js - contains the requirejs module configuration. It should register
all the modules that the components under test use.
_path/component.js_ - the files containing the tests should be in the
same folder structure as the components under test are,
relative to the 'www' folder.

## Running the test
Run the tests in a specific file with:

`mocha _path_/test.js`

For example, there is one test.js file in app/log. Run this with:

`mocha app/log/test.js`

It should display something like this:


RefreshingDayChart
constructor
√ should be instantiable


1 passing (5ms)

device -> idx:1234, type:Type, subtype:SubType
installed:handler for event type time_update



If the test runs fine, only the test output is displayed.
If an error occurs, mocha will show the error and complete stacktrace.

135 changes: 135 additions & 0 deletions test/www-test/app/log/test.js
@@ -0,0 +1,135 @@
const assert = require('assert');
const requirejs = require('requirejs');
const simple = require('simple-mock');

require('jsdom-global')();

// before(function (done) {
// });
describe('RefreshingDayChart', function () {
describe('constructor', function () {
it('should be instantiable', function() {
function baseParams(jquery) {
return {
jquery: jquery
};
}
function angularParams(location, route, scope, element) {
return {
location: location,
route: route,
scope: scope,
element: element
};
}
function domoticzParams(globals, api, datapointApi) {
return {
globals: globals,
api: api,
datapointApi: datapointApi
};
}
function chartParams(range, device, chartTitle, autoRefreshIsEnabled, dataSupplier) {
return {
range: range,
device: device,
chartTitle: chartTitle,
autoRefreshIsEnabled: autoRefreshIsEnabled,
dataSupplier: dataSupplier
};
}
requirejs(['main.js'], function () {
requirejs(['jquery'], function($) {
const $element = function () {
};
const $scope = function () {
};
const $location = {
search: function() {
return {
consoledebug: 'true'
};
}
};
const device = {
idx: 1234,
Type: 'Type',
SubType: 'SubType',
getUnit: function() {
return 'kWh;'
}
};
const domoticzApi = function () {
};
const domoticzApiResponse = {
then: function(handler) {

}
};
const domoticzGlobals = {
Get5MinuteHistoryDaysGraphTitle: function() {
return 'Get5MinuteHistoryDaysGraphTitle';
},
sensorTypeForDevice: function(device) { return 'chartType:'+device.idx; },
sensorNameForDevice: function(device) { return 'sensorName:'+device.idx; },
axisTitleForDevice: function(device) { return 'axisTitle:'+device.idx; }
};
simple.mock($element, 'highcharts').callFn(function (x) {
return {
highcharts: function() {
return {
container: {
onmousedown: function() {

}
}
};
}
};
});
simple.mock(domoticzApi, 'sendRequest').returnWith(domoticzApiResponse);
simple.mock($scope, '$on').callFn(function (eventType, handler) { console.log('installed:handler for event type '+eventType); });
simple.mock($, 't').callFn(function (text) { return text; });
requirejs(['RefreshingChart'], function (RefreshingChart) {
const sut = new RefreshingChart(
baseParams($),
angularParams($location, null, $scope, $element),
domoticzParams(domoticzGlobals, domoticzApi, null),
chartParams(
'day',
device,
'Chart Title',
function() { return true; },
{
valueAxes: function() {
return [{
title: {
text: 'Axis Title'
},
labels: {
formatter: function () {
return 'value' + ' ' + 'unit';
}
}
}];
},
dateFromDataItem: function(dataItem) {
return dataItem.d;
},
seriesSuppliers: [
{
valueKey: 'v',
valueFromDataItem: function(dataItem) {
return dataItem[this.valueKey];
}
}
]
}
)
);
});
});
});
})
});
});
File renamed without changes.
22 changes: 22 additions & 0 deletions test/www-test/main.js
@@ -0,0 +1,22 @@
require.config({
baseUrl: '../../www',
paths: {
'angular': 'js/angular.min',
'angular-route': 'js/angular-route.min',
'angular-animate': 'js/angular-animate.min',
'ngSanitize': 'js/angular-sanitize.min',
'angular-md5': 'js/angular-md5-min',
'ng-grid': 'js/ng-grid.min',
'ng-grid-flexible-height': 'js/ng-grid-flexible-height',
'highcharts-ng': 'js/highcharts-ng.min',
'angularAMD': 'js/angularAMD.min',
'angular-tree-control': 'js/angular-tree-control',
jquery: '../test/www-test/js/jquery-3.5.1',
Base: 'app/log/Base',
AngularBase: 'app/log/AngularBase',
DomoticzBase: 'app/log/DomoticzBase',
RefreshingChart: 'app/log/RefreshingChart',
RefreshingSingleChart: 'app/log/RefreshingSingleChart',
RefreshingMinMaxAvgChart: 'app/log/RefreshingMinMaxAvgChart'
}
});

0 comments on commit 826f78e

Please sign in to comment.