Skip to content

Commit

Permalink
feat(tests): add missing unit tests for Excel Export Service
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding-SE committed Nov 7, 2019
1 parent d23d08f commit eb0a536
Show file tree
Hide file tree
Showing 3 changed files with 326 additions and 61 deletions.
37 changes: 23 additions & 14 deletions src/app/modules/angular-slickgrid/models/fieldType.enum.ts
@@ -1,8 +1,17 @@
export enum FieldType {
/** unknown type */
unknown,

/** string type */
string,

/** boolean type (true/false) */
boolean,

/** integer number type (1,2,99) */
integer,

/** float number (with decimal) type */
float,

/** number includes Integer and Float */
Expand All @@ -11,52 +20,52 @@ export enum FieldType {
/** new Date(), javascript Date object */
date,

/** Format: 'YYYY-MM-DD' => 2001-01-01 */
/** Format: 'YYYY-MM-DD' => 2001-02-28 */
dateIso,

/** Format: 'YYYY-MM-DDTHH:mm:ss.SSSZ' => 2001-01-01T14:00:00.123Z */
/** Format: 'YYYY-MM-DDTHH:mm:ss.SSSZ' => 2001-02-28T14:00:00.123Z */
dateUtc,

/** new Date(), javacript Date Object with Time */
dateTime,

/** Format: 'YYYY-MM-DD HH:mm:ss' => 2001-01-01 14:01:01 */
/** Format: 'YYYY-MM-DD HH:mm:ss' => 2001-02-28 14:01:01 */
dateTimeIso,

/** Format: 'YYYY-MM-DD h:mm:ss a' => 2001-01-01 11:01:01 pm */
/** Format: 'YYYY-MM-DD h:mm:ss a' => 2001-02-28 11:01:01 pm */
dateTimeIsoAmPm,

/** Format: 'YYYY-MM-DD h:mm:ss A' => 2001-01-01 11:01:01 PM */
/** Format: 'YYYY-MM-DD h:mm:ss A' => 2001-02-28 11:01:01 PM */
dateTimeIsoAM_PM,

/** Format: 'YYYY-MM-DD HH:mm' => 2001-01-01 14:01 */
/** Format: 'YYYY-MM-DD HH:mm' => 2001-02-28 14:01 */
dateTimeShortIso,

/** Format (Euro): 'DD/MM/YYYY' => 02/28/2001 */
/** Format (Euro): 'DD/MM/YYYY' => 28/02/2001 */
dateEuro,

/** Format (Euro): 'D/M/YY' => 2/28/12 */
/** Format (Euro): 'D/M/YY' => 28/2/12 */
dateEuroShort,

/** Format (Euro): 'DD/MM/YYYY HH:mm' => 02/28/2001 13:01 */
/** Format (Euro): 'DD/MM/YYYY HH:mm' => 28/02/2001 13:01 */
dateTimeShortEuro,

/** Format (Euro): 'DD/MM/YYYY HH:mm:ss' => 02/28/2001 13:01:01 */
dateTimeEuro,

/** Format (Euro): 'DD/MM/YYYY hh:mm:ss a' => 02/28/2001 11:01:01 pm */
/** Format (Euro): 'DD/MM/YYYY hh:mm:ss a' => 28/02/2001 11:01:01 pm */
dateTimeEuroAmPm,

/** Format (Euro): 'DD/MM/YYYY hh:mm:ss A' => 02/28/2001 11:01:01 PM */
/** Format (Euro): 'DD/MM/YYYY hh:mm:ss A' => 28/02/2001 11:01:01 PM */
dateTimeEuroAM_PM,

/** Format (Euro): 'D/M/YY H:m:s' => 2/28/14 14:1:2 */
/** Format (Euro): 'D/M/YY H:m:s' => 28/2/14 14:1:2 */
dateTimeEuroShort,

/** Format (Euro): 'D/M/YY h:m:s a' => 2/28/14 1:2:10 pm */
/** Format (Euro): 'D/M/YY h:m:s a' => 28/2/14 1:2:10 pm */
dateTimeEuroShortAmPm,

/** Format (Euro): 'D/M/YY h:m:s A' => 2/28/14 14:1:1 PM */
/** Format (Euro): 'D/M/YY h:m:s A' => 28/2/14 14:1:1 PM */
dateTimeEuroShortAM_PM,

/** Format: 'MM/DD/YYYY' => 02/28/2001 */
Expand Down
@@ -1,5 +1,6 @@
import { TestBed } from '@angular/core/testing';
import { TranslateModule, TranslateService } from '@ngx-translate/core';
import * as moment from 'moment-mini';

import {
FileType,
Expand Down Expand Up @@ -984,6 +985,261 @@ describe('ExcelExportService', () => {
});
});
});

describe('useCellFormatByFieldType method', () => {
it('should return a date time format when using FieldType.dateTime and a Date object as input', async () => {
const input = new Date('2012-02-28 15:07:59');
const expectedDate = '2012-02-28 15:07:59';

service.init(gridStub, dataViewStub);
await service.exportToExcel(mockExportExcelOptions);
const output = service.useCellFormatByFieldType(input, FieldType.dateTime);

expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate });
});

it('should return a date time format when using FieldType.dateTimeIso', async () => {
const input = '2012-02-28 15:07:59';
const expectedDate = '2012-02-28 15:07:59';

service.init(gridStub, dataViewStub);
await service.exportToExcel(mockExportExcelOptions);
const output = service.useCellFormatByFieldType(input, FieldType.dateTimeIso);

expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate });
});

it('should return a date time format when using FieldType.dateTimeShortIso', async () => {
const input = '2012-02-28 15:07:59';
const expectedDate = '2012-02-28 15:07';

service.init(gridStub, dataViewStub);
await service.exportToExcel(mockExportExcelOptions);
const output = service.useCellFormatByFieldType(input, FieldType.dateTimeShortIso);

expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate });
});

it('should return a date time format when using FieldType.dateTimeIsoAmPm', async () => {
const input = '2012-02-28 15:07:59';
const expectedDate = '2012-02-28 03:07:59 pm';

service.init(gridStub, dataViewStub);
await service.exportToExcel(mockExportExcelOptions);
const output = service.useCellFormatByFieldType(input, FieldType.dateTimeIsoAmPm);

expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate });
});

it('should return a date time format when using FieldType.dateTimeIsoAM_PM', async () => {
const input = '2012-02-28 15:07:59';
const expectedDate = '2012-02-28 03:07:59 PM';

service.init(gridStub, dataViewStub);
await service.exportToExcel(mockExportExcelOptions);
const output = service.useCellFormatByFieldType(input, FieldType.dateTimeIsoAM_PM);

expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate });
});

it('should return a date time format when using FieldType.dateEuro', async () => {
const input = '2012-02-28 15:07:59';
const expectedDate = '28/02/2012';

service.init(gridStub, dataViewStub);
await service.exportToExcel(mockExportExcelOptions);
const output = service.useCellFormatByFieldType(input, FieldType.dateEuro);

expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate });
});

it('should return a date time format when using FieldType.dateEuroShort', async () => {
const input = '2012-02-28 15:07:59';
const expectedDate = '28/2/12';

service.init(gridStub, dataViewStub);
await service.exportToExcel(mockExportExcelOptions);
const output = service.useCellFormatByFieldType(input, FieldType.dateEuroShort);

expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate });
});

it('should return a date time format when using FieldType.dateTimeEuro', async () => {
const input = '2012-02-28 15:07:59';
const expectedDate = '28/02/2012 15:07:59';

service.init(gridStub, dataViewStub);
await service.exportToExcel(mockExportExcelOptions);
const output = service.useCellFormatByFieldType(input, FieldType.dateTimeEuro);

expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate });
});

it('should return a date time format when using FieldType.dateTimeShortEuro', async () => {
const input = '2012-02-28 15:07:59';
const expectedDate = '28/02/2012 15:07';

service.init(gridStub, dataViewStub);
await service.exportToExcel(mockExportExcelOptions);
const output = service.useCellFormatByFieldType(input, FieldType.dateTimeShortEuro);

expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate });
});

it('should return a date time format when using FieldType.dateTimeEuroAmPm', async () => {
const input = '2012-02-28 15:07:59';
const expectedDate = '28/02/2012 03:07:59 pm';

service.init(gridStub, dataViewStub);
await service.exportToExcel(mockExportExcelOptions);
const output = service.useCellFormatByFieldType(input, FieldType.dateTimeEuroAmPm);

expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate });
});

it('should return a date time format when using FieldType.dateTimeEuroAM_PM', async () => {
const input = '2012-02-28 15:07:59';
const expectedDate = '28/02/2012 03:07:59 PM';

service.init(gridStub, dataViewStub);
await service.exportToExcel(mockExportExcelOptions);
const output = service.useCellFormatByFieldType(input, FieldType.dateTimeEuroAM_PM);

expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate });
});

it('should return a date time format when using FieldType.dateTimeEuroShort', async () => {
const input = '2012-02-28 15:07:59';
const expectedDate = '28/2/12 15:7:59';

service.init(gridStub, dataViewStub);
await service.exportToExcel(mockExportExcelOptions);
const output = service.useCellFormatByFieldType(input, FieldType.dateTimeEuroShort);

expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate });
});

it('should return a date time format when using FieldType.dateTimeEuroShortAmPm', async () => {
const input = '2012-02-28 15:07:59';
const expectedDate = '28/2/12 3:7:59 pm';

service.init(gridStub, dataViewStub);
await service.exportToExcel(mockExportExcelOptions);
const output = service.useCellFormatByFieldType(input, FieldType.dateTimeEuroShortAmPm);

expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate });
});

it('should return a date time format when using FieldType.dateUs', async () => {
const input = new Date('2012-02-28 15:07:59');
const expectedDate = '02/28/2012';

service.init(gridStub, dataViewStub);
await service.exportToExcel(mockExportExcelOptions);
const output = service.useCellFormatByFieldType(input, FieldType.dateUs);

expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate });
});

it('should return a date time format when using FieldType.dateUsShort', async () => {
const input = new Date('2012-02-28 15:07:59');
const expectedDate = '2/28/12';

service.init(gridStub, dataViewStub);
await service.exportToExcel(mockExportExcelOptions);
const output = service.useCellFormatByFieldType(input, FieldType.dateUsShort);

expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate });
});

it('should return a date time format when using FieldType.dateTimeUs', async () => {
const input = new Date('2012-02-28 15:07:59');
const expectedDate = '02/28/2012 15:07:59';

service.init(gridStub, dataViewStub);
await service.exportToExcel(mockExportExcelOptions);
const output = service.useCellFormatByFieldType(input, FieldType.dateTimeUs);

expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate });
});

it('should return a date time format when using FieldType.dateTimeShortUs', async () => {
const input = new Date('2012-02-28 15:07:59');
const expectedDate = '02/28/2012 15:07';

service.init(gridStub, dataViewStub);
await service.exportToExcel(mockExportExcelOptions);
const output = service.useCellFormatByFieldType(input, FieldType.dateTimeShortUs);

expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate });
});

it('should return a date time format when using FieldType.dateTimeUsAmPm', async () => {
const input = new Date('2012-02-28 15:07:59');
const expectedDate = '02/28/2012 03:07:59 pm';

service.init(gridStub, dataViewStub);
await service.exportToExcel(mockExportExcelOptions);
const output = service.useCellFormatByFieldType(input, FieldType.dateTimeUsAmPm);

expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate });
});

it('should return a date time format when using FieldType.dateTimeUsAM_PM', async () => {
const input = new Date('2012-02-28 15:07:59');
const expectedDate = '02/28/2012 03:07:59 PM';

service.init(gridStub, dataViewStub);
await service.exportToExcel(mockExportExcelOptions);
const output = service.useCellFormatByFieldType(input, FieldType.dateTimeUsAM_PM);

expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate });
});

it('should return a date time format when using FieldType.dateTimeUsShort', async () => {
const input = new Date('2012-02-28 15:07:59');
const expectedDate = '2/28/12 15:7:59';

service.init(gridStub, dataViewStub);
await service.exportToExcel(mockExportExcelOptions);
const output = service.useCellFormatByFieldType(input, FieldType.dateTimeUsShort);

expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate });
});

it('should return a date time format when using FieldType.dateTimeUsShortAmPm', async () => {
const input = new Date('2012-02-28 15:07:59');
const expectedDate = '2/28/12 3:7:59 pm';

service.init(gridStub, dataViewStub);
await service.exportToExcel(mockExportExcelOptions);
const output = service.useCellFormatByFieldType(input, FieldType.dateTimeUsShortAmPm);

expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate });
});

xit('should return a date time format when using FieldType.dateUtc', async () => {
const input = moment('2013-05-23T17:55:00.325').utcOffset(420); // timezone that is +7 UTC hours
const expectedDate = '2013-05-24T04:55:00.325+07:00';

service.init(gridStub, dataViewStub);
await service.exportToExcel(mockExportExcelOptions);
const output = service.useCellFormatByFieldType(input, FieldType.dateUtc);

expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate });
});

it('should return a date time format when using FieldType.date', async () => {
const input = new Date('2012-02-28T23:01:52.103Z');
const expectedDate = '2012-02-28';

service.init(gridStub, dataViewStub);
await service.exportToExcel(mockExportExcelOptions);
const output = service.useCellFormatByFieldType(input, FieldType.date);

expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate });
});
});
});

describe('without ngx-translate', () => {
Expand Down

0 comments on commit eb0a536

Please sign in to comment.