Skip to content

Commit

Permalink
add basic unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SKarolFolio committed Nov 25, 2024
1 parent 0430707 commit 8133a1c
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 13 deletions.
16 changes: 8 additions & 8 deletions src/common/hooks/useRecordGeneration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ export const useRecordGeneration = () => {
const initialSchemaKey = useRecoilValue(state.config.initialSchemaKey);

const generateRecord = () => {
return recordGeneratorService
?.init({
schema,
initKey: initialSchemaKey,
userValues,
selectedEntries,
})
.generate();
recordGeneratorService?.init({
schema,
initKey: initialSchemaKey,
userValues,
selectedEntries,
});

return recordGeneratorService?.generate();
};

return { generateRecord };
Expand Down
3 changes: 1 addition & 2 deletions src/common/services/record/record.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export interface IRecord {
initKey: string | null;
userValues: UserValues;
selectedEntries: string[];
}) => IRecord;

}) => void;
generate: () => Record<string, RecordEntry<RecursiveRecordSchema>> | undefined;
}
5 changes: 2 additions & 3 deletions src/common/services/record/record.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { filterUserValues } from '@common/helpers/profile.helper';
import { SchemaTraverser } from './schemaTraverser';
import { IRecord } from './record.interface';

export class RecordGenerator {
export class RecordGenerator implements IRecord {
private schema: Map<string, SchemaEntry>;
private initKey: string | null;
private userValues: UserValues;
Expand Down Expand Up @@ -30,8 +31,6 @@ export class RecordGenerator {
this.initKey = initKey;
this.userValues = userValues;
this.selectedEntries = selectedEntries;

return this;
}

public generate() {
Expand Down
6 changes: 6 additions & 0 deletions src/test/__mocks__/common/hooks/useServicesContext.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ export const schemaCreatorService = {
generate: jest.fn(),
} as ISchemaService;

export const recordGeneratorService = {
init: jest.fn(),
generate: jest.fn(),
} as IRecordService;

jest.mock('@common/hooks/useServicesContext.ts', () => ({
useServicesContext: () => ({
userValuesService,
Expand All @@ -51,5 +56,6 @@ jest.mock('@common/hooks/useServicesContext.ts', () => ({
recordNormalizingService,
recordToSchemaMappingService,
schemaCreatorService,
recordGeneratorService,
}),
}));
33 changes: 33 additions & 0 deletions src/test/__tests__/common/hooks/useRecordGeneration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { recordGeneratorService } from '@src/test/__mocks__/common/hooks/useServicesContext.mock';
import { useRecoilValue } from 'recoil';
import { renderHook } from '@testing-library/react';
import { useRecordGeneration } from '@common/hooks/useRecordGeneration';

jest.mock('recoil');

describe('useRecordGeneration', () => {
it('generates a record successfully', () => {
const schema = 'mockSchema';
const userValues = 'mockUserValues';
const selectedEntries = 'mockSelectedEntries';
const initKey = 'mockInitialSchemaKey';

(useRecoilValue as jest.Mock)
.mockReturnValueOnce(schema)
.mockReturnValueOnce(userValues)
.mockReturnValueOnce(selectedEntries)
.mockReturnValueOnce(initKey);

const { result } = renderHook(() => useRecordGeneration());

result.current.generateRecord();

expect(recordGeneratorService.init).toHaveBeenCalledWith({
schema,
initKey,
userValues,
selectedEntries,
});
expect(recordGeneratorService.generate).toHaveBeenCalled();
});
});
92 changes: 92 additions & 0 deletions src/test/__tests__/common/services/record/record.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { RecordGenerator, SchemaTraverser } from '@common/services/record';
import * as ProfileHelper from '@common/helpers/profile.helper';

describe('RecordGenerator', () => {
let recordGenerator: RecordGenerator;
let schemaTraverserMock: SchemaTraverser;

beforeEach(() => {
schemaTraverserMock = {
init: jest.fn().mockReturnThis(),
traverse: jest.fn(),
} as unknown as SchemaTraverser;

recordGenerator = new RecordGenerator(schemaTraverserMock);
});

describe('init', () => {
it('sets the schema, initKey, userValues, and selectedEntries', () => {
const schema = new Map<string, SchemaEntry>();
const initKey = 'testKey';
const userValues = { key_1: { contents: [] } } as unknown as UserValues;
const selectedEntries = ['entry 1', 'entry 2'];

recordGenerator.init({ schema, initKey, userValues, selectedEntries });

expect(recordGenerator['schema']).toBe(schema);
expect(recordGenerator['initKey']).toBe(initKey);
expect(recordGenerator['userValues']).toBe(userValues);
expect(recordGenerator['selectedEntries']).toEqual(selectedEntries);
});
});

describe('generate', () => {
it('returns undefined if userValues is empty', () => {
recordGenerator['userValues'] = {};
recordGenerator['schema'] = new Map<string, SchemaEntry>([['key_1', {} as SchemaEntry]]);
recordGenerator['initKey'] = 'key_1';

const result = recordGenerator.generate();

expect(result).toBeUndefined();
expect(schemaTraverserMock.init).not.toHaveBeenCalled();
expect(schemaTraverserMock.traverse).not.toHaveBeenCalled();
});

it('returns undefined if schema is empty', () => {
recordGenerator['userValues'] = { key_1: { contents: [] } } as unknown as UserValues;
recordGenerator['schema'] = new Map<string, SchemaEntry>();
recordGenerator['initKey'] = 'key_1';

const result = recordGenerator.generate();

expect(result).toBeUndefined();
expect(schemaTraverserMock.init).not.toHaveBeenCalled();
expect(schemaTraverserMock.traverse).not.toHaveBeenCalled();
});

it('returns undefined if initKey is null', () => {
recordGenerator['userValues'] = { key_1: { contents: [] } } as unknown as UserValues;
recordGenerator['schema'] = new Map<string, SchemaEntry>([['key_1', {} as SchemaEntry]]);
recordGenerator['initKey'] = null;

const result = recordGenerator.generate();

expect(result).toBeUndefined();
expect(schemaTraverserMock.init).not.toHaveBeenCalled();
expect(schemaTraverserMock.traverse).not.toHaveBeenCalled();
});

it('calls "schemaTraverser.init" and "traverse" when all conditions are met', () => {
const filteredValues = { key_1: { contents: [] } } as unknown as UserValues;
jest.spyOn(ProfileHelper, 'filterUserValues').mockReturnValue(filteredValues);
schemaTraverserMock.traverse = jest.fn(() => {});

recordGenerator['userValues'] = { key_1: { contents: [] } } as unknown as UserValues;
recordGenerator['schema'] = new Map<string, SchemaEntry>([['key_1', {} as SchemaEntry]]);
recordGenerator['initKey'] = 'key_1';
recordGenerator['selectedEntries'] = ['entry 1'];

const result = recordGenerator.generate();

expect(schemaTraverserMock.init).toHaveBeenCalledWith({
schema: recordGenerator['schema'],
userValues: filteredValues,
selectedEntries: recordGenerator['selectedEntries'],
initialContainer: {},
});
expect(schemaTraverserMock.traverse).toHaveBeenCalledWith({ key: 'key_1' });
expect(result).toEqual({});
});
});
});

0 comments on commit 8133a1c

Please sign in to comment.