Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
s3fs committed Nov 21, 2024
1 parent ae36394 commit bf9e8ee
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/common/services/schema/schemaWithDuplicates.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class SchemaWithDuplicatesService implements ISchemaWithDuplicatesService
if (!constraints?.repeatable) return;

const updatedEntryUuid = uuidv4();
const updatedEntry = this.getCopiedEntry(entry, updatedEntryUuid, undefined);
const updatedEntry = this.getCopiedEntry(entry, updatedEntryUuid);
updatedEntry.children = this.getUpdatedChildren(children, updatedEntry);

const parentEntryUuid = getParentEntryUuid(path);
Expand Down Expand Up @@ -69,6 +69,7 @@ export class SchemaWithDuplicatesService implements ISchemaWithDuplicatesService
...parent.twinChildren,
[uri]: updatedTwinSiblings,
},
children: parent.children?.filter(child => child !== uuid),
});

this.updateDeletabilityAndPositioning(updatedTwinSiblings);
Expand Down
1 change: 1 addition & 0 deletions src/test/__mocks__/common/hooks/useServicesContext.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const schemaWithDuplicatesService = {
get: jest.fn(),
set: jest.fn(),
duplicateEntry: jest.fn(),
deleteEntry: jest.fn(),
} as unknown as ISchemaWithDuplicatesService;

export const lookupCacheService = {
Expand Down
37 changes: 37 additions & 0 deletions src/test/__tests__/common/hooks/useProfileSchema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import '@src/test/__mocks__/common/hooks/useServicesContext.mock';
import { schemaWithDuplicatesService } from '@src/test/__mocks__/common/hooks/useServicesContext.mock';
import { useProfileSchema } from '@common/hooks/useProfileSchema';
import { renderHook } from '@testing-library/react';
import { useSetRecoilState, useRecoilState } from 'recoil';

jest.mock('recoil');

describe('useProfileSchema', () => {
const entry = {
uri: 'mockUri',
path: ['testKey-0', 'testKey-2', 'testKey-4', 'testKey-6'],
uuid: 'testKey-6',
children: ['nonExistent', 'testKey-7'],
};

beforeEach(() => {
(useSetRecoilState as jest.Mock).mockImplementation(jest.fn);
(useRecoilState as jest.Mock).mockReturnValueOnce([new Set(), jest.fn()]);
});

test('get schema with copied entries', () => {
const { result } = renderHook(() => useProfileSchema());

result.current.getSchemaWithCopiedEntries(entry, []);

expect(schemaWithDuplicatesService.duplicateEntry).toHaveBeenCalled();
})

test('get schema with deleted entries', () => {
const { result } = renderHook(() => useProfileSchema());

result.current.getSchemaWithDeletedEntries(entry);

expect(schemaWithDuplicatesService.deleteEntry).toHaveBeenCalled();
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ describe('SchemaWithDuplicatesService', () => {

let schemaWithDuplicatesService: SchemaWithDuplicatesService;

beforeEach(() => {
const initServices = (altSchema: Schema = schema) => {
const selectedEntriesService = new SelectedEntriesService(selectedEntries);
schemaWithDuplicatesService = new SchemaWithDuplicatesService(schema, selectedEntriesService);
});
schemaWithDuplicatesService = new SchemaWithDuplicatesService(altSchema, selectedEntriesService);
};

describe('duplicateEntry', () => {
beforeEach(initServices);

const constraints = { repeatable: true } as Constraints;
const entry = {
path: ['testKey-0', 'testKey-2', 'testKey-4'],
Expand Down Expand Up @@ -123,4 +125,110 @@ describe('SchemaWithDuplicatesService', () => {
expect(schemaWithDuplicatesService.get()).toEqual(schema);
});
});

describe('deleteEntry', () => {
const entry = {
uri: 'mockUri',
path: ['testKey-0', 'testKey-2', 'testKey-4', 'testKey-6'],
uuid: 'testKey-6',
children: ['nonExistent', 'testKey-7'],
deletable: true,
};

const getSchema = (altEntry: SchemaEntry = entry, otherEntries: [string, SchemaEntry][] = []) =>
new Map([
['testKey-0', { path: ['testKey-0'], uuid: 'testKey-0', children: ['testKey-1', 'testKey-2'] }],
['testKey-1', { path: ['testKey-0', 'testKey-1'], uuid: 'testKey-1', children: ['testKey-3'] }],
['testKey-2', { path: ['testKey-0', 'testKey-2'], uuid: 'testKey-2', children: ['testKey-4'] }],
['testKey-3', { path: ['testKey-0', 'testKey-1', 'testKey-3'], uuid: 'testKey-3', children: [] }],
[
'testKey-4',
{
path: ['testKey-0', 'testKey-2', 'testKey-4'],
uuid: 'testKey-4',
children: ['testKey-5', altEntry.uuid],
twinChildren: { mockUri: ['testKey-5', altEntry.uuid] },
},
],
[
'testKey-5',
{
uri: 'mockUri',
path: ['testKey-0', 'testKey-2', 'testKey-4', 'testKey-5'],
uuid: 'testKey-5',
children: [],
deletable: true,
},
],
['testKey-6', altEntry],
...otherEntries,
]);

test('deletes an entry', () => {
initServices(
getSchema(entry, [
[
'testKey-7',
{
path: ['testKey-0', 'testKey-2', 'testKey-4', 'testKey-6', 'testKey-7'],
uuid: 'testKey-7',
children: [],
},
],
]),
);

const testResult = new Map([
['testKey-0', { path: ['testKey-0'], uuid: 'testKey-0', children: ['testKey-1', 'testKey-2'] }],
['testKey-1', { path: ['testKey-0', 'testKey-1'], uuid: 'testKey-1', children: ['testKey-3'] }],
['testKey-2', { path: ['testKey-0', 'testKey-2'], uuid: 'testKey-2', children: ['testKey-4'] }],
['testKey-3', { path: ['testKey-0', 'testKey-1', 'testKey-3'], uuid: 'testKey-3', children: [] }],
[
'testKey-4',
{
path: ['testKey-0', 'testKey-2', 'testKey-4'],
uuid: 'testKey-4',
children: ['testKey-5'],
twinChildren: { mockUri: ['testKey-5'] },
},
],
[
'testKey-5',
{
uri: 'mockUri',
path: ['testKey-0', 'testKey-2', 'testKey-4', 'testKey-5'],
uuid: 'testKey-5',
children: [],
deletable: false,
},
],
[
'testKey-5',
{
uri: 'mockUri',
path: ['testKey-0', 'testKey-2', 'testKey-4', 'testKey-5'],
uuid: 'testKey-5',
children: [],
cloneIndex: 0,
deletable: false,
},
],
]);

schemaWithDuplicatesService.deleteEntry(entry);

expect(schemaWithDuplicatesService.get()).toEqual(testResult);
});

test("doesn't delete an entry if it lack deletable property", () => {
const nonDeletableEntry = { ...entry, deletable: false };
const schema = getSchema(nonDeletableEntry);

initServices(schema);

schemaWithDuplicatesService.deleteEntry(nonDeletableEntry);

expect(schemaWithDuplicatesService.get()).toEqual(schema);
});
});
});

0 comments on commit bf9e8ee

Please sign in to comment.