Skip to content

Commit

Permalink
Unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TheJaredWilcurt committed Dec 24, 2023
1 parent a3ad5d8 commit 8dd782f
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 8 deletions.
2 changes: 1 addition & 1 deletion index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const nwSplasherAutoUpdate = {
* new version is available. Launches a window pointed
* to the latest version.
*
* @param {OPTIONS} options The user's options object
* @param {OPTIONS} options The user's options object
*/
downloadLatestAppAndOpenWindowInBackground: async function (options) {
// Validate options
Expand Down
3 changes: 3 additions & 0 deletions src/constants.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const path = require('path');

const LIBRARY_NAME = 'nw-splasher-auto-update';

const LIBRARY_LOG_PREFIX = '_________________________\nNW-Splasher-Auto-Update:\n';

const DEFAULT_CLOSE_SPLASH_AFTER = 3000;
const DEFAULT_PORT_NUMBER = 4443;

Expand All @@ -21,6 +23,7 @@ module.exports = {
DEFAULT_DOWNLOAD_RETRIES,
DEFAULT_EXTRACT_RETRIES,
DEFAULT_PORT_NUMBER,
LIBRARY_LOG_PREFIX,
LIBRARY_NAME,
STORAGE_LOCATION
};
17 changes: 11 additions & 6 deletions src/helpers.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
* @file File contains helper functions used by different files in the library.
*/

const { OPTIONS } = require('../api-type-definitions.cjs');

const { LIBRARY_LOG_PREFIX } = require('./constants.cjs');

const helpers = {
/**
* Helper function for human readable logging. Calls customLogger
Expand All @@ -14,25 +18,26 @@ const helpers = {
* @example
* throwError(options, 'Message', err);
*
* @param {object} options The user's options containing verbose and customLogger settings
* @param {string} message The text to be logged
* @param {object} error Optional object with additional details
* @param {OPTIONS} options The user's options containing verbose and customLogger settings
* @param {string} message The text to be logged
* @param {object} error Optional object with additional details
*/
throwError: function (options, message, error) {
const libraryString = '_________________________\nNW-Splasher-Auto-Update:\n';
options = options || {};

if (options.verbose && options.customLogger && error) {
options.customLogger(message, error);
} else if (options.verbose && options.customLogger) {
options.customLogger(message);
} else if (options.verbose && error) {
console.error(
libraryString,
LIBRARY_LOG_PREFIX,
message,
error
);
} else if (options.verbose) {
console.error(
libraryString,
LIBRARY_LOG_PREFIX,
message
);
}
Expand Down
32 changes: 32 additions & 0 deletions tests/api-type-definitions.test.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

/**
* @file A stub test file to give coverage to the JSDocs types file.
*/

const {
CUSTOMLOGGER,
OPTIONS,
SPLASHER,
AUTOUPDATE,
NEWWINDOW
} = require('../api-type-definitions.cjs');

describe('api-type-definitions.cjs', () => {
test('Stub for coverage', () => {
expect(CUSTOMLOGGER)
.toEqual(undefined);

expect(OPTIONS)
.toEqual(undefined);

expect(SPLASHER)
.toEqual(undefined);

expect(AUTOUPDATE)
.toEqual(undefined);

expect(NEWWINDOW)
.toEqual(undefined);
});
});
100 changes: 100 additions & 0 deletions tests/src/helpers.test.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
'use strict';

/**
* @file Unit tests for helper functions.
*/

const { LIBRARY_LOG_PREFIX } = require('../../src/constants.cjs');
const helpers = require('../../src/helpers.cjs');

describe('helpers.cjs', () => {
let customLogger;
let options;
const consoleError = console.error;

beforeEach(() => {
console.error = vi.fn();
customLogger = vi.fn();
options = {
verbose: true,
customLogger
};
});

afterEach(() => {
vi.clearAllMocks();
console.error = consoleError;
});

describe('throwError', () => {
const message = 'This is the message';
const error = new Error('This is the error');

test('Does nothing if nothing passed in', () => {
helpers.throwError();

expect(options.customLogger)
.not.toHaveBeenCalled();

expect(console.error)
.not.toHaveBeenCalled();
});

test('Calls customLogger with message and error', () => {
helpers.throwError(options, message, error);

expect(options.customLogger)
.toHaveBeenCalledWith(message, error);

expect(console.error)
.not.toHaveBeenCalled();
});

test('Calls customLogger with just message', () => {
helpers.throwError(options, message);

expect(options.customLogger)
.toHaveBeenCalledWith(message);

expect(console.error)
.not.toHaveBeenCalled();
});

test('Calls console.error with message and error', () => {
delete options.customLogger;
helpers.throwError(options, message, error);

expect(console.error)
.toHaveBeenCalledWith(LIBRARY_LOG_PREFIX, message, error);

expect(options.customLogger)
.toEqual(undefined);
});

test('Calls console.error with just message', () => {
delete options.customLogger;
helpers.throwError(options, message);

expect(console.error)
.toHaveBeenCalledWith(LIBRARY_LOG_PREFIX, message);

expect(options.customLogger)
.toEqual(undefined);
});
});

describe('requiredFunctionMissing', () => {
test('Throws error when called', () => {
let message;

try {
message = helpers.requiredFunctionMissing();
} catch (error) {
message = error;
}

expect(message)
.toEqual('ERROR: Required function missing.');
});
});
});
2 changes: 1 addition & 1 deletion tests/src/validation.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
const helpers = require('../../src/helpers.cjs');
const validation = require('../../src/validation.cjs');

describe('validation.js', () => {
describe('validation.cjs', () => {
let customLogger;
let options;
const consoleError = console.error;
Expand Down

0 comments on commit 8dd782f

Please sign in to comment.