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

Compute state display tests (#643)

* Move computeDomain and format functions to js

* Add tests for computeStateDisplay

* Always recalculate state display

* Remove LANGUAGE from hassUtils object

* Move AppLocalizeBehavior import to mixins

* Import mixins to state-card-display

* Safety check on computeStateDisplay

* Don't store computed domains on stateObj

* Integration tests for state-card-display

* Include extractDomain code in polymer repo

* Remove util function null checking

* Dont render test element without hass and stateObj

* Revert "Don't store computed domains on stateObj"

This reverts commit e3509d7182.

* Revert "Always recalculate state display"

This reverts commit 27c24e2694.
This commit is contained in:
Adam Mills
2017-11-21 00:46:36 -05:00
committed by Paulus Schoutsen
parent 7e77a7c32c
commit 3412edb843
20 changed files with 476 additions and 141 deletions

View File

@@ -5,14 +5,14 @@ const assert = require('assert');
describe('attributeClassNames', function() {
const attrs = ['mock_attr1', 'mock_attr2'];
it('null state', function() {
it('Skips null states', function() {
const stateObj = null;
assert.strictEqual(
attributeClassNames(stateObj, attrs),
'');
});
it('matches no attrbutes', function() {
it('Matches no attrbutes', function() {
const stateObj = {
attributes: {
other_attr_1: 1,
@@ -24,7 +24,7 @@ describe('attributeClassNames', function() {
'');
});
it('matches one attrbute', function() {
it('Matches one attrbute', function() {
const stateObj = {
attributes: {
other_attr_1: 1,
@@ -37,7 +37,7 @@ describe('attributeClassNames', function() {
'has-mock_attr1');
});
it('matches two attrbutes', function() {
it('Matches two attrbutes', function() {
const stateObj = {
attributes: {
other_attr_1: 1,

View File

@@ -0,0 +1,12 @@
import computeDomain from '../../../js/common/util/compute_domain';
const assert = require('assert');
describe('computeDomain', function() {
it('Detects sensor domain', function() {
const stateObj = {
entity_id: 'sensor.test',
};
assert.strictEqual(computeDomain(stateObj), 'sensor');
});
});

View File

@@ -0,0 +1,184 @@
import computeStateDisplay from '../../../js/common/util/compute_state_display';
const assert = require('assert');
describe('computeStateDisplay', function() {
const haLocalize = function(namespace, message, ...args) {
// Mock Localize function for testing
return namespace + '.' + message + (args.length ? ': ' + args.join(',') : '');
};
it('Localizes binary sensor defaults', function() {
const stateObj = {
entity_id: 'binary_sensor.test',
state: 'off',
attributes: {
},
};
assert.strictEqual(computeStateDisplay(haLocalize, stateObj, 'en'), 'state.binary_sensor.default.off');
});
it('Localizes binary sensor device class', function() {
const stateObj = {
entity_id: 'binary_sensor.test',
state: 'off',
attributes: {
device_class: 'moisture',
},
};
assert.strictEqual(computeStateDisplay(haLocalize, stateObj, 'en'), 'state.binary_sensor.moisture.off');
});
it('Localizes binary sensor invalid device class', function() {
const altHaLocalize = function(namespace, message, ...args) {
if (namespace === 'state.binary_sensor.invalid_device_class') return null;
return haLocalize(namespace, message, ...args);
};
const stateObj = {
entity_id: 'binary_sensor.test',
state: 'off',
attributes: {
device_class: 'invalid_device_class',
},
};
assert.strictEqual(computeStateDisplay(altHaLocalize, stateObj, 'en'), 'state.binary_sensor.default.off');
});
it('Localizes sensor value with units', function() {
const stateObj = {
entity_id: 'sensor.test',
state: '123',
attributes: {
unit_of_measurement: 'm',
},
};
assert.strictEqual(computeStateDisplay(haLocalize, stateObj, 'en'), '123 m');
});
it('Localizes input_datetime with full date time', function() {
const stateObj = {
entity_id: 'input_datetime.test',
state: '123',
attributes: {
has_date: true,
has_time: true,
year: 2017,
month: 11,
day: 18,
hour: 11,
minute: 12,
second: 13,
},
};
assert.strictEqual(computeStateDisplay(haLocalize, stateObj, 'en'), 'November 18, 2017, 11:12 AM');
});
it('Localizes input_datetime with date', function() {
const stateObj = {
entity_id: 'input_datetime.test',
state: '123',
attributes: {
has_date: true,
has_time: false,
year: 2017,
month: 11,
day: 18,
hour: 11,
minute: 12,
second: 13,
},
};
assert.strictEqual(computeStateDisplay(haLocalize, stateObj, 'en'), 'November 18, 2017');
});
it('Localizes input_datetime with time', function() {
const stateObj = {
entity_id: 'input_datetime.test',
state: '123',
attributes: {
has_date: false,
has_time: true,
year: 2017,
month: 11,
day: 18,
hour: 11,
minute: 12,
second: 13,
},
};
assert.strictEqual(computeStateDisplay(haLocalize, stateObj, 'en'), '11:12 AM');
});
it('Localizes zwave ready', function() {
const stateObj = {
entity_id: 'zwave.test',
state: 'ready',
attributes: {
query_stage: 'Complete',
},
};
assert.strictEqual(computeStateDisplay(haLocalize, stateObj, 'en'), 'state.zwave.default.ready');
});
it('Localizes zwave initializing', function() {
const stateObj = {
entity_id: 'zwave.test',
state: 'initializing',
attributes: {
query_stage: 'Probe',
},
};
assert.strictEqual(computeStateDisplay(haLocalize, stateObj, 'en'), 'state.zwave.query_stage.initializing: query_stage,Probe');
});
it('Localizes cover open', function() {
const stateObj = {
entity_id: 'cover.test',
state: 'open',
attributes: {
},
};
assert.strictEqual(computeStateDisplay(haLocalize, stateObj, 'en'), 'state.cover.open');
});
it('Localizes unavailable', function() {
const altHaLocalize = function(namespace, message, ...args) {
if (namespace === 'state.sensor') return null;
return haLocalize(namespace, message, ...args);
};
const stateObj = {
entity_id: 'sensor.test',
state: 'unavailable',
attributes: {
},
};
assert.strictEqual(computeStateDisplay(altHaLocalize, stateObj, 'en'), 'state.default.unavailable');
});
it('Localizes custom state', function() {
const altHaLocalize = function(namespace, message, ...args) {
// No matches can be found
return null;
};
const stateObj = {
entity_id: 'sensor.test',
state: 'My Custom State',
attributes: {
},
};
assert.strictEqual(computeStateDisplay(altHaLocalize, stateObj, 'en'), 'My Custom State');
});
it('Only calculates state display once per immutable state object', function() {
const stateObj = {
entity_id: 'cover.test',
state: 'open',
attributes: {
},
};
assert.strictEqual(computeStateDisplay(haLocalize, stateObj, 'en'), 'state.cover.open');
stateObj.state = 'closing';
assert.strictEqual(computeStateDisplay(haLocalize, stateObj, 'en'), 'state.cover.open');
});
});

View File

@@ -0,0 +1,20 @@
import formatDate from '../../../js/common/util/format_date';
const assert = require('assert');
describe('formatDate', function() {
const dateObj = new Date(
2017, 10, 18,
11, 12, 13, 1400,
);
it('Formats English dates', function() {
assert.strictEqual(formatDate(dateObj, 'en'), 'November 18, 2017');
});
// Node only contains intl support for english formats. This test at least ensures
// the fallback to a different locale
it('Formats other dates', function() {
assert.strictEqual(formatDate(dateObj, 'fr'), '2017 M11 18');
});
});

View File

@@ -0,0 +1,20 @@
import formatDateTime from '../../../js/common/util/format_date_time';
const assert = require('assert');
describe('formatDateTime', function() {
const dateObj = new Date(
2017, 10, 18,
11, 12, 13, 1400,
);
it('Formats English date times', function() {
assert.strictEqual(formatDateTime(dateObj, 'en'), 'November 18, 2017, 11:12 AM');
});
// Node only contains intl support for english formats. This test at least ensures
// the fallback to a different locale
it('Formats other date times', function() {
assert.strictEqual(formatDateTime(dateObj, 'fr'), '2017 M11 18 11:12');
});
});

View File

@@ -0,0 +1,20 @@
import formatTime from '../../../js/common/util/format_time';
const assert = require('assert');
describe('formatTime', function() {
const dateObj = new Date(
2017, 10, 18,
11, 12, 13, 1400,
);
it('Formats English times', function() {
assert.strictEqual(formatTime(dateObj, 'en'), '11:12 AM');
});
// Node only contains intl support for english formats. This test at least ensures
// the fallback to a different locale
it('Formats other times', function() {
assert.strictEqual(formatTime(dateObj, 'fr'), '11:12');
});
});