Skip to content

Commit

Permalink
test(date-helpers): enhance test coverage and remove Sentry
Browse files Browse the repository at this point in the history
  • Loading branch information
liby committed Oct 18, 2023
1 parent ce23723 commit 2861444
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 79 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "feat(date-helpers): add type assertion for `DateLike` input",
"packageName": "@rightcapital/date-helpers",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "chore: update TypeScript config",
"packageName": "@rightcapital/exceptions",
"email": "[email protected]",
"dependentChangeType": "patch"
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"build": "pnpm run clean && pnpm -r --filter=./packages/** run build",
"clean": "pnpm -r --parallel --filter=./packages/** exec tsc --build --clean",
"pretest": "pnpm run build",
"test": "jest",
"test": "pnpm run pretest && jest",
"lint": "concurrently pnpm:lint:*",
"lint:eslint": "eslint --max-warnings=0 .",
"lint:prettier": "prettier -c .",
Expand Down
70 changes: 68 additions & 2 deletions packages/date-helpers/__tests__/date-helpers.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,65 @@
import { parseISO } from 'date-fns';
import { DateHelpers } from '../src/date-helpers';
import { InvalidArgumentException } from '@rightcapital/exceptions';

const output = parseISO('2019-10-21');

describe('DateHelpers', () => {
describe('DateHelpers input validation', () => {
it('parseDateString should throw for unrecognized date formats', () => {
const invalidDateString = 'ABCDE';
expect(() => DateHelpers.parseDateString(invalidDateString)).toThrowError(
InvalidArgumentException,
);
});

it('parseDateString should throw error for null or undefined input', () => {
// @ts-expect-error - intentionally passing invalid input to test error handling
expect(() => DateHelpers.parseDateString(null)).toThrow(
'Input cannot be null or undefined.',
);

// @ts-expect-error - intentionally passing invalid input to test error handling
expect(() => DateHelpers.parseDateString(undefined)).toThrow(
'Input cannot be null or undefined.',
);
});

it('formatDateLikeToString should throw InvalidArgumentException for invalid date input', () => {
expect(() =>
DateHelpers.formatDateLikeToString(
'invalid date',
DateHelpers.isoDateFormat,
),
).toThrowError(InvalidArgumentException);
});

it('formatDateLikeToString should throw InvalidArgumentException for invalid date format', () => {
expect(() =>
DateHelpers.formatDateLikeToString(new Date(), 'invalid format'),
).toThrowError(InvalidArgumentException);
});

it('formatDateLikeToString should throw error for null, undefined, or invalid input', () => {
expect(() =>
// @ts-expect-error - intentionally passing invalid input to test error handling
DateHelpers.formatDateLikeToString(null, DateHelpers.isoDateFormat),
).toThrowError(InvalidArgumentException);
expect(() =>
// @ts-expect-error - intentionally passing invalid input to test error handling
DateHelpers.formatDateLikeToString(undefined, DateHelpers.isoDateFormat),
).toThrowError(InvalidArgumentException);
expect(() =>
// @ts-expect-error - intentionally passing invalid input to test error handling
DateHelpers.formatDateLikeToString({}, DateHelpers.isoDateFormat),
).toThrowError(InvalidArgumentException);
expect(() =>
// @ts-expect-error - intentionally passing invalid input to test error handling
DateHelpers.formatDateLikeToString(12345, DateHelpers.isoDateFormat),
).toThrowError(InvalidArgumentException);
});
});

describe('DateHelpers output validation', () => {
it('date string to Date object conversion test', () => {
expect(DateHelpers.parseDateString('10/21/19')).toStrictEqual(output);
});
Expand Down Expand Up @@ -35,12 +91,22 @@ describe('DateHelpers', () => {
).toBe('10/21/2019 00:00:00');
});

it('convertDateToMonthDayYearString should format dates correctly', () => {
it('formatDateLikeToUsLocaleMediumDateString should format dates correctly', () => {
expect(DateHelpers.formatDateLikeToUsLocaleMediumDateString(output)).toBe(
'Oct 21, 2019',
);
expect(
DateHelpers.formatDateLikeToUsLocaleMediumDateString('2019-10-21'),
).toBe('Oct 21, 2019');
});

it('formatDateLikeToString should return an empty string if the input is an empty string', () => {
const emptyString = '';
expect(
DateHelpers.formatDateLikeToString(
emptyString,
DateHelpers.isoDateFormat,
),
).toBe(emptyString);
});
});
2 changes: 0 additions & 2 deletions packages/date-helpers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,9 @@
"date-fns": "2.30.0"
},
"peerDependencies": {
"@sentry/browser": "7.x",
"date-fns": "^2.29.3"
},
"devDependencies": {
"@sentry/browser": "7.74.0",
"typedoc": "0.25.2",
"typedoc-plugin-markdown": "3.16.0"
}
Expand Down
31 changes: 13 additions & 18 deletions packages/date-helpers/src/date-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { InvalidArgumentException } from '@rightcapital/exceptions';
import { captureException, withScope, Scope } from '@sentry/browser';
import { format, isValid, parse, parseISO } from 'date-fns';

export type DateLike = Date | string;
Expand All @@ -22,21 +21,25 @@ export class DateHelpers {
/** Format for date strings in ISO format, e.g., "2023-12-31" */
public static isoDateFormat = 'yyyy-MM-dd';

private static ensureValidDateInput(input: unknown): asserts input is DateLike {
private static ensureValidDateInput(
input: unknown,
): asserts input is DateLike {
if (input === null || input === undefined) {
throw new InvalidArgumentException(`Input cannot be null or undefined. Received: ${input}`);
throw new InvalidArgumentException(
`Input cannot be null or undefined. Received: ${input}`,
);
}

if (input instanceof Date) {
return; // It's a valid Date object.
}

if (typeof input === 'string') {
return; // It's a valid string.
}

throw new InvalidArgumentException(
`Input must be a Date object or a string. Received: ${typeof input} - ${input}`
`Input must be a Date object or a string. Received: ${typeof input} - ${input}`,
);
}

Expand Down Expand Up @@ -79,17 +82,9 @@ export class DateHelpers {
});

if (!isValid(output)) {
withScope((scope: Scope) => {
scope.setLevel('error');
scope.setExtras({
input,
});
captureException(
new InvalidArgumentException(
`Invalid Date: unable to parse date string - ${input}. The input might not match any expected formats or is not a valid date.`,
),
);
});
throw new InvalidArgumentException(
`Invalid Date: unable to parse date string - ${input}. The input might not match any expected formats or is not a valid date.`,
);
}

return output;
Expand Down
56 changes: 0 additions & 56 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2861444

Please sign in to comment.