From 8133a1cd7ed60e805ddb5734f69342f088b536cc Mon Sep 17 00:00:00 2001 From: Siarhei Karol Date: Mon, 25 Nov 2024 18:06:32 +0300 Subject: [PATCH] add basic unit tests --- src/common/hooks/useRecordGeneration.ts | 16 ++-- .../services/record/record.interface.ts | 3 +- src/common/services/record/record.ts | 5 +- .../common/hooks/useServicesContext.mock.ts | 6 ++ .../common/hooks/useRecordGeneration.test.ts | 33 +++++++ .../common/services/record/record.test.ts | 92 +++++++++++++++++++ 6 files changed, 142 insertions(+), 13 deletions(-) create mode 100644 src/test/__tests__/common/hooks/useRecordGeneration.test.ts create mode 100644 src/test/__tests__/common/services/record/record.test.ts diff --git a/src/common/hooks/useRecordGeneration.ts b/src/common/hooks/useRecordGeneration.ts index e1ac9645..4495f3d3 100644 --- a/src/common/hooks/useRecordGeneration.ts +++ b/src/common/hooks/useRecordGeneration.ts @@ -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 }; diff --git a/src/common/services/record/record.interface.ts b/src/common/services/record/record.interface.ts index 28c3e19a..1e23ae5f 100644 --- a/src/common/services/record/record.interface.ts +++ b/src/common/services/record/record.interface.ts @@ -4,7 +4,6 @@ export interface IRecord { initKey: string | null; userValues: UserValues; selectedEntries: string[]; - }) => IRecord; - + }) => void; generate: () => Record> | undefined; } diff --git a/src/common/services/record/record.ts b/src/common/services/record/record.ts index 7a00c389..b279560b 100644 --- a/src/common/services/record/record.ts +++ b/src/common/services/record/record.ts @@ -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; private initKey: string | null; private userValues: UserValues; @@ -30,8 +31,6 @@ export class RecordGenerator { this.initKey = initKey; this.userValues = userValues; this.selectedEntries = selectedEntries; - - return this; } public generate() { diff --git a/src/test/__mocks__/common/hooks/useServicesContext.mock.ts b/src/test/__mocks__/common/hooks/useServicesContext.mock.ts index 7850b4f6..0bbfb0de 100644 --- a/src/test/__mocks__/common/hooks/useServicesContext.mock.ts +++ b/src/test/__mocks__/common/hooks/useServicesContext.mock.ts @@ -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, @@ -51,5 +56,6 @@ jest.mock('@common/hooks/useServicesContext.ts', () => ({ recordNormalizingService, recordToSchemaMappingService, schemaCreatorService, + recordGeneratorService, }), })); diff --git a/src/test/__tests__/common/hooks/useRecordGeneration.test.ts b/src/test/__tests__/common/hooks/useRecordGeneration.test.ts new file mode 100644 index 00000000..03740efe --- /dev/null +++ b/src/test/__tests__/common/hooks/useRecordGeneration.test.ts @@ -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(); + }); +}); diff --git a/src/test/__tests__/common/services/record/record.test.ts b/src/test/__tests__/common/services/record/record.test.ts new file mode 100644 index 00000000..ceee805f --- /dev/null +++ b/src/test/__tests__/common/services/record/record.test.ts @@ -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(); + 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([['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(); + 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([['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([['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({}); + }); + }); +});