Skip to content

Commit

Permalink
#1918 - updated unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Brajesh Kumar authored and Brajesh Kumar committed Oct 19, 2023
1 parent 757b500 commit d673456
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
21 changes: 8 additions & 13 deletions src/app/core/export/download-service/download.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,29 +90,24 @@ describe("DownloadService", () => {

@DatabaseEntity("TestEntity")
class TestEntity extends Entity {
@DatabaseField() enumProperty: ConfigurableEnumValue;
@DatabaseField() dateProperty: Date;
@DatabaseField() boolProperty: boolean;
@DatabaseField({ "label": "test enum" }) enumProperty: ConfigurableEnumValue;
@DatabaseField({ "label": "test date" }) dateProperty: Date;
@DatabaseField({ "label": "test boolean" }) boolProperty: boolean;
}

const testEntity = new TestEntity();
testEntity.enumProperty = testEnumValue;
testEntity.dateProperty = new Date(testDate);
testEntity.boolProperty = true;

const objectToArray = [
testEntity.enumProperty.label,
testEntity.dateProperty.toISOString(),
testEntity.boolProperty.toString(),
];

const csvExport = await service.createCsv([testEntity]);

const rows = csvExport.split(DownloadService.SEPARATOR_ROW);
expect(rows).toHaveSize(1 + 1); // includes 1 header line
expect(objectToArray).toContain(testEnumValue.label);
expect(objectToArray).toContain(new Date(testDate).toISOString());
expect(objectToArray).toContain('true');
const columnValues = rows[1].split(DownloadService.SEPARATOR_COL);
expect(columnValues).toHaveSize(3 + 1); // Properties + _id
expect(columnValues).toContain['"' + testEnumValue.label + '"'];
expect(columnValues).toContain['"' + testDate + '"'];
expect(columnValues).toContain['"true"'];
});

it("should export a date as YYYY-MM-dd only", async () => {
Expand Down
13 changes: 7 additions & 6 deletions src/app/core/export/download-service/download.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { LoggingService } from "../../logging/logging.service";
import { DataTransformationService } from "../data-transformation-service/data-transformation.service";
import { transformToReadableFormat } from "../../common-components/entity-subrecord/entity-subrecord/value-accessor";
import { Papa } from "ngx-papaparse";
import { EntitySchemaField } from "app/core/entity/schema/entity-schema-field";

/**
* This service allows to start a download process from the browser.
Expand Down Expand Up @@ -114,13 +115,13 @@ export class DownloadService {
}

exportFile(data: any[], entityConstructor: { schema: any; }) {
const entitySchema = [entityConstructor.schema];
const columnLabel = {};

entitySchema[0].forEach((value: { value: string, label: string }, key: string) => {
if (value.label) columnLabel[key] = value.label;
const entitySchema = entityConstructor.schema;
const columnLabel = new Map<string, EntitySchemaField>();
entitySchema.forEach((value: { value: string, label: string }, key: string) => {
if (value.label) columnLabel[key] = value.label;
});

const exportEntities = data.map((item) => {
for (const key in item) {
if (!columnLabel.hasOwnProperty(key)) delete item[key];
Expand Down

0 comments on commit d673456

Please sign in to comment.