Skip to content

Commit

Permalink
Improve callback assert to ensure a error class
Browse files Browse the repository at this point in the history
  • Loading branch information
diogotorres97 authored and afsampaio committed Oct 30, 2020
1 parent 6116f71 commit 657b8d6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
12 changes: 8 additions & 4 deletions src/asserts/callback-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,20 @@ module.exports = function(fn, customClass) {
* Class name.
*/

this.__class__ = customClass || 'Callback';

if (!_.isFunction(fn)) {
throw new Error('Callback must be instantiated with a function');
if (_.isNil(customClass)) {
throw new Error('Callback must be instantiated with a custom class');
}

this.__class__ = customClass;

/**
* Fn.
*/

if (!_.isFunction(fn)) {
throw new Error('Callback must be instantiated with a function');
}

this.fn = fn;

/**
Expand Down
43 changes: 24 additions & 19 deletions test/asserts/callback-assert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,25 @@ const Assert = BaseAssert.extend({
*/

describe('CallbackAssert', () => {
it('should fail if `customClass` is missing', () => {
try {
Assert.callback(value => value === 'foobiz').validate('foobar');
} catch (e) {
expect(e.message).toEqual('Callback must be instantiated with a custom class');
}
});

it('should fail if `customClass` is `null`', () => {
try {
Assert.callback(value => value === 'foobiz', null).validate('foobar');
} catch (e) {
expect(e.message).toEqual('Callback must be instantiated with a custom class');
}
});

it('should throw an error if `value` is missing', () => {
try {
Assert.callback().validate();
Assert.callback(null, 'CustomClass').validate();

fail();
} catch (e) {
Expand All @@ -32,7 +48,7 @@ describe('CallbackAssert', () => {

it('should throw an error if `value` is not a function', () => {
try {
Assert.callback().validate('foobar');
Assert.callback(null, 'CustomClass').validate('foobar');

fail();
} catch (e) {
Expand All @@ -43,10 +59,10 @@ describe('CallbackAssert', () => {
it('should throw an error if the given function is invalid', () => {
try {
// eslint-disable-next-line no-undef
Assert.callback(() => thisFunctionDoesNotExist()).validate('foobar');
Assert.callback(() => thisFunctionDoesNotExist(), 'CustomClass').validate('foobar');
} catch (e) {
expect(e).toBeInstanceOf(Violation);
expect(e.show().assert).toEqual('Callback');
expect(e.show().assert).toEqual('CustomClass');
expect(e.show().value).toEqual('foobar');
expect(e.show().violation).not.toBeUndefined();
expect(e.show().violation.error).toBeInstanceOf(ReferenceError);
Expand All @@ -56,35 +72,24 @@ describe('CallbackAssert', () => {

it('should throw an error if the callback function returns `false`', () => {
try {
Assert.callback(value => value === 'foobiz').validate('foobar');
Assert.callback(value => value === 'foobiz', 'CustomClass').validate('foobar');
} catch (e) {
expect(e).toBeInstanceOf(Violation);
expect(e.show().assert).toEqual('Callback');
expect(e.show().assert).toEqual('CustomClass');
expect(e.show().value).toEqual('foobar');
expect(e.show().violation.result).toBeFalsy();
}
});

it('should expose `assert` equal to `Callback`', () => {
try {
Assert.callback(value => value === 'foobiz').validate('foobar');
} catch (e) {
expect(e.show().assert).toEqual('Callback');
}
});

it('should have a `class` option and expose it as `assert`', () => {
it('should expose `assert` equal to `CustomClass`', () => {
try {
Assert.callback(value => value === 'foobiz', 'CustomClass').validate('foobar');
} catch (e) {
expect(e).toBeInstanceOf(Violation);
expect(e.show().assert).toEqual('CustomClass');
expect(e.show().value).toEqual('foobar');
expect(e.show().violation.result).toBeFalsy();
}
});

it('should not throw an error if the callback function returns `true`', () => {
Assert.callback(value => value === 'foobar').validate('foobar');
Assert.callback(value => value === 'foobar', 'CustomClass').validate('foobar');
});
});

0 comments on commit 657b8d6

Please sign in to comment.