Skip to content

Commit

Permalink
test: Increase code coverage in package jsapi-utils (#1114)
Browse files Browse the repository at this point in the history
Closes #1096 

Increase code coverage in package jsapi-utils
  • Loading branch information
emilyhuxng authored Mar 14, 2023
1 parent 0be0850 commit 362928f
Show file tree
Hide file tree
Showing 13 changed files with 1,667 additions and 110 deletions.
85 changes: 85 additions & 0 deletions packages/jsapi-utils/src/DateUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,88 @@ describe('dateTimeString parsing tests', () => {
testDateTimeStringThrows('2012-04-20 12:13:14.321Overflow');
});
});

describe('makeDateWrapper', () => {
it('should use default values if not given arguments', () => {
const expectedDate = new Date(2022, 0, 1, 0, 0, 0, 0);

expect(
DateUtils.makeDateWrapper('Asia/Dubai', 2022).valueOf()
).toStrictEqual(expectedDate.valueOf().toString());
});
});

describe('parseDateValues', () => {
it('should return null if any value is invalid', () => {
expect(
DateUtils.parseDateValues(
'test',
'test',
'test',
'test',
'test',
'test',
'test'
)
).toBe(null);
});
});

describe('parseDateRange', () => {
const MS_PER_DAY = 1000 * 60 * 60 * 24;

function dateDiffInMillisseconds(a: Date, b: Date) {
// Discard the time and time-zone information.
const utc1 = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate());
const utc2 = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate());

return Math.floor(utc2 - utc1);
}

it('should throw an error if the text is empty', () => {
expect(() => DateUtils.parseDateRange('', 'America/New_York')).toThrowError(
'Cannot parse date range from empty string'
);
});

it('should return a range of null values if text is "null"', () => {
expect(DateUtils.parseDateRange('null', 'America/New_York')).toEqual([
null,
null,
]);
});

it('should return a range from today to tomorrow if text is "today"', () => {
const range = DateUtils.parseDateRange('today', 'America/New_York');
const start = range[0];
const end = range[1];
if (start && end) {
const startDate = start?.asDate();
const endDate = end?.asDate();
expect(dateDiffInMillisseconds(startDate, endDate)).toBe(MS_PER_DAY);
}
});

it('should return null as the end range if text is "now"', () => {
const range = DateUtils.parseDateRange('now', 'America/New_York');
expect(range[1]).toBeNull();
});

it('should throw an error if a value in text is invalid', () => {
expect(() =>
DateUtils.parseDateRange('9999-99-99', 'America/New_York')
).toThrowError(/Unable to extract date values from/i);
});
});

describe('getJsDate', () => {
it('returns a date object given that input is a number', () => {
const expectedDate = new Date(10000);
expect(DateUtils.getJsDate(10000)).toEqual(expectedDate);
});

it('returns a date object given a DateWrapper', () => {
const dateWrapper = DateUtils.makeDateWrapper('America/New_York', 2022);
expect(DateUtils.getJsDate(dateWrapper)).toEqual(dateWrapper.asDate());
});
});
35 changes: 35 additions & 0 deletions packages/jsapi-utils/src/Formatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ describe('makeColumnFormatMap', () => {
formatMap.get(TableUtils.dataType.DECIMAL)?.get(conflictingColumnName)
).toBe(lastFormat);
});

it('returns an empty map if columnFormattingRules is null', () => {
// @ts-expect-error test null columnFormattingRules
const formatMap = Formatter.makeColumnFormatMap(null);
expect(formatMap.size).toBe(0);
});
});

it('returns correct formatters for given column types', () => {
Expand Down Expand Up @@ -136,6 +142,11 @@ describe('getColumnFormat', () => {
});

describe('getFormattedString', () => {
it('returns an empty string when value is null', () => {
const formatter = makeFormatter();
expect(formatter.getFormattedString(null, 'decimal')).toBe('');
});

it('passes undefined to formatter.format for column with no custom format', () => {
const value = 'randomValue';
const columnType = TYPE_DATETIME;
Expand Down Expand Up @@ -182,3 +193,27 @@ describe('getFormattedString', () => {
columnTypeFormatter.format = originalFormatFn;
});
});

describe('getColumnFormatMapForType', () => {
it('should get columnFormatMap for a given column type and create new map entry', () => {
const formatter = makeFormatter();
const formatMap = formatter.getColumnFormatMapForType('decimal', true);
if (formatMap) {
expect(formatMap).not.toBeUndefined();
expect(formatMap.size).toBe(0);
}
});

it('returns undefined if no formatmap exists and createIfNecessary is false', () => {
const formatter = new Formatter();
const formatMap = formatter.getColumnFormatMapForType('decimal');
expect(formatMap).toBeUndefined();
});
});

describe('timeZone', () => {
it('should return the time zone name', () => {
const formatter = makeFormatter();
expect(formatter.timeZone).toBe('America/New_York');
});
});
57 changes: 56 additions & 1 deletion packages/jsapi-utils/src/FormatterUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import Formatter from './Formatter';
import FormatterUtils from './FormatterUtils';
import FormatterUtils, {
getColumnFormats,
getDateTimeFormatterOptions,
} from './FormatterUtils';
import {
TableColumnFormat,
TableColumnFormatter,
TableColumnFormatType,
} from './formatters';
import TableUtils from './TableUtils';
import { ColumnFormatSettings, DateTimeFormatSettings } from './Settings';

function makeFormatter(...settings: ConstructorParameters<typeof Formatter>) {
return new Formatter(...settings);
Expand Down Expand Up @@ -83,3 +87,54 @@ describe('isCustomColumnFormatDefined', () => {
).toBe(false);
});
});

describe('getColumnFormats', () => {
it('should return an array of format rules', () => {
const settings: ColumnFormatSettings = {
formatter: [
{
columnType: 'integer',
columnName: 'test1',
format: {
label: 'test1',
formatString: '0.0',
type: 'type-context-custom',
},
},
{
columnType: 'decimal',
columnName: 'test2',
format: {
label: 'test2',
formatString: '0.0',
type: 'type-context-custom',
},
},
],
};

expect(getColumnFormats(settings)).toEqual(settings.formatter);
});

it('should return undefined if settings or settings.formatter is undefined', () => {
expect(getColumnFormats()).toBeUndefined();
});
});

describe('getDateTimeFormatterOptions', () => {
it('should return an object containing date and time formatter options', () => {
const settings: DateTimeFormatSettings = {
timeZone: 'America/New_York',
defaultDateTimeFormat: 'yyyy-MM-dd HH:mm:ss.SSS',
showTimeZone: true,
showTSeparator: false,
};
const expectedObject = {
...settings,
defaultDateTimeFormatString: 'yyyy-MM-dd HH:mm:ss.SSS',
};
delete expectedObject.defaultDateTimeFormat;

expect(getDateTimeFormatterOptions(settings)).toEqual(expectedObject);
});
});
Loading

0 comments on commit 362928f

Please sign in to comment.