Skip to content

Commit 657b8d6

Browse files
diogotorres97afsampaio
authored andcommitted
Improve callback assert to ensure a error class
1 parent 6116f71 commit 657b8d6

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

src/asserts/callback-assert.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,20 @@ module.exports = function(fn, customClass) {
1616
* Class name.
1717
*/
1818

19-
this.__class__ = customClass || 'Callback';
20-
21-
if (!_.isFunction(fn)) {
22-
throw new Error('Callback must be instantiated with a function');
19+
if (_.isNil(customClass)) {
20+
throw new Error('Callback must be instantiated with a custom class');
2321
}
2422

23+
this.__class__ = customClass;
24+
2525
/**
2626
* Fn.
2727
*/
2828

29+
if (!_.isFunction(fn)) {
30+
throw new Error('Callback must be instantiated with a function');
31+
}
32+
2933
this.fn = fn;
3034

3135
/**

test/asserts/callback-assert.test.js

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,25 @@ const Assert = BaseAssert.extend({
2020
*/
2121

2222
describe('CallbackAssert', () => {
23+
it('should fail if `customClass` is missing', () => {
24+
try {
25+
Assert.callback(value => value === 'foobiz').validate('foobar');
26+
} catch (e) {
27+
expect(e.message).toEqual('Callback must be instantiated with a custom class');
28+
}
29+
});
30+
31+
it('should fail if `customClass` is `null`', () => {
32+
try {
33+
Assert.callback(value => value === 'foobiz', null).validate('foobar');
34+
} catch (e) {
35+
expect(e.message).toEqual('Callback must be instantiated with a custom class');
36+
}
37+
});
38+
2339
it('should throw an error if `value` is missing', () => {
2440
try {
25-
Assert.callback().validate();
41+
Assert.callback(null, 'CustomClass').validate();
2642

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

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

3753
fail();
3854
} catch (e) {
@@ -43,10 +59,10 @@ describe('CallbackAssert', () => {
4359
it('should throw an error if the given function is invalid', () => {
4460
try {
4561
// eslint-disable-next-line no-undef
46-
Assert.callback(() => thisFunctionDoesNotExist()).validate('foobar');
62+
Assert.callback(() => thisFunctionDoesNotExist(), 'CustomClass').validate('foobar');
4763
} catch (e) {
4864
expect(e).toBeInstanceOf(Violation);
49-
expect(e.show().assert).toEqual('Callback');
65+
expect(e.show().assert).toEqual('CustomClass');
5066
expect(e.show().value).toEqual('foobar');
5167
expect(e.show().violation).not.toBeUndefined();
5268
expect(e.show().violation.error).toBeInstanceOf(ReferenceError);
@@ -56,35 +72,24 @@ describe('CallbackAssert', () => {
5672

5773
it('should throw an error if the callback function returns `false`', () => {
5874
try {
59-
Assert.callback(value => value === 'foobiz').validate('foobar');
75+
Assert.callback(value => value === 'foobiz', 'CustomClass').validate('foobar');
6076
} catch (e) {
6177
expect(e).toBeInstanceOf(Violation);
62-
expect(e.show().assert).toEqual('Callback');
78+
expect(e.show().assert).toEqual('CustomClass');
6379
expect(e.show().value).toEqual('foobar');
6480
expect(e.show().violation.result).toBeFalsy();
6581
}
6682
});
6783

68-
it('should expose `assert` equal to `Callback`', () => {
69-
try {
70-
Assert.callback(value => value === 'foobiz').validate('foobar');
71-
} catch (e) {
72-
expect(e.show().assert).toEqual('Callback');
73-
}
74-
});
75-
76-
it('should have a `class` option and expose it as `assert`', () => {
84+
it('should expose `assert` equal to `CustomClass`', () => {
7785
try {
7886
Assert.callback(value => value === 'foobiz', 'CustomClass').validate('foobar');
7987
} catch (e) {
80-
expect(e).toBeInstanceOf(Violation);
8188
expect(e.show().assert).toEqual('CustomClass');
82-
expect(e.show().value).toEqual('foobar');
83-
expect(e.show().violation.result).toBeFalsy();
8489
}
8590
});
8691

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

0 commit comments

Comments
 (0)