Skip to content

Commit

Permalink
Improvement: Add autoStart validation
Browse files Browse the repository at this point in the history
  • Loading branch information
a179346 committed Oct 1, 2022
1 parent d4653fa commit b7e6c3f
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ export function validateOptions(val: unknown): asserts val is AsyncIntervalJobOp
if (typeof val === 'undefined') return;
if (typeof val !== 'object' || val === null)
throw new TypeError('Invalid "options". must be an object');
if ('autoStart' in val) {
const autoStart = (val as { autoStart: unknown }).autoStart;
if (typeof autoStart !== 'boolean' && typeof autoStart !== 'undefined')
throw new TypeError('Invalid "options.autoStart". must be a boolean');
}
if ('runImmediately' in val) {
const runImmediately = (val as { runImmediately: unknown }).runImmediately;
if (typeof runImmediately !== 'boolean' && typeof runImmediately !== 'undefined')
Expand Down
96 changes: 95 additions & 1 deletion src/test/validateOptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@ describe('[validateOptions]', () => {
});
});

describe('autoStart:', () => {
it('Input value.autoStart is true', () => {
checkShouldSuccess({
autoStart: true,
});
});
it('Input value.autoStart is false', () => {
checkShouldSuccess({
autoStart: false,
});
});
it('Input value.autoStart is undefined', () => {
checkShouldSuccess({
autoStart: undefined,
});
});
});

describe('runImmediately:', () => {
it('Input value.runImmediately is true', () => {
checkShouldSuccess({
Expand All @@ -34,7 +52,7 @@ describe('[validateOptions]', () => {
});
});

describe('runImmediately:', () => {
describe('stopOnError:', () => {
it('Input value.stopOnError is true', () => {
checkShouldSuccess({
stopOnError: true,
Expand Down Expand Up @@ -130,6 +148,82 @@ describe('[validateOptions]', () => {
});
});

describe('autoStart', () => {
function checkShouldFail(val: unknown) {
expect(() => validateOptions({ autoStart: val })).to.throw(
TypeError,
'Invalid "options.autoStart". must be a boolean'
);
}

/**
* Number
*/
it('Input value is Number (0)', () => {
checkShouldFail(0);
});
it('Input value is Number (NaN)', () => {
checkShouldFail(NaN);
});
it('Input value is Number (Infinity)', () => {
checkShouldFail(Infinity);
});
it('Input value is Number (-1)', () => {
checkShouldFail(-1);
});
it('Input value is Number (1)', () => {
checkShouldFail(1);
});
it('Input value is Number (0.1)', () => {
checkShouldFail(0.1);
});
it('Input value is Number (-0.1)', () => {
checkShouldFail(-0.1);
});

/**
* null
*/
it('Input value is null', () => {
checkShouldFail(null);
});

/**
* object
*/
it('Input value is object', () => {
checkShouldFail({});
});

/**
* function
*/
it('Input value is function', () => {
checkShouldFail(() => {
// pass
});
});

/**
* string
*/
it('Input value is string ("")', () => {
checkShouldFail('');
});
it('Input value is string ("1")', () => {
checkShouldFail('1');
});
it('Input value is string ("10")', () => {
checkShouldFail('10');
});
it('Input value is string ("a")', () => {
checkShouldFail('a');
});
it('Input value is string ("true")', () => {
checkShouldFail('true');
});
});

describe('runImmediately', () => {
function checkShouldFail(val: unknown) {
expect(() => validateOptions({ runImmediately: val })).to.throw(
Expand Down

0 comments on commit b7e6c3f

Please sign in to comment.