Skip to content

Commit 362928f

Browse files
authored
test: Increase code coverage in package jsapi-utils (#1114)
Closes #1096 Increase code coverage in package jsapi-utils
1 parent 0be0850 commit 362928f

13 files changed

Lines changed: 1667 additions & 110 deletions

packages/jsapi-utils/src/DateUtils.test.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,88 @@ describe('dateTimeString parsing tests', () => {
173173
testDateTimeStringThrows('2012-04-20 12:13:14.321Overflow');
174174
});
175175
});
176+
177+
describe('makeDateWrapper', () => {
178+
it('should use default values if not given arguments', () => {
179+
const expectedDate = new Date(2022, 0, 1, 0, 0, 0, 0);
180+
181+
expect(
182+
DateUtils.makeDateWrapper('Asia/Dubai', 2022).valueOf()
183+
).toStrictEqual(expectedDate.valueOf().toString());
184+
});
185+
});
186+
187+
describe('parseDateValues', () => {
188+
it('should return null if any value is invalid', () => {
189+
expect(
190+
DateUtils.parseDateValues(
191+
'test',
192+
'test',
193+
'test',
194+
'test',
195+
'test',
196+
'test',
197+
'test'
198+
)
199+
).toBe(null);
200+
});
201+
});
202+
203+
describe('parseDateRange', () => {
204+
const MS_PER_DAY = 1000 * 60 * 60 * 24;
205+
206+
function dateDiffInMillisseconds(a: Date, b: Date) {
207+
// Discard the time and time-zone information.
208+
const utc1 = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate());
209+
const utc2 = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate());
210+
211+
return Math.floor(utc2 - utc1);
212+
}
213+
214+
it('should throw an error if the text is empty', () => {
215+
expect(() => DateUtils.parseDateRange('', 'America/New_York')).toThrowError(
216+
'Cannot parse date range from empty string'
217+
);
218+
});
219+
220+
it('should return a range of null values if text is "null"', () => {
221+
expect(DateUtils.parseDateRange('null', 'America/New_York')).toEqual([
222+
null,
223+
null,
224+
]);
225+
});
226+
227+
it('should return a range from today to tomorrow if text is "today"', () => {
228+
const range = DateUtils.parseDateRange('today', 'America/New_York');
229+
const start = range[0];
230+
const end = range[1];
231+
if (start && end) {
232+
const startDate = start?.asDate();
233+
const endDate = end?.asDate();
234+
expect(dateDiffInMillisseconds(startDate, endDate)).toBe(MS_PER_DAY);
235+
}
236+
});
237+
238+
it('should return null as the end range if text is "now"', () => {
239+
const range = DateUtils.parseDateRange('now', 'America/New_York');
240+
expect(range[1]).toBeNull();
241+
});
242+
243+
it('should throw an error if a value in text is invalid', () => {
244+
expect(() =>
245+
DateUtils.parseDateRange('9999-99-99', 'America/New_York')
246+
).toThrowError(/Unable to extract date values from/i);
247+
});
248+
});
249+
250+
describe('getJsDate', () => {
251+
it('returns a date object given that input is a number', () => {
252+
const expectedDate = new Date(10000);
253+
expect(DateUtils.getJsDate(10000)).toEqual(expectedDate);
254+
});
255+
256+
it('returns a date object given a DateWrapper', () => {
257+
const dateWrapper = DateUtils.makeDateWrapper('America/New_York', 2022);
258+
expect(DateUtils.getJsDate(dateWrapper)).toEqual(dateWrapper.asDate());
259+
});
260+
});

packages/jsapi-utils/src/Formatter.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ describe('makeColumnFormatMap', () => {
7373
formatMap.get(TableUtils.dataType.DECIMAL)?.get(conflictingColumnName)
7474
).toBe(lastFormat);
7575
});
76+
77+
it('returns an empty map if columnFormattingRules is null', () => {
78+
// @ts-expect-error test null columnFormattingRules
79+
const formatMap = Formatter.makeColumnFormatMap(null);
80+
expect(formatMap.size).toBe(0);
81+
});
7682
});
7783

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

138144
describe('getFormattedString', () => {
145+
it('returns an empty string when value is null', () => {
146+
const formatter = makeFormatter();
147+
expect(formatter.getFormattedString(null, 'decimal')).toBe('');
148+
});
149+
139150
it('passes undefined to formatter.format for column with no custom format', () => {
140151
const value = 'randomValue';
141152
const columnType = TYPE_DATETIME;
@@ -182,3 +193,27 @@ describe('getFormattedString', () => {
182193
columnTypeFormatter.format = originalFormatFn;
183194
});
184195
});
196+
197+
describe('getColumnFormatMapForType', () => {
198+
it('should get columnFormatMap for a given column type and create new map entry', () => {
199+
const formatter = makeFormatter();
200+
const formatMap = formatter.getColumnFormatMapForType('decimal', true);
201+
if (formatMap) {
202+
expect(formatMap).not.toBeUndefined();
203+
expect(formatMap.size).toBe(0);
204+
}
205+
});
206+
207+
it('returns undefined if no formatmap exists and createIfNecessary is false', () => {
208+
const formatter = new Formatter();
209+
const formatMap = formatter.getColumnFormatMapForType('decimal');
210+
expect(formatMap).toBeUndefined();
211+
});
212+
});
213+
214+
describe('timeZone', () => {
215+
it('should return the time zone name', () => {
216+
const formatter = makeFormatter();
217+
expect(formatter.timeZone).toBe('America/New_York');
218+
});
219+
});

packages/jsapi-utils/src/FormatterUtils.test.ts

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import Formatter from './Formatter';
2-
import FormatterUtils from './FormatterUtils';
2+
import FormatterUtils, {
3+
getColumnFormats,
4+
getDateTimeFormatterOptions,
5+
} from './FormatterUtils';
36
import {
47
TableColumnFormat,
58
TableColumnFormatter,
69
TableColumnFormatType,
710
} from './formatters';
811
import TableUtils from './TableUtils';
12+
import { ColumnFormatSettings, DateTimeFormatSettings } from './Settings';
913

1014
function makeFormatter(...settings: ConstructorParameters<typeof Formatter>) {
1115
return new Formatter(...settings);
@@ -83,3 +87,54 @@ describe('isCustomColumnFormatDefined', () => {
8387
).toBe(false);
8488
});
8589
});
90+
91+
describe('getColumnFormats', () => {
92+
it('should return an array of format rules', () => {
93+
const settings: ColumnFormatSettings = {
94+
formatter: [
95+
{
96+
columnType: 'integer',
97+
columnName: 'test1',
98+
format: {
99+
label: 'test1',
100+
formatString: '0.0',
101+
type: 'type-context-custom',
102+
},
103+
},
104+
{
105+
columnType: 'decimal',
106+
columnName: 'test2',
107+
format: {
108+
label: 'test2',
109+
formatString: '0.0',
110+
type: 'type-context-custom',
111+
},
112+
},
113+
],
114+
};
115+
116+
expect(getColumnFormats(settings)).toEqual(settings.formatter);
117+
});
118+
119+
it('should return undefined if settings or settings.formatter is undefined', () => {
120+
expect(getColumnFormats()).toBeUndefined();
121+
});
122+
});
123+
124+
describe('getDateTimeFormatterOptions', () => {
125+
it('should return an object containing date and time formatter options', () => {
126+
const settings: DateTimeFormatSettings = {
127+
timeZone: 'America/New_York',
128+
defaultDateTimeFormat: 'yyyy-MM-dd HH:mm:ss.SSS',
129+
showTimeZone: true,
130+
showTSeparator: false,
131+
};
132+
const expectedObject = {
133+
...settings,
134+
defaultDateTimeFormatString: 'yyyy-MM-dd HH:mm:ss.SSS',
135+
};
136+
delete expectedObject.defaultDateTimeFormat;
137+
138+
expect(getDateTimeFormatterOptions(settings)).toEqual(expectedObject);
139+
});
140+
});

0 commit comments

Comments
 (0)