Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add thing view tests with onOffSwitch. #935

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@
"nyc": "^11.0.2",
"simple-oauth2": "^1.4.0",
"sinon": "^4.5.0",
"ts-jest": "^21.0.0"
"ts-jest": "^21.0.0",
"websocket": "^1.0.25"
},
"jest": {
"coverageDirectory": "./coverage/",
Expand Down
44 changes: 42 additions & 2 deletions src/test/jsdom-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,50 @@

const sinon = require('sinon');
const {JSDOM} = require('jsdom');
const {server, chai} = require('./common');
const {
TEST_USER,
createUser,
} = require('./user');
const {URL} = require('url');
const {w3cwebsocket} = require('websocket');

// Setup the jsdom environment
const {document} =
(new JSDOM('<!doctype html><html><body></body></html>')).window;
(new JSDOM('<!doctype html><html><body></body></html>',
{url: 'https://localhost:4443/'})).window;
global.document = document;
global.window = document.defaultView;
global.navigator = global.window.navigator;
global.URL = URL;
// Since a test server dose not listen port, WebSocket always fails.
global.WebSocket = w3cwebsocket;
global.fetch = function(url, option) {
const {body, headers} = option;
const method = option.method ? option.method.toLowerCase() : 'get';

return new Promise((resolve, reject) => {
let request = chai.request(server)[method](url.pathname);
Object.keys(headers).forEach(function(key) {
request.set(key, headers[key]);
});

if (body) {
try {
request = request.send(JSON.parse(body));
} catch (err) {
reject(err);
}
}

request.end((_err, res) => {
res.json = () => {
return Promise.resolve(res.body);
};
resolve(res);
});
});
};

const strage = {};
global.localStorage = {
Expand All @@ -34,7 +71,10 @@ expect.extend({
},
});

beforeEach(() => {
beforeEach(async () => {
const jwt = await createUser(server, TEST_USER);
global.localStorage.setItem('jwt', jwt);
global.window.API.jwt = jwt;
global.sandbox = sinon.sandbox.create();
});

Expand Down
5 changes: 4 additions & 1 deletion src/test/schema-form/form-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,16 +366,19 @@ describe('Form', () => {
},
};

const addonName = 'settings-adapter';

const {node} = createSchemaForm({
schema,
addonName,
});

const input = node.querySelector('input');
input.value = 'bar';
fireEvent(input, 'change');

sandbox.stub(console, 'error').callsFake((error) => {
expect(error).toContain('fetch is not defined');
expect(error).toContain(`Failed to set config add-on: ${addonName}`);
done();
});

Expand Down
5 changes: 3 additions & 2 deletions src/test/schema-form/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ const STATIC_JS_PATH = '../../../static/js';

const SchemaForm = require(`${STATIC_JS_PATH}/addons-config/schema-form`);

module.exports.createSchemaForm = function({schema, formData}) {
const schemaForm = new SchemaForm(schema, 'test', 'test', formData);
module.exports.createSchemaForm =
function({schema, formData, addonName = 'test'}) {
const schemaForm = new SchemaForm(schema, 'test', addonName, formData);
const node = schemaForm.render();
return {schemaForm, node};
};
Expand Down
50 changes: 50 additions & 0 deletions src/test/thing-view/on-off-switch-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require('../jsdom-common');
const {_fireEvent, sleep, addThing} = require('./test-utils');


describe('OnOffSwitch', () => {
let thing;
const desc = {
id: 'onOffSwitchId',
name: 'onOffSwitchName',
type: 'onOffSwitch',
properties: {
on: {type: 'boolean', value: false},
},
actions: {
blink: {
description: 'Blink the switch on and off',
},
},
events: {
surge: {
description: 'Surge in power detected',
},
},
};

beforeEach(async () => {
thing = await addThing(desc);
});

it('should render a thing name', () => {
const node = document.body.querySelector('#things .thing-name');
expect(node.textContent).toEqual('onOffSwitchName');
});

it('should be switch off with input false', async () => {
await sleep(200);
const node = document.body.querySelector('.on-off-switch.off');
expect(node).not.toBeNull();
});

it('should handle a click event', async () => {
await sleep(200);
let node = document.body.querySelector('.on-off-switch.off');
expect(node).not.toBeNull();
thing.element.click();
await sleep(200);
node = document.body.querySelector('.on-off-switch.on');
expect(node).not.toBeNull();
});
});
90 changes: 90 additions & 0 deletions src/test/thing-view/test-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
const STATIC_JS_PATH = '../../../static/js';

const Constants = require('../../constants');
const {server, chai, mockAdapter} = require('../common');
const {headerAuth} = require('../user');

const UnknownThing = require(`${STATIC_JS_PATH}/unknown-thing`);
const OnOffSwitch = require(`${STATIC_JS_PATH}//on-off-switch`);
const BinarySensor = require(`${STATIC_JS_PATH}/binary-sensor`);
const ColorLight = require(`${STATIC_JS_PATH}/color-light`);
const DimmableLight = require(`${STATIC_JS_PATH}/dimmable-light`);
const DimmableColorLight = require(`${STATIC_JS_PATH}/dimmable-color-light`);
const OnOffLight = require(`${STATIC_JS_PATH}/on-off-light`);
const MultiLevelSwitch = require(`${STATIC_JS_PATH}/multi-level-switch`);
const MultiLevelSensor = require(`${STATIC_JS_PATH}/multi-level-sensor`);
const SmartPlug = require(`${STATIC_JS_PATH}/smart-plug`);

beforeEach(() => {
const element = document.createElement('div');
element.id = 'things';
document.body.appendChild(element);
});

afterEach(() => {
document.body.innerHTML = '';
});

module.exports.fireEvent = function(element, event) {
const evt = document.createEvent('HTMLEvents');
evt.initEvent(event, true, true);
return !element.dispatchEvent(evt);
};

module.exports.sleep = function(ms) {
return new Promise((res) => {
setTimeout(res, ms);
});
};

module.exports.addThing = async function(desc, format) {
const {id} = desc;
const jwt = global.localStorage.getItem('jwt');
await chai.request(server)
.post(Constants.THINGS_PATH)
.set('Accept', 'application/json')
.set(...headerAuth(jwt))
.send(desc);
await mockAdapter().addDevice(id, desc);

const res = await chai.request(server)
.get(`${Constants.THINGS_PATH}/${id}`)
.set('Accept', 'application/json')
.set(...headerAuth(jwt));

desc = res.body;
let thing;
switch (desc.type) {
case 'onOffSwitch':
thing = new OnOffSwitch(desc, format);
break;
case 'onOffLight':
thing = new OnOffLight(desc, format);
break;
case 'onOffColorLight':
thing = new ColorLight(desc, format);
break;
case 'dimmableLight':
thing = new DimmableLight(desc, format);
break;
case 'dimmableColorLight':
thing = new DimmableColorLight(desc, format);
break;
case 'binarySensor':
thing = new BinarySensor(desc, format);
break;
case 'multiLevelSensor':
thing = new MultiLevelSensor(desc, format);
break;
case 'multiLevelSwitch':
thing = new MultiLevelSwitch(desc, format);
break;
case 'smartPlug':
thing = new SmartPlug(desc, format);
break;
default:
thing = new UnknownThing(desc, format);
break;
}
return thing;
};
5 changes: 2 additions & 3 deletions static/js/thing.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
*/

'use strict';

const App = require('./app');
const API = require('./api');
const Utils = require('./utils');

Expand Down Expand Up @@ -40,7 +38,8 @@ const Thing = function(description, format, options) {
this.properties = {};
// Parse base URL of Thing
if (description.href) {
this.href = new URL(description.href, App.ORIGIN);
const origin = window.location.origin;
this.href = new URL(description.href, origin);
this.id = this.href.pathname.split('/').pop();

const wsHref = this.href.href.replace(/^http/, 'ws');
Expand Down
21 changes: 20 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5292,7 +5292,7 @@ is-tar@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/is-tar/-/is-tar-1.0.0.tgz#2f6b2e1792c1f5bb36519acaa9d65c0d26fe853d"

is-typedarray@~1.0.0:
is-typedarray@^1.0.0, is-typedarray@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/is-typedarray/-/is-typedarray-1.0.0.tgz#e479c80858df0c1b11ddda6940f96011fcda4a9a"

Expand Down Expand Up @@ -9684,6 +9684,12 @@ type-is@~1.6.15:
media-typer "0.3.0"
mime-types "~2.1.15"

typedarray-to-buffer@^3.1.2:
version "3.1.5"
resolved "https://registry.yarnpkg.com/typedarray-to-buffer/-/typedarray-to-buffer-3.1.5.tgz#a97ee7a9ff42691b9f783ff1bc5112fe3fca9080"
dependencies:
is-typedarray "^1.0.0"

typedarray@^0.0.6:
version "0.0.6"
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
Expand Down Expand Up @@ -10168,6 +10174,15 @@ webpack@^4.1.1:
watchpack "^1.5.0"
webpack-sources "^1.0.1"

websocket@^1.0.25:
version "1.0.25"
resolved "https://registry.yarnpkg.com/websocket/-/websocket-1.0.25.tgz#998ec790f0a3eacb8b08b50a4350026692a11958"
dependencies:
debug "^2.2.0"
nan "^2.3.3"
typedarray-to-buffer "^3.1.2"
yaeti "^0.0.6"

whatwg-encoding@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/whatwg-encoding/-/whatwg-encoding-1.0.1.tgz#3c6c451a198ee7aec55b1ec61d0920c67801a5f4"
Expand Down Expand Up @@ -10344,6 +10359,10 @@ y18n@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/y18n/-/y18n-4.0.0.tgz#95ef94f85ecc81d007c264e190a120f0a3c8566b"

yaeti@^0.0.6:
version "0.0.6"
resolved "https://registry.yarnpkg.com/yaeti/-/yaeti-0.0.6.tgz#f26f484d72684cf42bedfb76970aa1608fbf9577"

yallist@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/yallist/-/yallist-2.1.2.tgz#1c11f9218f076089a47dd512f93c6699a6a81d52"
Expand Down