Skip to content

Commit

Permalink
#1362 - updated test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Brajesh Kumar authored and Brajesh Kumar committed Nov 1, 2023
1 parent a87ab10 commit 5b0bbfb
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 43 deletions.
89 changes: 58 additions & 31 deletions src/app/core/duplicate-records/duplicate-records.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,97 @@
import { TestBed } from '@angular/core/testing';
import { DuplicateRecordsService } from './duplicate-records.service';
import { DuplicateRecordService } from './duplicate-records.service';
import { EntityMapperService } from '../entity/entity-mapper/entity-mapper.service';
import { DatabaseEntity, EntityRegistry } from '../entity/database-entity.decorator';
import { EntitySchemaService } from '../entity/schema/entity-schema.service';
import { DatabaseEntity, entityRegistry, EntityRegistry } from '../entity/database-entity.decorator';
import { Database } from '../database/database';
import { SessionService } from '../session/session-service/session.service';
import { Entity } from '../entity/model/entity';
import { DatabaseField } from '../entity/database-field.decorator';
import { CoreModule } from '../core.module';
import { ComponentRegistry } from 'app/dynamic-components';
import { UpdateMetadata } from '../entity/model/update-metadata';

describe('DuplicateRecordsService', () => {
let service: DuplicateRecordsService;
let service: DuplicateRecordService;
let entityMapperService: EntityMapperService;
let entityTypes: EntityRegistry;
let entitySchemaService: EntitySchemaService;
let database: Database;
let sessionService: SessionService;
let database: Database;

@DatabaseEntity("DuplicateTestEntity")
class DuplicateTestEntity extends Entity {
@DatabaseField() name: String;
@DatabaseField() boolProperty: boolean;
@DatabaseField() created: UpdateMetadata;
@DatabaseField() updated: UpdateMetadata;
@DatabaseField() inactive: boolean;
}

beforeEach(() => {
TestBed.configureTestingModule({
imports: [CoreModule],
providers: [
DuplicateRecordsService,
DuplicateRecordService,
Database,
EntityMapperService,
EntityRegistry,
EntitySchemaService,
SessionService,
{ provide: Database, useValue: {} },
],
{ provide: EntityRegistry, useValue: entityRegistry },
ComponentRegistry,]
});
service = TestBed.inject(DuplicateRecordsService);
service = TestBed.inject(DuplicateRecordService);
entityMapperService = TestBed.inject(EntityMapperService);
entityTypes = TestBed.inject(EntityRegistry);
entitySchemaService = TestBed.inject(EntitySchemaService);
database = TestBed.inject(Database);
sessionService = TestBed.inject(SessionService);
});

it('should be created', () => {
expect(service).toBeTruthy();
});

it('should transform data correctly', () => {
const duplicateTest = new DuplicateTestEntity();
duplicateTest.name = "TestName"
duplicateTest.boolProperty = true;

const schemaName = DuplicateTestEntity.ENTITY_TYPE;
const originalData = [
{
record: duplicateTest
}
];

const transformedData = service.transformData(originalData, schemaName);
expect(transformedData[0]).toBeInstanceOf(Entity);
expect(transformedData[0]._id).toBeDefined();
expect(transformedData[0]._id).not.toBe(duplicateTest['_id']);
expect(transformedData[0].name).toMatch(/^Copy of /);
expect(transformedData[0].boolProperty).toBe(true);
});

it('should duplicate record and save data', async () => {
@DatabaseEntity("DuplicateTestEntity")
class DuplicateTestEntity extends Entity {
@DatabaseField() string: String;
@DatabaseField() boolProperty: boolean;
@DatabaseEntity("SaveTestEntity")
class SaveTestEntity extends Entity {
@DatabaseField() name: String;
@DatabaseField() boolProperty: boolean;
@DatabaseField() created: UpdateMetadata;
@DatabaseField() updated: UpdateMetadata;
@DatabaseField() inactive: boolean;
}

const duplicateTestEntity = new DuplicateTestEntity();
duplicateTestEntity.string = "TestName"
const duplicateTestEntity = new SaveTestEntity();
duplicateTestEntity.name = "TestName"
duplicateTestEntity.boolProperty = true;
duplicateTestEntity.inactive = false;

const schemaName =DuplicateTestEntity.ENTITY_TYPE;

const originalData = [
{
record: duplicateTestEntity
}
];

const transformDataSpy = spyOn(service, 'transformData').and.callThrough();
const saveAllSpy = spyOn(entityMapperService, 'saveAll');

const data = transformDataSpy(originalData, schemaName)

await service.getDataforDuplicate(originalData, schemaName);
await service.duplicateRecord(originalData, schemaName);
expect(transformDataSpy).toHaveBeenCalledWith(originalData, schemaName);
expect(saveAllSpy).toHaveBeenCalledWith(data);
expect(saveAllSpy).toHaveBeenCalled();

});
});
10 changes: 6 additions & 4 deletions src/app/core/duplicate-records/duplicate-records.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ import { Entity } from '../entity/model/entity';
providedIn: 'root'
})

export class DuplicateRecordsService {
export class DuplicateRecordService {
get: jasmine.Spy<jasmine.Func>;
constructor(
private entitymapperservice: EntityMapperService,
private entityTypes: EntityRegistry,
private entityService: EntitySchemaService,
) {}
async getDataforDuplicate(data: any, schemaName: string) {
const duplicateData = this.transformData(data, schemaName)
this.entitymapperservice.saveAll(duplicateData);

async duplicateRecord(data: any, schemaName: string) {
const duplicateData = this.transformData(data, schemaName);
const results = await this.entitymapperservice.saveAll(duplicateData);
return results;
}

transformData(originalData: any, schemaName: string): any {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { DuplicateRecordsDirective } from "./duplicate-records.directive";
import { DuplicateRecordsService } from "../duplicate-records.service";
import { DuplicateRecordService } from "../duplicate-records.service";

describe("ExportDataDirective", () => {
let mockDuplicateRecord: jasmine.SpyObj<DuplicateRecordsService>;
let mockDuplicateRecord: jasmine.SpyObj<DuplicateRecordService>;
let directive: DuplicateRecordsDirective;

beforeEach(() => {
mockDuplicateRecord = jasmine.createSpyObj(["getDataforDuplicate"]);
mockDuplicateRecord = jasmine.createSpyObj(["duplicateRecord"]);
directive = new DuplicateRecordsDirective(mockDuplicateRecord);
});

Expand All @@ -17,6 +17,6 @@ describe("ExportDataDirective", () => {
it("should call DuplicateRecord when button is clicked", () => {
directive.click();

expect(mockDuplicateRecord.getDataforDuplicate).toHaveBeenCalled();
expect(mockDuplicateRecord.duplicateRecord).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Directive, HostListener, Input } from "@angular/core";
import { DuplicateRecordsService } from '../duplicate-records.service';
import { DuplicateRecordService } from '../duplicate-records.service';

@Directive({
selector: '[appDuplicateRecords]',
Expand All @@ -10,10 +10,10 @@ export class DuplicateRecordsDirective {
@Input("appDuplicateRecords") data: any = [];
@Input() entityType: string = '';

constructor( private duplicaterecords: DuplicateRecordsService) {}

constructor( private duplicateRecordService: DuplicateRecordService) {}
@HostListener("click")
click(){
return this.duplicaterecords.getDataforDuplicate(this.data, this.entityType);
return this.duplicateRecordService.duplicateRecord(this.data, this.entityType);
}
}

0 comments on commit 5b0bbfb

Please sign in to comment.