1
0
mirror of https://github.com/home-assistant/frontend.git synced 2025-12-19 18:28:42 +00:00

Separate more JS util logic to be unit tested (#705)

* Move featureClassNames to js util

* Add tests for featureClassNames

* Strip empty feature class names

* Move canToggleDomain to js util

* Add tests for canToggleDomain

* Refactor canToggleDomain to ensure boolean return

* Switch to chai assert for richer syntax options

* Move canToggleState to js util

* Tests for canToggleState

* Enable linting for mocha tests

* Move stateCardType to js util

* Add tests for stateCardType

* Move stateMoreInfoType to js util

* Tests for stateMoreInfoType

* Include mdn Array includes polyfill
This commit is contained in:
Adam Mills
2017-12-03 23:56:16 -05:00
committed by Paulus Schoutsen
parent c1e7f4cc77
commit a723c62f4f
23 changed files with 340 additions and 106 deletions

View File

@@ -1,6 +1,6 @@
import attributeClassNames from '../../../js/common/util/attribute_class_names';
import { assert } from 'chai';
const assert = require('assert');
import attributeClassNames from '../../../js/common/util/attribute_class_names';
describe('attributeClassNames', () => {
const attrs = ['mock_attr1', 'mock_attr2'];

View File

@@ -0,0 +1,39 @@
import { assert } from 'chai';
import canToggleDomain from '../../../js/common/util/can_toggle_domain';
describe('canToggleDomain', () => {
const hass = {
config: {
services: {
light: {
turn_on: null, // Service keys only need to be present for test
turn_off: null,
},
lock: {
lock: null,
unlock: null,
},
sensor: {
custom_service: null,
},
},
},
};
it('Detects lights toggle', () => {
assert.isTrue(canToggleDomain(hass, 'light'));
});
it('Detects locks toggle', () => {
assert.isTrue(canToggleDomain(hass, 'lock'));
});
it('Detects sensors do not toggle', () => {
assert.isFalse(canToggleDomain(hass, 'sensor'));
});
it('Detects binary sensors do not toggle', () => {
assert.isFalse(canToggleDomain(hass, 'binary_sensor'));
});
});

View File

@@ -0,0 +1,40 @@
import { assert } from 'chai';
import canToggleState from '../../../js/common/util/can_toggle_state';
describe('canToggleState', () => {
const hass = {
config: {
services: {
light: {
turn_on: null, // Service keys only need to be present for test
turn_off: null,
},
},
},
};
it('Detects lights toggle', () => {
const stateObj = {
entity_id: 'light.bla',
state: 'on',
};
assert.isTrue(canToggleState(hass, stateObj));
});
it('Detects group with toggle', () => {
const stateObj = {
entity_id: 'group.bla',
state: 'on',
};
assert.isTrue(canToggleState(hass, stateObj));
});
it('Detects group without toggle', () => {
const stateObj = {
entity_id: 'group.devices',
state: 'home',
};
assert.isFalse(canToggleState(hass, stateObj));
});
});

View File

@@ -1,6 +1,6 @@
import computeDomain from '../../../js/common/util/compute_domain';
import { assert } from 'chai';
const assert = require('assert');
import computeDomain from '../../../js/common/util/compute_domain';
describe('computeDomain', () => {
it('Returns domains', () => {

View File

@@ -1,6 +1,6 @@
import computeStateDisplay from '../../../js/common/util/compute_state_display';
import { assert } from 'chai';
const assert = require('assert');
import computeStateDisplay from '../../../js/common/util/compute_state_display';
describe('computeStateDisplay', () => {
const haLocalize = function (namespace, message, ...args) {
@@ -156,7 +156,7 @@ describe('computeStateDisplay', () => {
});
it('Localizes custom state', () => {
const altHaLocalize = function (namespace, message, ...args) {
const altHaLocalize = function () {
// No matches can be found
return null;
};

View File

@@ -1,6 +1,6 @@
import computeStateDomain from '../../../js/common/util/compute_state_domain.js';
import { assert } from 'chai';
const assert = require('assert');
import computeStateDomain from '../../../js/common/util/compute_state_domain.js';
describe('computeStateDomain', () => {
it('Detects sensor domain', () => {

View File

@@ -0,0 +1,56 @@
import { assert } from 'chai';
import featureClassNames from '../../../js/common/util/feature_class_names';
describe('featureClassNames', () => {
const classNames = {
1: 'has-feature_a',
2: 'has-feature_b',
4: 'has-feature_c',
8: 'has-feature_d',
};
it('Skips null states', () => {
const stateObj = null;
assert.strictEqual(
featureClassNames(stateObj, classNames),
''
);
});
it('Matches no features', () => {
const stateObj = {
attributes: {
supported_features: 64,
},
};
assert.strictEqual(
featureClassNames(stateObj, classNames),
''
);
});
it('Matches one feature', () => {
const stateObj = {
attributes: {
supported_features: 72,
},
};
assert.strictEqual(
featureClassNames(stateObj, classNames),
'has-feature_d'
);
});
it('Matches two features', () => {
const stateObj = {
attributes: {
supported_features: 73,
},
};
assert.strictEqual(
featureClassNames(stateObj, classNames),
'has-feature_a has-feature_d'
);
});
});

View File

@@ -1,6 +1,6 @@
import formatDate from '../../../js/common/util/format_date';
import { assert } from 'chai';
const assert = require('assert');
import formatDate from '../../../js/common/util/format_date';
describe('formatDate', () => {
const dateObj = new Date(

View File

@@ -1,6 +1,6 @@
import formatDateTime from '../../../js/common/util/format_date_time';
import { assert } from 'chai';
const assert = require('assert');
import formatDateTime from '../../../js/common/util/format_date_time';
describe('formatDateTime', () => {
const dateObj = new Date(

View File

@@ -1,6 +1,6 @@
import formatTime from '../../../js/common/util/format_time';
import { assert } from 'chai';
const assert = require('assert');
import formatTime from '../../../js/common/util/format_time';
describe('formatTime', () => {
const dateObj = new Date(

View File

@@ -1,6 +1,6 @@
import { hasLocation } from '../../../js/common/util/location';
import { assert } from 'chai';
const assert = require('assert');
import { hasLocation } from '../../../js/common/util/location';
describe('hasLocation', () => {
it('flags states with location', () => {

View File

@@ -0,0 +1,55 @@
import { assert } from 'chai';
import stateCardType from '../../../js/common/util/state_card_type';
describe('stateCardType', () => {
const hass = {
config: {
services: {
light: {
turn_on: null, // Service keys only need to be present for test
turn_off: null,
},
},
},
};
it('Returns display for unavailable states', () => {
const stateObj = {
state: 'unavailable',
};
assert.strictEqual(stateCardType(hass, stateObj), 'display');
});
it('Returns media_player for media_player states', () => {
const stateObj = {
entity_id: 'media_player.bla',
};
assert.strictEqual(stateCardType(hass, stateObj), 'media_player');
});
it('Returns toggle for states that can toggle', () => {
const stateObj = {
entity_id: 'light.bla',
attributes: {},
};
assert.strictEqual(stateCardType(hass, stateObj), 'toggle');
});
it('Returns display for states with hidden control', () => {
const stateObj = {
entity_id: 'light.bla',
attributes: {
control: 'hidden',
},
};
assert.strictEqual(stateCardType(hass, stateObj), 'display');
});
it('Returns display for entities that cannot toggle', () => {
const stateObj = {
entity_id: 'sensor.bla',
};
assert.strictEqual(stateCardType(hass, stateObj), 'display');
});
});

View File

@@ -0,0 +1,28 @@
import { assert } from 'chai';
import stateMoreInfoType from '../../../js/common/util/state_more_info_type';
describe('stateMoreInfoType', () => {
it('Returns media_player for media_player states', () => {
const stateObj = {
entity_id: 'media_player.bla',
};
assert.strictEqual(stateMoreInfoType(stateObj), 'media_player');
});
it('Returns hidden for input_select states', () => {
const stateObj = {
entity_id: 'input_select.bla',
attributes: {},
};
assert.strictEqual(stateMoreInfoType(stateObj), 'hidden');
});
it('Returns default for switch states', () => {
const stateObj = {
entity_id: 'switch.bla',
attributes: {},
};
assert.strictEqual(stateMoreInfoType(stateObj), 'default');
});
});