Skip to content

Commit

Permalink
test: test deleteProperty, deleteProperties, padLeft, padRight util m…
Browse files Browse the repository at this point in the history
…ethods
  • Loading branch information
teclone committed Mar 25, 2019
1 parent 992c8c0 commit fbb4698
Showing 1 changed file with 79 additions and 1 deletion.
80 changes: 79 additions & 1 deletion tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ describe('Utils', function() {
describe('.makeArray<T>(arg: T | T[], isNullValid=false): T[]', function() {
it(`should return argument if is an array`, function() {
const arg = ['item'];
expect(Utils.makeArray(arg)).toStrictEqual(arg);
expect(Utils.makeArray(arg)).toBe(arg);
});

it(`should put argument into an array and return`, function() {
Expand All @@ -145,6 +145,56 @@ describe('Utils', function() {
});
});

describe('.makeObject<T>(arg: T | any): T | object', function() {
it(`should return argument if is an object`, function() {
const arg = ['item'];
expect(Utils.makeObject(arg)).toBe(arg);
});

it(`should return empty object if argument is not an object`, function() {
expect(Utils.makeObject(null)).toEqual({});
});
});

describe('.deleteProperty(key: string, target: object): boolean', function() {
it(`should delete the given property from the target object`, function() {
const target = {name: 'name'};
expect(Utils.deleteProperty('name', target)).toBeTruthy();
expect(target.name).toBeUndefined();
});

it(`should return false if the given property cannot be deleted because it is non
configurable`, function() {
const target = {};
Object.defineProperty(target, 'name', {
value: 'name',
configurable: false
});
expect(Utils.deleteProperty('name', target)).toBeFalsy();
expect(target['name']).toBeDefined();
});
});

describe('.deleteProperties(keys: string[], target: object): boolean', function() {
it(`should delete the given array of properties from the target object`, function() {
const target = {name: 'name'};
expect(Utils.deleteProperties(['name'], target)).toBeTruthy();
expect(target.name).toBeUndefined();
});

it(`should return false if any of the given properties cannot be deleted because it is non
configurable`, function() {
const target = {age: 22};
Object.defineProperty(target, 'name', {
value: 'name',
configurable: false
});
expect(Utils.deleteProperties(['age', 'name'], target)).toBeFalsy();
expect(target['name']).toBeDefined();
expect(target.age).toBeUndefined();
});
});

describe('.keyNotSetOrTrue(key: string, object: object): boolean', function() {
it(`should return true if key is not set in the given object`, function() {
expect(Utils.keyNotSetOrTrue('key', {})).toBeTruthy();
Expand Down Expand Up @@ -404,4 +454,32 @@ describe('Utils', function() {
expect(Utils.camelCase('my-second_string')).toEqual('mySecondString');
});
});

describe('.padLeft(target: string | number, length:number = 4, padWith: string | number = 0): string', function() {

it(`should pad the given target to the left with the given padWith value up till the
target length meets the given length`, function() {
expect(Utils.padLeft(12, 6, 0)).toEqual('000012');
expect(Utils.padLeft(12, 3, '-')).toEqual('-12');
expect(Utils.padLeft(12, 2, '-')).toEqual('12');
});

it(`should default the length to 4 and the padWith argument to 0 if not given`, function() {
expect(Utils.padLeft(12)).toEqual('0012');
});
});

describe('.padRight(target: string | number, length:number = 4, padWith: string | number = 0): string', function() {

it(`should pad the given target to the right with the given padWith value up till the
target length meets the given length`, function() {
expect(Utils.padRight(12, 6, 0)).toEqual('120000');
expect(Utils.padRight(12, 3, '-')).toEqual('12-');
expect(Utils.padRight(12, 2, '-')).toEqual('12');
});

it(`should default the length to 4 and the padWith argument to 0 if not given`, function() {
expect(Utils.padRight(12)).toEqual('1200');
});
});
});

0 comments on commit fbb4698

Please sign in to comment.