Skip to content

Commit

Permalink
add: Null input value and more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EmersonRabelo committed Feb 9, 2024
1 parent 69cb5fa commit f726e2d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/lib/util/assertString.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default function assertString(input) {
if (input === undefined) throw new TypeError(`Expected a string but received a ${input}`);
if (input === undefined || input === null) throw new TypeError(`Expected a string but received a ${input}`);
if (input.constructor.name !== 'String') throw new TypeError(`Expected a string but received a ${input.constructor.name}`);
}
32 changes: 24 additions & 8 deletions test/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,43 @@ describe('Util', () => {
});

describe('assertString', () => {
it('Should throw an error if no argument is provided', () => {
it('Should throw an error if no argument is provided, undefined', () => {
assert.throws(() => { assertString(); }, TypeError);
});

it('Should throw an error if the argument is not a string, number', () => {
assert.throws(() => { assertString(123); }, TypeError);
it('Should throw an error if argument provided is a null', () => {
assert.throws(() => { assertString(null); }, TypeError);
});

it('Should throw an error if the argument is not a string, Object', () => {
it('Should throw an error if argument provided is a Boolean', () => {
assert.throws(() => { assertString(true); }, TypeError);
});

it('Should throw an error if argument provided is a Date', () => {
assert.throws(() => { assertString(new Date()); }, TypeError);
});

it('Should throw an error if argument provided is a Number(NaN)', () => {
assert.throws(() => { assertString(NaN); }, TypeError);
});

it('Should throw an error if argument provided is a Number', () => {
assert.throws(() => { assertString(2024); }, TypeError);
});

it('Should throw an error if argument provided is an Object', () => {
assert.throws(() => { assertString({}); }, TypeError);
});

it('Should throw an error if the argument is not a string, Array', () => {
it('Should throw an error if argument provided is an Array', () => {
assert.throws(() => { assertString([]); }, TypeError);
});

it('Should not throw an error if the argument is a string', () => {
it('Should not throw an error if the argument is an empty string', () => {
assert.doesNotThrow(() => { assertString(''); });
});

it('Should not throw an error if the argument is a string', () => {
assert.doesNotThrow(() => { assertString('testing'); });
it('Should not throw an error if the argument is a String', () => {
assert.doesNotThrow(() => { assertString('antidisestablishmentarianism'); });
});
});

0 comments on commit f726e2d

Please sign in to comment.