Skip to content

Commit

Permalink
feat(bul-import): fixing typescript error
Browse files Browse the repository at this point in the history
  • Loading branch information
its-mitesh-kumar committed Dec 13, 2024
1 parent 6d19980 commit 54ca224
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@
export function getNestedValue<T>(obj: T, path: string): any {
return path
.split('.')
.reduce((acc, key) => (acc && acc[key] ? acc[key] : undefined), obj);
.reduce(
(acc, key) =>
acc && (acc as Record<string, any>)[key]
? (acc as Record<string, any>)[key]
: undefined,
obj,
);
}
export enum SortingOrderEnum {
ASC = 'asc',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type FindAllImportsResponse =

function sortImports(
imports: Components.Schemas.Import[],
sortColumn: string,
sortColumn: string = 'repository.name',
sortOrder: string = SortingOrderEnum.ASC,
) {
imports.sort((a, b) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ import {
mockGetRepositories,
mockSelectedRepositories,
} from '../mocks/mockData';
import { ApprovalTool, RepositoryStatus } from '../types';
import {
AddedRepositoryColumnNameEnum,
ApprovalTool,
RepositoryStatus,
SortingOrderEnum,
} from '../types';
import { prepareDataForSubmission } from '../utils/repository-utils';
import {
BulkImportAPI,
Expand Down Expand Up @@ -304,12 +309,24 @@ describe('BulkImportBackendClient', () => {

describe('getImportJobs', () => {
it('getImportJobs should retrieve the import jobs successfully', async () => {
const jobs = await bulkImportApi.getImportJobs(1, 2, '');
const jobs = await bulkImportApi.getImportJobs(
1,
2,
'',
AddedRepositoryColumnNameEnum.repoName,
SortingOrderEnum.ASC,
);
expect(jobs).toEqual(mockGetImportJobs);
});

it('getImportJobs should retrieve the import jobs based on search string', async () => {
const jobs = await bulkImportApi.getImportJobs(1, 2, 'cup');
const jobs = await bulkImportApi.getImportJobs(
1,
2,
'cup',
AddedRepositoryColumnNameEnum.repoName,
SortingOrderEnum.ASC,
);
expect(jobs).toEqual(
mockGetImportJobs.imports.filter(r =>
r.repository.name?.includes('cup'),
Expand All @@ -318,9 +335,15 @@ describe('BulkImportBackendClient', () => {
});

it('getImportJobs should handle non-200/204 responses correctly', async () => {
await expect(bulkImportApi.getImportJobs(1, 2, '')).resolves.toEqual(
expect.objectContaining([]),
);
await expect(
bulkImportApi.getImportJobs(
1,
2,
'',
AddedRepositoryColumnNameEnum.repoName,
SortingOrderEnum.ASC,
),
).resolves.toEqual(expect.objectContaining([]));
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
AddRepositoryData,
SortingOrderEnum,
} from '../../types';
import { getComparator } from '../../utils/repository-utils';
import { RepositoriesHeader } from '../AddRepositories/RepositoriesHeader';
import { AddedRepositoriesTableBody } from './AddedRepositoriesTableBody';
import DeleteRepositoryDialog from './DeleteRepositoryDialog';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import { renderHook, waitFor } from '@testing-library/react';

import { mockGetImportJobs, mockGetRepositories } from '../mocks/mockData';
import { AddedRepositoryColumnNameEnum, SortingOrderEnum } from '../types';
import { useAddedRepositories } from './useAddedRepositories';

jest.mock('@backstage/core-plugin-api', () => ({
Expand Down Expand Up @@ -48,7 +49,15 @@ jest.mock('formik', () => ({

describe('useAddedRepositories', () => {
it('should return import jobs', async () => {
const { result } = renderHook(() => useAddedRepositories(1, 5, ''));
const { result } = renderHook(() =>
useAddedRepositories(
1,
5,
'',
AddedRepositoryColumnNameEnum.repoName,
SortingOrderEnum.ASC,
),
);
await waitFor(() => {
expect(result.current.loading).toBeFalsy();
expect(result.current.data.totalJobs).toBe(4);
Expand Down

0 comments on commit 54ca224

Please sign in to comment.