Skip to content

Commit

Permalink
more getDisplay tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mhkeller committed Mar 21, 2023
1 parent cc7cdb7 commit 40933d3
Show file tree
Hide file tree
Showing 14 changed files with 196 additions and 32 deletions.
7 changes: 3 additions & 4 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
module.exports = {
extends: [
'semistandard'
],
extends: ['semistandard'],
rules: {
quotes: [
'error',
'single',
{
allowTemplateLiterals: true
}
]
],
'operator-linebreak': ['error', 'after']
}
};
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"useTabs": false,
"singleQuote": true,
"trailingComma": "none",
"printWidth": 100
}
35 changes: 23 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
"dependencies": {
"chalk": "^4.1.2",
"clean-stack": "^5.1.0",
"node-notifier": "^10.0.1",
"underscore": "^1.13.6"
"node-notifier": "^10.0.1"
},
"devDependencies": {
"eslint": "^8.36.0",
Expand All @@ -24,6 +23,7 @@
"eslint-plugin-n": "^15.6.1",
"eslint-plugin-promise": "^6.1.1",
"mocha": "^10.2.0",
"prettier": "^2.8.5",
"strip-ansi": "^7.0.1"
},
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isObject } from 'underscore';
import notifier from 'node-notifier';
import cS from 'clean-stack';
import stripAnsi from 'strip-ansi';
Expand All @@ -10,6 +9,7 @@ import constructPrefix from './lib/constructPrefix.js';
import normalize from './lib/normalize.js';
import print from './lib/print.js';
import getDisplay from './lib/getDisplay.js';
import isObject from './utils/isObject';

/**
Send a notification
Expand Down
17 changes: 10 additions & 7 deletions src/lib/getDisplay.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isObject, isBoolean } from 'underscore';
import isObject from '../utils/isObject.js';

/**
* If we are passing an object, set the entire display
Expand All @@ -9,9 +9,12 @@ function construct (val) {
if (isObject(val)) {
return val;
}
return {
messageStyle: val
};
if (val) {
return {
messageStyle: val
};
}
return {};
}

export default function getDisplay ({ baseDisplay, displays }, display, desktop) {
Expand All @@ -24,9 +27,9 @@ export default function getDisplay ({ baseDisplay, displays }, display, desktop)
result = construct(displays[display]);
} else {
result = construct(display);
if (isBoolean(desktop) === true) {
result.desktop = desktop;
}
}
if (typeof desktop === 'boolean') {
result.desktop = desktop;
}
return { ...baseDisplay, ...result };
}
6 changes: 2 additions & 4 deletions src/lib/normalize.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
/* --------------------------------------------
* Normalize what we pass as the message and value strings
*/
import { isNumber, isBoolean } from 'underscore';

export default function normalize (val) {
// Simple stringify numbers
if (isNumber(val)) {
if (typeof val === 'number') {
return String(val);
// Pretty print objects (arrays, dates, dictionaries, errors) and booleans
} else if (typeof val === 'object' || isBoolean(val)) {
} else if (typeof val === 'object' || typeof val === 'boolean') {
return JSON.stringify(val);
} else {
return val;
Expand Down
5 changes: 5 additions & 0 deletions src/utils/isObject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default function isObject (val) {
return Array.isArray(val) === false &&
typeof val === 'object' &&
val !== null;
}
52 changes: 52 additions & 0 deletions test/formatTime.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* globals describe it */
import assert from 'assert';

import fn from '../src/utils/formatTime.js';

const name = 'formatTime';

const rows = [
{ args: [0], expected: '0ms' },
{ args: [0.1 * 0.2], expected: '0ms' },
{ args: [2], expected: '2ms' },
{ args: [20], expected: '20ms' },
{ args: [6814], expected: '6.81s' },
{ args: [7565], expected: '7.56s' },
{ args: [7686], expected: '7.69s' },
{ args: [7692], expected: '7.69s' },
{ args: [7805], expected: '7.81s' },
{ args: [7944], expected: '7.94s' },
{ args: [20.4], expected: '20.4ms' },
{ args: [20.5], expected: '20.5ms' },
{ args: [20.6], expected: '20.6ms' },
{ args: [60.02000000000004], expected: '60ms' },
{ args: [60.1], expected: '60.1ms' },
{ args: [1500], expected: '1.5s' },
{ args: [1750], expected: '1.75s' },
{ args: [40000], expected: '40s' },
{ args: [40500], expected: '40.5s' },
{ args: [40750], expected: '40.75s' },
{ args: [60000], expected: '1m' },
{ args: [61000], expected: '1m 1s' },
{ args: [61750], expected: '1m 1s' },
{ args: [10 * 60 * 1000], expected: '10m' },
{ args: [60 * 1000 * 10 + 500], expected: '10m' },
{ args: [60 * 1000 * 10 + 1600], expected: '10m 1s' },
{ args: [51 * 60 * 1000 + 40 * 1000], expected: '51m 40s' },
{ args: [59 * 60 * 1000 + 59 * 1000], expected: '59m 59s' },
{ args: [60 * 60 * 1000 + 59 * 1000], expected: '1h 59s' },
{ args: [2 * 60 * 60 * 1000 + 30 * 1000 + 59 * 1000], expected: '2h 1m 29s' },
{ args: [2 * 60 * 60 * 1000 + 30 * 1000], expected: '2h 30s' },
{ args: [2 * 60 * 60 * 1000 + 300], expected: '2h 1s' },
{ args: [2 * 60 * 60 * 1000 + 1200], expected: '2h 1s' },
{ args: [2 * 60 * 60 * 1000 + 300], expected: '2h 1s' }
];

describe(name, () => {
rows.forEach(t => {
it(`should match expected ${t.args[0]}`, () => {
const actual = fn(t.args[0]);
assert.equal(actual, t.expected);
});
});
});
4 changes: 2 additions & 2 deletions test/formatTimestamp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import fn from '../src/utils/formatTimestamp.js';

const name = 'formatTimestamp';

const timesToTest = [
const rows = [
{ args: [new Date(2017, 0, 1)], expected: '00:00:00.00' },
{ args: [new Date(2017, 0, 1, 11, 33, 30, 0)], expected: '11:33:30.00' },
{ args: [new Date(2017, 0, 1, 11, 33, 30, 50)], expected: '11:33:30.05' },
Expand All @@ -21,7 +21,7 @@ const timesToTest = [
];

describe(name, () => {
timesToTest.forEach(t => {
rows.forEach((t) => {
it(`should match expected ${t.args[0]}`, () => {
const actual = fn(t.args[0]);
assert.equal(actual, t.expected);
Expand Down
19 changes: 19 additions & 0 deletions test/getProjectName.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* globals describe it */
import assert from 'assert';

import fn from '../src/lib/getProjectName.js';

const name = 'getProjectName';

const rows = [
{ args: [], expected: 'notify' }
];

describe(name, () => {
rows.forEach((t) => {
it(`should match expected`, () => {
const actual = fn(t.args[0]);
assert.equal(actual, t.expected);
});
});
});
28 changes: 28 additions & 0 deletions test/isObject.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* globals describe it */
import assert from 'assert';

import fn from '../src/utils/isObject.js';

const name = 'isObject';

const rows = [
{ args: [{}], expected: true },
{ args: [0], expected: false },
{ args: [1], expected: false },
{ args: [NaN], expected: false },
{ args: [undefined], expected: false },
{ args: [null], expected: false },
{ args: [false], expected: false },
{ args: [true], expected: false },
{ args: [Infinity], expected: false },
{ args: [-Infinity], expected: false }
];

describe(name, () => {
rows.forEach((t) => {
it(`should match expected`, () => {
const actual = fn(t.args[0]);
assert.equal(actual, t.expected);
});
});
});
43 changes: 43 additions & 0 deletions test/x_getDisplay.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* globals describe it */
import assert from 'assert';

import fn from '../src/lib/getDisplay.js';
import defaults from '../src/defaults.js';

const name = 'getDisplay';

const rows = [
// Empty
{ args: [defaults, undefined, undefined], expected: defaults.baseDisplay },

// Setting desktop
{ args: [defaults, {}, true], expected: { ...defaults.baseDisplay, desktop: true } },
{ args: [defaults, {}, false], expected: { ...defaults.baseDisplay, desktop: false } },
{ args: [defaults, {}, {}], expected: { ...defaults.baseDisplay, desktop: false } },
{ args: [defaults, {}, []], expected: { ...defaults.baseDisplay, desktop: false } },
{ args: [defaults, {}, 1], expected: { ...defaults.baseDisplay, desktop: false } },
{ args: [defaults, {}, ''], expected: { ...defaults.baseDisplay, desktop: false } },

// Setting colors
{ args: [defaults, 'red', ''], expected: { ...defaults.baseDisplay, messageStyle: 'red' } },
{ args: [defaults, 'bold', ''], expected: { ...defaults.baseDisplay, messageStyle: 'bold' } },
{ args: [defaults, ['red'], ''], expected: { ...defaults.baseDisplay, messageStyle: ['red'] } },
{ args: [defaults, ['bold'], ''], expected: { ...defaults.baseDisplay, messageStyle: ['bold'] } },
{ args: [defaults, ['bold', 'blue'], ''], expected: { ...defaults.baseDisplay, messageStyle: ['bold', 'blue'] } },

// Setting colors and desktop
{ args: [defaults, 'red', true], expected: { ...defaults.baseDisplay, messageStyle: 'red', desktop: true } },
{ args: [defaults, 'bold', false], expected: { ...defaults.baseDisplay, messageStyle: 'bold', desktop: false } },
{ args: [defaults, ['red'], {}], expected: { ...defaults.baseDisplay, messageStyle: ['red'], desktop: false } },
{ args: [defaults, ['bold'], []], expected: { ...defaults.baseDisplay, messageStyle: ['bold'], desktop: false } },
{ args: [defaults, ['bold', 'blue'], ''], expected: { ...defaults.baseDisplay, messageStyle: ['bold', 'blue'], desktop: false } },
];

describe(name, () => {
rows.forEach((t) => {
it(`should match expected ${t.args[1]}`, () => {
const actual = fn(...t.args);
assert.deepEqual(actual, t.expected);
});
});
});
Empty file added test/x_normalize.test.js
Empty file.

0 comments on commit 40933d3

Please sign in to comment.