-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0430707
commit 8133a1c
Showing
6 changed files
with
142 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/test/__tests__/common/hooks/useRecordGeneration.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({}); | ||
}); | ||
}); | ||
}); |