mirror of
https://github.com/GeorgeSG/lovelace-time-picker-card.git
synced 2025-12-28 21:00:29 +00:00
Add some tests
This commit is contained in:
@@ -15,5 +15,5 @@
|
||||
"@typescript-eslint/no-non-null-assertion": 0,
|
||||
"@typescript-eslint/explicit-function-return-type": [1, { "allowExpressions": true }]
|
||||
},
|
||||
"ignorePatterns": ["dist/", "node_modules/"]
|
||||
"ignorePatterns": ["dist/", "node_modules/", "*.js"]
|
||||
}
|
||||
|
||||
37
karma.conf.js
Normal file
37
karma.conf.js
Normal file
@@ -0,0 +1,37 @@
|
||||
module.exports = function (config) {
|
||||
config.set({
|
||||
basePath: 'test/',
|
||||
frameworks: ['mocha', 'chai'],
|
||||
files: ['*.test.ts'],
|
||||
preprocessors: {
|
||||
'*.test.ts': ['rollup'],
|
||||
},
|
||||
|
||||
rollupPreprocessor: {
|
||||
plugins: [
|
||||
require('@rollup/plugin-node-resolve')(),
|
||||
require('@rollup/plugin-json')(),
|
||||
require('@rollup/plugin-typescript')(),
|
||||
require('rollup-plugin-babel')(),
|
||||
],
|
||||
output: {
|
||||
format: 'umd',
|
||||
sourcemap: 'inline',
|
||||
},
|
||||
},
|
||||
|
||||
// possible values: 'dots', 'progress'
|
||||
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
|
||||
reporters: ['progress'],
|
||||
|
||||
port: 9876,
|
||||
colors: true,
|
||||
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
|
||||
logLevel: config.LOG_INFO,
|
||||
autoWatch: false,
|
||||
singleRun: true,
|
||||
|
||||
browsers: ['ChromeHeadless'],
|
||||
concurrency: Infinity,
|
||||
});
|
||||
};
|
||||
2136
package-lock.json
generated
2136
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
10
package.json
10
package.json
@@ -19,6 +19,7 @@
|
||||
"build": "npm run lint && npm run rollup",
|
||||
"lint": "eslint src/*.ts",
|
||||
"rollup": "rollup -c",
|
||||
"test": "./node_modules/karma/bin/karma start",
|
||||
"release-patch": "npm version patch && git push origin master --tags",
|
||||
"release-minor": "npm version minor && git push origin master --tags",
|
||||
"release-major": "npm version major && git push origin master --tags"
|
||||
@@ -31,19 +32,28 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.9.0",
|
||||
"@open-wc/testing": "^2.5.16",
|
||||
"@rollup/plugin-json": "^4.0.3",
|
||||
"@rollup/plugin-node-resolve": "^7.1.1",
|
||||
"@rollup/plugin-typescript": "^4.0.0",
|
||||
"@types/chai": "^4.2.11",
|
||||
"@typescript-eslint/eslint-plugin": "^2.30.0",
|
||||
"@typescript-eslint/parser": "^2.30.0",
|
||||
"eslint": "^6.8.0",
|
||||
"eslint-config-prettier": "^6.11.0",
|
||||
"eslint-plugin-prettier": "^3.1.3",
|
||||
"karma": "^5.0.5",
|
||||
"karma-chai": "^0.1.0",
|
||||
"karma-chrome-launcher": "^3.1.0",
|
||||
"karma-mocha": "^2.0.1",
|
||||
"karma-rollup-preprocessor": "^7.0.5",
|
||||
"mocha": "^7.1.2",
|
||||
"prettier": "^2.0.5",
|
||||
"rollup": "^1.32.1",
|
||||
"rollup-plugin-babel": "^4.4.0",
|
||||
"rollup-plugin-serve": "^1.0.1",
|
||||
"rollup-plugin-terser": "^5.3.0",
|
||||
"sinon": "^9.0.2",
|
||||
"typescript": "^3.8.3"
|
||||
}
|
||||
}
|
||||
|
||||
17
test/test-error-card.ts
Normal file
17
test/test-error-card.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { LovelaceCard } from 'custom-card-helpers';
|
||||
import { customElement, html, LitElement, TemplateResult } from 'lit-element';
|
||||
|
||||
@customElement('hui-error-card')
|
||||
export class HuiErrorCard extends LitElement implements LovelaceCard {
|
||||
setConfig(): void {
|
||||
return;
|
||||
}
|
||||
|
||||
render(): TemplateResult {
|
||||
return html``;
|
||||
}
|
||||
|
||||
getCardSize(): number {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
111
test/time-picker-card.test.ts
Normal file
111
test/time-picker-card.test.ts
Normal file
@@ -0,0 +1,111 @@
|
||||
import { expect, fixture, html } from '@open-wc/testing';
|
||||
import { HomeAssistant } from 'custom-card-helpers';
|
||||
import { HassEntity } from 'home-assistant-js-websocket';
|
||||
import '../src/time-picker-card';
|
||||
import { TimePickerCard } from '../src/time-picker-card';
|
||||
import { TimePickerCardConfig, HourMode } from '../src/types';
|
||||
import './test-error-card';
|
||||
|
||||
async function render(config?: {
|
||||
entityId?: string;
|
||||
friendlyName?: string;
|
||||
hasTime?: boolean;
|
||||
hourMode?: HourMode;
|
||||
hideName?: boolean;
|
||||
}): Promise<TimePickerCard> {
|
||||
const { entityId, friendlyName, hasTime, hourMode, hideName } = Object.assign(
|
||||
{
|
||||
entityId: 'input_datetime.wake_time',
|
||||
friendlyName: 'Wake Time',
|
||||
hasTime: true,
|
||||
hideName: false,
|
||||
},
|
||||
config
|
||||
);
|
||||
|
||||
const testEntity: HassEntity = {
|
||||
entity_id: entityId,
|
||||
attributes: {
|
||||
friendly_name: friendlyName,
|
||||
has_time: hasTime,
|
||||
},
|
||||
state: '00:10:00',
|
||||
last_changed: '',
|
||||
last_updated: '',
|
||||
context: {
|
||||
id: entityId,
|
||||
user_id: null,
|
||||
},
|
||||
};
|
||||
|
||||
const hass: Partial<HomeAssistant> = {
|
||||
states: {
|
||||
[entityId]: testEntity,
|
||||
},
|
||||
};
|
||||
|
||||
const testConfig: TimePickerCardConfig = {
|
||||
type: 'custom:time-picker-card',
|
||||
entity: entityId,
|
||||
hour_mode: hourMode,
|
||||
hide: {
|
||||
name: hideName,
|
||||
},
|
||||
};
|
||||
|
||||
const card = await fixture<TimePickerCard>(
|
||||
html`<time-picker-card .hass=${hass} .config=${testConfig}></time-picker-card>`
|
||||
);
|
||||
|
||||
card.setConfig(testConfig);
|
||||
|
||||
return card;
|
||||
}
|
||||
|
||||
function errorCard(): string {
|
||||
return html`<hui-error-card></hui-error-card>`.getHTML();
|
||||
}
|
||||
|
||||
describe('test', () => {
|
||||
it('renders', async () => {
|
||||
const card = await render();
|
||||
expect(card).shadowDom.to.match('time-picker-card');
|
||||
});
|
||||
|
||||
it('errors if entity is not an input_datetime', async () => {
|
||||
const card = await render({ entityId: 'sensor.test_sensor' });
|
||||
expect(card).shadowDom.to.equal(errorCard());
|
||||
});
|
||||
|
||||
it("errors if entity doesn't have time", async () => {
|
||||
const card = await render({ hasTime: false });
|
||||
expect(card).shadowDom.to.equal(errorCard());
|
||||
});
|
||||
|
||||
it('renders card name', async () => {
|
||||
const card = await render();
|
||||
expect(card.shadowRoot?.innerHTML).to.match(/Wake Time/);
|
||||
});
|
||||
|
||||
it('hides card name', async () => {
|
||||
const card = await render({ hideName: true });
|
||||
expect(card.shadowRoot?.innerHTML).not.to.match(/Wake Time/);
|
||||
});
|
||||
|
||||
it('renders time units', async () => {
|
||||
const card = await render();
|
||||
expect(card.shadowRoot?.innerHTML.match(/<time-unit>/g)!.length).to.equal(2);
|
||||
});
|
||||
|
||||
describe('time-period', () => {
|
||||
it('is not rendered by default', async () => {
|
||||
const card = await render();
|
||||
expect(card.shadowRoot?.innerHTML.match(/<time-period>/g)).to.be.null;
|
||||
});
|
||||
|
||||
it('renders in 12-hour mode', async () => {
|
||||
const card = await render({ hourMode: 12 });
|
||||
expect(card.shadowRoot?.innerHTML.match(/<time-period>/g)!.length).to.equal(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -12,6 +12,7 @@
|
||||
"noImplicitAny": false,
|
||||
"skipLibCheck": true,
|
||||
"resolveJsonModule": true,
|
||||
"experimentalDecorators": true
|
||||
"experimentalDecorators": true,
|
||||
"allowSyntheticDefaultImports": true // for importing sinon in tests
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user