mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-07-17 16:38:18 +00:00
test: coverage
This commit is contained in:
parent
e4ff4a78cc
commit
943bdecd3e
6 changed files with 271 additions and 5 deletions
|
|
@ -1,7 +1,16 @@
|
|||
import test from 'supertape';
|
||||
import {test, stub} from 'supertape';
|
||||
import {create} from 'auto-globals';
|
||||
import {tryCatch} from 'try-catch';
|
||||
import {isContainClass} from './dom-tree.js';
|
||||
import {
|
||||
isContainClass,
|
||||
getByTag,
|
||||
getById,
|
||||
getByClass,
|
||||
getByDataName,
|
||||
getByClassAll,
|
||||
hide,
|
||||
show,
|
||||
} from './dom-tree.js';
|
||||
|
||||
test('dom: isContainClass: no element', (t) => {
|
||||
const [e] = tryCatch(isContainClass);
|
||||
|
|
@ -38,3 +47,93 @@ test('dom: isContainClass: contains: array', (t) => {
|
|||
t.calledWith(contains, [className], 'should call contains');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('dom: getByTag', (t) => {
|
||||
const getElementsByTagName = stub();
|
||||
const element = {getElementsByTagName};
|
||||
|
||||
getByTag('div', element);
|
||||
|
||||
t.calledWith(getElementsByTagName, ['div'], 'should call getElementsByTagName');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('dom: getById', (t) => {
|
||||
const querySelector = stub();
|
||||
const element = {querySelector};
|
||||
|
||||
getById('my-id', element);
|
||||
|
||||
t.calledWith(querySelector, ['#my-id'], 'should call querySelector with id selector');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('dom: getByClassAll', (t) => {
|
||||
const getElementsByClassName = stub();
|
||||
const element = {getElementsByClassName};
|
||||
|
||||
getByClassAll('my-class', element);
|
||||
|
||||
t.calledWith(getElementsByClassName, ['my-class'], 'should call getElementsByClassName');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('dom: getByClass: calls getByClassAll', (t) => {
|
||||
const element = {
|
||||
getElementsByClassName: stub().returns(['first']),
|
||||
};
|
||||
|
||||
const result = getByClass('my-class', element);
|
||||
|
||||
t.equal(result, 'first', 'should return first element from class list');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('dom: getByDataName', (t) => {
|
||||
const querySelector = stub();
|
||||
const element = {querySelector};
|
||||
|
||||
getByDataName('hello', element);
|
||||
|
||||
t.calledWith(querySelector, ['[data-name="hello"]'], 'should call querySelector with data-name selector');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('dom: hide', (t) => {
|
||||
const add = stub();
|
||||
const element = {
|
||||
classList: {add},
|
||||
};
|
||||
|
||||
hide(element);
|
||||
|
||||
t.calledWith(add, ['hidden'], 'should add hidden class');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('dom: show', (t) => {
|
||||
const remove = stub();
|
||||
const element = {
|
||||
classList: {remove},
|
||||
};
|
||||
|
||||
show(element);
|
||||
|
||||
t.calledWith(remove, ['hidden'], 'should remove hidden class');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('dom: getByClassAll: without element uses document', (t) => {
|
||||
const getElementsByClassName = stub();
|
||||
const prevDocument = globalThis.document;
|
||||
|
||||
globalThis.document = {getElementsByClassName};
|
||||
|
||||
getByClassAll('my-class');
|
||||
|
||||
globalThis.document = prevDocument;
|
||||
|
||||
t.calledWith(getElementsByClassName, ['my-class'], 'should fallback to document when no element');
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import {test} from 'supertape';
|
||||
import {convert} from './input.js';
|
||||
import {test, stub} from 'supertape';
|
||||
import {convert, getName, getValue, setValue} from './input.js';
|
||||
|
||||
test('cloudcmd: client: config: input: convert', (t) => {
|
||||
const result = convert({
|
||||
|
|
@ -13,3 +13,106 @@ test('cloudcmd: client: config: input: convert', (t) => {
|
|||
t.deepEqual(result, expected);
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('cloudcmd: client: config: input: convert: bool', (t) => {
|
||||
const result = convert({
|
||||
auth: true,
|
||||
});
|
||||
|
||||
const expected = {
|
||||
auth: ' checked',
|
||||
};
|
||||
|
||||
t.deepEqual(result, expected);
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('cloudcmd: client: config: input: convert: bool false', (t) => {
|
||||
const result = convert({
|
||||
auth: false,
|
||||
});
|
||||
|
||||
const expected = {
|
||||
auth: '',
|
||||
};
|
||||
|
||||
t.deepEqual(result, expected);
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('cloudcmd: client: config: input: getName', (t) => {
|
||||
const getAttribute = stub().returns('js-hello');
|
||||
const element = {getAttribute};
|
||||
|
||||
const result = getName(element);
|
||||
|
||||
t.equal(result, 'hello', 'should strip js- prefix');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('cloudcmd: client: config: input: getValue: checkbox', (t) => {
|
||||
const querySelector = stub().returns({
|
||||
type: 'checkbox',
|
||||
checked: true,
|
||||
});
|
||||
const element = {querySelector};
|
||||
|
||||
const result = getValue('auth', element);
|
||||
|
||||
t.ok(result, 'should return checked value');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('cloudcmd: client: config: input: getValue: number', (t) => {
|
||||
const querySelector = stub().returns({
|
||||
type: 'number',
|
||||
value: '42',
|
||||
});
|
||||
const element = {querySelector};
|
||||
|
||||
const result = getValue('port', element);
|
||||
|
||||
t.equal(result, 42, 'should return number');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('cloudcmd: client: config: input: getValue: default', (t) => {
|
||||
const querySelector = stub().returns({
|
||||
type: 'text',
|
||||
value: 'hello',
|
||||
});
|
||||
const element = {querySelector};
|
||||
|
||||
const result = getValue('name', element);
|
||||
|
||||
t.equal(result, 'hello', 'should return value as is');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('cloudcmd: client: config: input: setValue: checkbox', (t) => {
|
||||
const el = {
|
||||
type: 'checkbox',
|
||||
checked: false,
|
||||
};
|
||||
const querySelector = stub().returns(el);
|
||||
const element = {querySelector};
|
||||
|
||||
setValue('auth', true, element);
|
||||
|
||||
t.ok(el.checked, 'should set checked');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('cloudcmd: client: config: input: setValue: default', (t) => {
|
||||
const el = {
|
||||
type: 'text',
|
||||
value: 'old',
|
||||
};
|
||||
const querySelector = stub().returns(el);
|
||||
const element = {querySelector};
|
||||
|
||||
setValue('name', 'new', element);
|
||||
|
||||
t.equal(el.value, 'new', 'should set value');
|
||||
t.end();
|
||||
});
|
||||
|
|
|
|||
|
|
@ -65,7 +65,7 @@ test('sw: register: registerSW: http', async (t, {location, navigator}) => {
|
|||
|
||||
test('sw: register: registerSW: https self-signed', async (t, {location, navigator}) => {
|
||||
Object.assign(location, {
|
||||
protocol: 'https',
|
||||
protocol: 'https:',
|
||||
hostname: 'self-signed.badssl.com',
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -32,3 +32,34 @@ test('distribute: log: config', (t) => {
|
|||
}, {
|
||||
checkAssertionsCount: false,
|
||||
});
|
||||
|
||||
|
||||
test('distribute: log: stringToRGB', (t) => {
|
||||
const result = log.stringToRGB('abc');
|
||||
|
||||
t.deepEqual(result, [97, 3, 294], 'should return [charCode, length, crc]');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('distribute: log: makeColor', (t) => {
|
||||
const result = log.makeColor('hello');
|
||||
|
||||
t.ok(result.includes('hello'), 'should return colored string containing the input');
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
||||
test('distribute: log: getDescription', (t) => {
|
||||
const message = 'some error';
|
||||
const result = log.getDescription({
|
||||
message,
|
||||
});
|
||||
|
||||
t.equal(result, message, 'should return message from error object');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('distribute: log: connectedStr', (t) => {
|
||||
t.ok(log.connectedStr, 'should have connectedStr');
|
||||
t.end();
|
||||
});
|
||||
|
|
|
|||
|
|
@ -54,3 +54,17 @@ test('cloudcmd: server: env: bool: number: 0', (t) => {
|
|||
t.notOk(result);
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('cloudcmd: server: env: bool: zero uppercase', (t) => {
|
||||
const {CLOUDCMD_TERMINAL} = process.env;
|
||||
|
||||
process.env.CLOUDCMD_TERMINAL = '0';
|
||||
|
||||
const result = env.bool('terminal');
|
||||
|
||||
process.env.CLOUDCMD_TERMINAL = CLOUDCMD_TERMINAL;
|
||||
|
||||
t.notOk(result);
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -30,3 +30,22 @@ test('themes: no args', (t) => {
|
|||
t.match(themes.light, css);
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('themes: production', (t) => {
|
||||
const themes = getThemes({
|
||||
isDev: false,
|
||||
});
|
||||
|
||||
t.ok(themes.dark, 'should have dark theme');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('themes: production: light', (t) => {
|
||||
const themes = getThemes({
|
||||
isDev: false,
|
||||
});
|
||||
|
||||
t.ok(themes.light, 'should have light theme');
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue