Skip to content

Commit

Permalink
Add stub config tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bokub committed Apr 26, 2021
1 parent d596dac commit 5819e74
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ However, you can enforce a [specific version](https://github.com/bokub/rgb-light

The `rbg-light-card` is meant to be included in the [Lovelace Entities Card](https://www.home-assistant.io/lovelace/entities/)

You can start with a sample configuration by choosing "**Custom: RGB Light Card**" in the Lovelace card selector

Example configuration:

```yaml
Expand Down
4 changes: 2 additions & 2 deletions card.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ class RGBLightCard extends HTMLElement {
entity.entity_id.indexOf('light.') === 0 &&
entity.attributes &&
entity.attributes.supported_color_modes &&
entity.attributes.supported_color_modes.indexOf('hs') !== -1
entity.attributes.supported_color_modes.find((mode) => ['hs', 'rgb', 'xy'].indexOf(mode) !== -1)
);
const entity = supportedEntities.length > 0 ? supportedEntities[0].entity_id : 'light.example_light';

Expand Down Expand Up @@ -255,7 +255,7 @@ window.customCards.push({
});

console.info(
'\n %c RGB Light Card %c v1.8.0 %c \n',
'\n %c RGB Light Card %c v1.9.0 %c \n',
'background-color: #555;color: #fff;padding: 3px 2px 3px 3px;border-radius: 3px 0 0 3px;font-family: DejaVu Sans,Verdana,Geneva,sans-serif;text-shadow: 0 1px 0 rgba(1, 1, 1, 0.3)',
'background-color: #bc81e0;background-image: linear-gradient(90deg, #b65cff, #11cbfa);color: #fff;padding: 3px 3px 3px 2px;border-radius: 0 3px 3px 0;font-family: DejaVu Sans,Verdana,Geneva,sans-serif;text-shadow: 0 1px 0 rgba(1, 1, 1, 0.3)',
'background-color: transparent'
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "rgb-light-card",
"description": "A Lovelace custom card for RGB lights",
"version": "1.8.0",
"version": "1.9.0",
"author": "Boris K",
"bugs": "https://github.com/bokub/rgb-light-card/issues",
"devDependencies": {
Expand Down
3 changes: 3 additions & 0 deletions test/_setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ global.logged = [];
console.info = (...args) => {
logged.push(args.join(' '));
};

// Mock window for window.customCards
global.window = {};
31 changes: 30 additions & 1 deletion test/unit-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ test("Setting HASS creates the card, but doesn't update it", (t) => {
t.is(oldContent, card.content);
});

test('Hide when off option works ', (t) => {
test('hide_when_off option works', (t) => {
const card = new RGBLightCard();
card.setConfig({ entity: 'light.example', colors: [] });
card.hass = { states: { 'light.example': { state: 'off' } } };
Expand All @@ -125,3 +125,32 @@ test('Hide when off option works ', (t) => {
card.hass = { states: { 'light.example': { state: 'off' } } };
t.false(card.content.classList.contains('hidden')); // Not hidden
});

test('Card is added to window.customCards', (t) => {
new RGBLightCard();
t.is(window.customCards.length, 1);
t.deepEqual(window.customCards[0], {
type: 'rgb-light-card',
name: 'RGB Light Card',
description: 'A custom card for RGB lights',
preview: true,
});
});

test('Card has a stub config', (t) => {
let conf = RGBLightCard.getStubConfig({ states: {} });
t.is(conf.entities[1].entity, 'light.example_light');

conf = RGBLightCard.getStubConfig({
states: {
'plop.light_1': { attributes: { supported_color_modes: ['hs'] }, entity_id: 'plop.light_1' },
'light.light_2': { attributes: { supported_color_modes: ['onoff'] }, entity_id: 'light.light_2' },
'light.light_3': { attributes: { supported_color_modes: ['onoff', 'rgb'] }, entity_id: 'light.light_3' },
},
});
t.is(conf.entities.length, 2);
t.is(conf.entities[0].entity, 'light.light_3');
t.is(conf.entities[1].entity, 'light.light_3');
t.is(conf.entities[1].type, 'custom:rgb-light-card');
t.is(conf.entities[1].colors.length, 4);
});

0 comments on commit 5819e74

Please sign in to comment.