MVP tests for hass-util.html (#629)

* MVP tests for hass-util.html

* MVP util.js to allow individual JS function export

* Use mocha to unit test js logic

* Isolate mocha test directory

* Move mocha opts to separate file

* Default export of util function

* Use reify for mocha tests instead of babel
This commit is contained in:
Adam Mills 2017-11-16 00:42:54 -05:00 committed by Paulus Schoutsen
parent 3ff9fe1041
commit 70c082716f
9 changed files with 155 additions and 27 deletions

1
.gitignore vendored
View File

@ -9,6 +9,7 @@ npm-debug.log
.DS_Store .DS_Store
hass_frontend/* hass_frontend/*
hass_frontend_es5/* hass_frontend_es5/*
.reify-cache
# Python stuff # Python stuff
*.py[cod] *.py[cod]

View File

@ -6,6 +6,7 @@ const rollupConfigEs6 = require('../../rollup.config-es6');
gulp.task('run_rollup_es5', () => gulp.src([ gulp.task('run_rollup_es5', () => gulp.src([
'js/core.js', 'js/core.js',
'js/compatibility.js', 'js/compatibility.js',
'js/util.js',
'js/automation-editor/automation-editor.js', 'js/automation-editor/automation-editor.js',
'js/script-editor/script-editor.js', 'js/script-editor/script-editor.js',
'demo_data/demo_data.js', 'demo_data/demo_data.js',
@ -16,6 +17,7 @@ gulp.task('run_rollup_es5', () => gulp.src([
gulp.task('run_rollup', () => gulp.src([ gulp.task('run_rollup', () => gulp.src([
'js/core.js', 'js/core.js',
'js/automation-editor/automation-editor.js', 'js/automation-editor/automation-editor.js',
'js/util.js',
'js/script-editor/script-editor.js', 'js/script-editor/script-editor.js',
'demo_data/demo_data.js', 'demo_data/demo_data.js',
]) ])

View File

@ -0,0 +1,6 @@
export default function attributeClassNames(stateObj, attributes) {
if (!stateObj) return '';
return attributes.map(function (attribute) {
return attribute in stateObj.attributes ? 'has-' + attribute : '';
}).filter(attr => attr !== '').join(' ');
}

13
js/util.js Normal file
View File

@ -0,0 +1,13 @@
/**
* Export hass util functions to window.
*
* This file is a workaround for the fact that Polymer 2 doesn't work well with
* ES6 JS imports. Once we move to Polymer 3, we should be able to simply
* import these functions where we need them.
*/
import attributeClassNames from './common/util/attribute_class_names';
window.hassUtil = window.hassUtil || {};
window.hassUtil.attributeClassNames = attributeClassNames;

View File

@ -17,7 +17,8 @@
"dev-watch-es5": "npm run gulp watch_ru_all_es5 gen-service-worker-es5", "dev-watch-es5": "npm run gulp watch_ru_all_es5 gen-service-worker-es5",
"lint_js": "eslint src panels js --ext js,html", "lint_js": "eslint src panels js --ext js,html",
"lint_html": "ls -1 src/home-assistant.html panels/**/ha-panel-*.html | xargs polymer lint --input", "lint_html": "ls -1 src/home-assistant.html panels/**/ha-panel-*.html | xargs polymer lint --input",
"test": "npm run lint_js && npm run lint_html" "mocha": "node_modules/.bin/mocha --opts test-mocha/mocha.opts",
"test": "npm run lint_js && npm run lint_html && npm run mocha"
}, },
"author": "Paulus Schoutsen <Paulus@PaulusSchoutsen.nl> (http://paulusschoutsen.nl)", "author": "Paulus Schoutsen <Paulus@PaulusSchoutsen.nl> (http://paulusschoutsen.nl)",
"license": "Apache-2.0", "license": "Apache-2.0",
@ -58,6 +59,7 @@
"home-assistant-js-websocket": "^1.1.2", "home-assistant-js-websocket": "^1.1.2",
"html-minifier": "^3.5.6", "html-minifier": "^3.5.6",
"merge-stream": "^1.0.1", "merge-stream": "^1.0.1",
"mocha": "^4.0.1",
"parse5": "^3.0.3", "parse5": "^3.0.3",
"polymer-analyzer": "^2.3.0", "polymer-analyzer": "^2.3.0",
"polymer-build": "^2.1.0", "polymer-build": "^2.1.0",
@ -65,6 +67,7 @@
"polymer-cli": "^1.5.6", "polymer-cli": "^1.5.6",
"preact": "^8.2.6", "preact": "^8.2.6",
"pump": "^1.0.2", "pump": "^1.0.2",
"reify": "^0.12.3",
"require-dir": "^0.3.2", "require-dir": "^0.3.2",
"rollup": "^0.51.3", "rollup": "^0.51.3",
"rollup-plugin-babel": "^3.0.2", "rollup-plugin-babel": "^3.0.2",

View File

@ -1,4 +1,5 @@
<script src='../../bower_components/fecha/fecha.min.js'></script> <script src='../../bower_components/fecha/fecha.min.js'></script>
<script src='../../build-temp/util.js'></script>
<!-- <!--
collection of utility functions. collection of utility functions.
@ -38,13 +39,6 @@ window.hassUtil.HIDE_MORE_INFO = [
window.hassUtil.LANGUAGE = navigator.languages ? window.hassUtil.LANGUAGE = navigator.languages ?
navigator.languages[0] : navigator.language || navigator.userLanguage; navigator.languages[0] : navigator.language || navigator.userLanguage;
window.hassUtil.attributeClassNames = function (stateObj, attributes) {
if (!stateObj) return '';
return attributes.map(function (attribute) {
return attribute in stateObj.attributes ? 'has-' + attribute : '';
}).join(' ');
};
// Expects featureClassNames to be an object mapping feature-bit -> className // Expects featureClassNames to be an object mapping feature-bit -> className
window.hassUtil.featureClassNames = function (stateObj, featureClassNames) { window.hassUtil.featureClassNames = function (stateObj, featureClassNames) {
if (!stateObj || !stateObj.attributes.supported_features) return ''; if (!stateObj || !stateObj.attributes.supported_features) return '';

View File

@ -0,0 +1,53 @@
import attributeClassNames from '../../../js/common/util/attribute_class_names';
const assert = require('assert');
describe('attributeClassNames', function() {
const attrs = ['mock_attr1', 'mock_attr2'];
it('null state', function() {
const stateObj = null;
assert.strictEqual(
attributeClassNames(stateObj, attrs),
'');
});
it('matches no attrbutes', function() {
const stateObj = {
attributes: {
other_attr_1: 1,
other_attr_2: 2,
},
};
assert.strictEqual(
attributeClassNames(stateObj, attrs),
'');
});
it('matches one attrbute', function() {
const stateObj = {
attributes: {
other_attr_1: 1,
other_attr_2: 2,
mock_attr1: 3,
},
};
assert.strictEqual(
attributeClassNames(stateObj, attrs),
'has-mock_attr1');
});
it('matches two attrbutes', function() {
const stateObj = {
attributes: {
other_attr_1: 1,
other_attr_2: 2,
mock_attr1: 3,
mock_attr2: null,
},
};
assert.strictEqual(
attributeClassNames(stateObj, attrs),
'has-mock_attr1 has-mock_attr2');
});
});

5
test-mocha/mocha.opts Normal file
View File

@ -0,0 +1,5 @@
-r reify
--recursive
--compilers js:babel-core/register
--timeout 10000
test-mocha

View File

@ -500,6 +500,10 @@ acorn@^5.2.1:
version "5.2.1" version "5.2.1"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-5.2.1.tgz#317ac7821826c22c702d66189ab8359675f135d7" resolved "https://registry.yarnpkg.com/acorn/-/acorn-5.2.1.tgz#317ac7821826c22c702d66189ab8359675f135d7"
acorn@~5.1.1:
version "5.1.2"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-5.1.2.tgz#911cb53e036807cf0fa778dc5d370fbd864246d7"
adm-zip@~0.4.3: adm-zip@~0.4.3:
version "0.4.7" version "0.4.7"
resolved "https://registry.yarnpkg.com/adm-zip/-/adm-zip-0.4.7.tgz#8606c2cbf1c426ce8c8ec00174447fd49b6eafc1" resolved "https://registry.yarnpkg.com/adm-zip/-/adm-zip-0.4.7.tgz#8606c2cbf1c426ce8c8ec00174447fd49b6eafc1"
@ -1957,7 +1961,7 @@ command-line-usage@^3.0.1, command-line-usage@^3.0.3, command-line-usage@^3.0.5:
table-layout "^0.3.0" table-layout "^0.3.0"
typical "^2.6.0" typical "^2.6.0"
commander@2.11.x, commander@^2.9.0, commander@~2.11.0: commander@2.11.0, commander@2.11.x, commander@^2.9.0, commander@~2.11.0:
version "2.11.0" version "2.11.0"
resolved "https://registry.yarnpkg.com/commander/-/commander-2.11.0.tgz#157152fd1e7a6c8d98a5b715cf376df928004563" resolved "https://registry.yarnpkg.com/commander/-/commander-2.11.0.tgz#157152fd1e7a6c8d98a5b715cf376df928004563"
@ -2243,7 +2247,7 @@ debug@2.6.8, debug@^2.2.0, debug@^2.6.8:
dependencies: dependencies:
ms "2.0.0" ms "2.0.0"
debug@^3.0.1: debug@3.1.0, debug@^3.0.1:
version "3.1.0" version "3.1.0"
resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261" resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261"
dependencies: dependencies:
@ -2378,6 +2382,10 @@ diff@3.2.0:
version "3.2.0" version "3.2.0"
resolved "https://registry.yarnpkg.com/diff/-/diff-3.2.0.tgz#c9ce393a4b7cbd0b058a725c93df299027868ff9" resolved "https://registry.yarnpkg.com/diff/-/diff-3.2.0.tgz#c9ce393a4b7cbd0b058a725c93df299027868ff9"
diff@3.3.1:
version "3.3.1"
resolved "https://registry.yarnpkg.com/diff/-/diff-3.3.1.tgz#aa8567a6eed03c531fc89d3f711cd0e5259dec75"
diff@^2.1.2: diff@^2.1.2:
version "2.2.3" version "2.2.3"
resolved "https://registry.yarnpkg.com/diff/-/diff-2.2.3.tgz#60eafd0d28ee906e4e8ff0a52c1229521033bf99" resolved "https://registry.yarnpkg.com/diff/-/diff-2.2.3.tgz#60eafd0d28ee906e4e8ff0a52c1229521033bf99"
@ -3457,6 +3465,17 @@ glob@7.1.1:
once "^1.3.0" once "^1.3.0"
path-is-absolute "^1.0.0" path-is-absolute "^1.0.0"
glob@7.1.2, glob@^7.0.0, glob@^7.0.3, glob@^7.0.5, glob@^7.1.1, glob@^7.1.2:
version "7.1.2"
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.2.tgz#c19c9df9a028702d678612384a6552404c636d15"
dependencies:
fs.realpath "^1.0.0"
inflight "^1.0.4"
inherits "2"
minimatch "^3.0.4"
once "^1.3.0"
path-is-absolute "^1.0.0"
glob@^4.3.1: glob@^4.3.1:
version "4.5.3" version "4.5.3"
resolved "https://registry.yarnpkg.com/glob/-/glob-4.5.3.tgz#c6cb73d3226c1efef04de3c56d012f03377ee15f" resolved "https://registry.yarnpkg.com/glob/-/glob-4.5.3.tgz#c6cb73d3226c1efef04de3c56d012f03377ee15f"
@ -3486,17 +3505,6 @@ glob@^6.0.1:
once "^1.3.0" once "^1.3.0"
path-is-absolute "^1.0.0" path-is-absolute "^1.0.0"
glob@^7.0.0, glob@^7.0.3, glob@^7.0.5, glob@^7.1.1, glob@^7.1.2:
version "7.1.2"
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.2.tgz#c19c9df9a028702d678612384a6552404c636d15"
dependencies:
fs.realpath "^1.0.0"
inflight "^1.0.4"
inherits "2"
minimatch "^3.0.4"
once "^1.3.0"
path-is-absolute "^1.0.0"
glob@~3.1.21: glob@~3.1.21:
version "3.1.21" version "3.1.21"
resolved "https://registry.yarnpkg.com/glob/-/glob-3.1.21.tgz#d29e0a055dea5138f4d07ed40e8982e83c2066cd" resolved "https://registry.yarnpkg.com/glob/-/glob-3.1.21.tgz#d29e0a055dea5138f4d07ed40e8982e83c2066cd"
@ -3644,6 +3652,10 @@ grouped-queue@^0.3.0:
dependencies: dependencies:
lodash "^4.17.2" lodash "^4.17.2"
growl@1.10.3:
version "1.10.3"
resolved "https://registry.yarnpkg.com/growl/-/growl-1.10.3.tgz#1926ba90cf3edfe2adb4927f5880bc22c66c790f"
growl@1.9.2: growl@1.9.2:
version "1.9.2" version "1.9.2"
resolved "https://registry.yarnpkg.com/growl/-/growl-1.9.2.tgz#0ea7743715db8d8de2c5ede1775e1b45ac85c02f" resolved "https://registry.yarnpkg.com/growl/-/growl-1.9.2.tgz#0ea7743715db8d8de2c5ede1775e1b45ac85c02f"
@ -5344,6 +5356,18 @@ minimist@^1.1.0, minimist@^1.1.3, minimist@^1.2.0:
version "1.2.0" version "1.2.0"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284" resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284"
minipass@^2.2.1:
version "2.2.1"
resolved "https://registry.yarnpkg.com/minipass/-/minipass-2.2.1.tgz#5ada97538b1027b4cf7213432428578cb564011f"
dependencies:
yallist "^3.0.0"
minizlib@^1.0.3:
version "1.0.4"
resolved "https://registry.yarnpkg.com/minizlib/-/minizlib-1.0.4.tgz#8ebb51dd8bbe40b0126b5633dbb36b284a2f523c"
dependencies:
minipass "^2.2.1"
mkdirp@0.5.0: mkdirp@0.5.0:
version "0.5.0" version "0.5.0"
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.0.tgz#1d73076a6df986cd9344e15e71fcc05a4c9abf12" resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.0.tgz#1d73076a6df986cd9344e15e71fcc05a4c9abf12"
@ -5373,6 +5397,21 @@ mocha@^3.4.2:
mkdirp "0.5.1" mkdirp "0.5.1"
supports-color "3.1.2" supports-color "3.1.2"
mocha@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/mocha/-/mocha-4.0.1.tgz#0aee5a95cf69a4618820f5e51fa31717117daf1b"
dependencies:
browser-stdout "1.3.0"
commander "2.11.0"
debug "3.1.0"
diff "3.3.1"
escape-string-regexp "1.0.5"
glob "7.1.2"
growl "1.10.3"
he "1.1.1"
mkdirp "0.5.1"
supports-color "4.4.0"
ms@0.7.0: ms@0.7.0:
version "0.7.0" version "0.7.0"
resolved "https://registry.yarnpkg.com/ms/-/ms-0.7.0.tgz#865be94c2e7397ad8a57da6a633a6e2f30798b83" resolved "https://registry.yarnpkg.com/ms/-/ms-0.7.0.tgz#865be94c2e7397ad8a57da6a633a6e2f30798b83"
@ -6533,6 +6572,14 @@ regjsparser@^0.1.4:
dependencies: dependencies:
jsesc "~0.5.0" jsesc "~0.5.0"
reify@^0.12.3:
version "0.12.3"
resolved "https://registry.yarnpkg.com/reify/-/reify-0.12.3.tgz#b1aa0c196e7dc7fb7e9ad42b87c1e10fe7f97f97"
dependencies:
acorn "~5.1.1"
minizlib "^1.0.3"
semver "^5.3.0"
relateurl@0.2.x: relateurl@0.2.x:
version "0.2.7" version "0.2.7"
resolved "https://registry.yarnpkg.com/relateurl/-/relateurl-0.2.7.tgz#54dbf377e51440aca90a4cd274600d3ff2d888a9" resolved "https://registry.yarnpkg.com/relateurl/-/relateurl-0.2.7.tgz#54dbf377e51440aca90a4cd274600d3ff2d888a9"
@ -7434,6 +7481,12 @@ supports-color@3.1.2:
dependencies: dependencies:
has-flag "^1.0.0" has-flag "^1.0.0"
supports-color@4.4.0, supports-color@^4.0.0:
version "4.4.0"
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-4.4.0.tgz#883f7ddabc165142b2a61427f3352ded195d1a3e"
dependencies:
has-flag "^2.0.0"
supports-color@^0.2.0: supports-color@^0.2.0:
version "0.2.0" version "0.2.0"
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-0.2.0.tgz#d92de2694eb3f67323973d7ae3d8b55b4c22190a" resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-0.2.0.tgz#d92de2694eb3f67323973d7ae3d8b55b4c22190a"
@ -7442,12 +7495,6 @@ supports-color@^2.0.0:
version "2.0.0" version "2.0.0"
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-2.0.0.tgz#535d045ce6b6363fa40117084629995e9df324c7" resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-2.0.0.tgz#535d045ce6b6363fa40117084629995e9df324c7"
supports-color@^4.0.0:
version "4.4.0"
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-4.4.0.tgz#883f7ddabc165142b2a61427f3352ded195d1a3e"
dependencies:
has-flag "^2.0.0"
sw-precache@^5.1.1, sw-precache@^5.2.0: sw-precache@^5.1.1, sw-precache@^5.2.0:
version "5.2.0" version "5.2.0"
resolved "https://registry.yarnpkg.com/sw-precache/-/sw-precache-5.2.0.tgz#eb6225ce580ceaae148194578a0ad01ab7ea199c" resolved "https://registry.yarnpkg.com/sw-precache/-/sw-precache-5.2.0.tgz#eb6225ce580ceaae148194578a0ad01ab7ea199c"
@ -8372,6 +8419,10 @@ yallist@^2.1.2:
version "2.1.2" version "2.1.2"
resolved "https://registry.yarnpkg.com/yallist/-/yallist-2.1.2.tgz#1c11f9218f076089a47dd512f93c6699a6a81d52" resolved "https://registry.yarnpkg.com/yallist/-/yallist-2.1.2.tgz#1c11f9218f076089a47dd512f93c6699a6a81d52"
yallist@^3.0.0:
version "3.0.2"
resolved "https://registry.yarnpkg.com/yallist/-/yallist-3.0.2.tgz#8452b4bb7e83c7c188d8041c1a837c773d6d8bb9"
yargs@~3.10.0: yargs@~3.10.0:
version "3.10.0" version "3.10.0"
resolved "https://registry.yarnpkg.com/yargs/-/yargs-3.10.0.tgz#f7ee7bd857dd7c1d2d38c0e74efbd681d1431fd1" resolved "https://registry.yarnpkg.com/yargs/-/yargs-3.10.0.tgz#f7ee7bd857dd7c1d2d38c0e74efbd681d1431fd1"