Skip to content

Commit

Permalink
fix: optimization with promise all, add new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikvagner committed Nov 26, 2024
1 parent d85f4e9 commit ac35cac
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { render } from '@testing-library/react';
import {
ReactQueryTestWrapper,
defaultContentItemWithSnapshot,
defaultSnapshotItem,
defaultTemplateItem,
defaultTemplateItem2,
} from 'testingHelpers';
import DeleteSnapshotsModal from './DeleteSnapshotsModal';
import {DELETE_ROUTE} from 'Routes/constants';
import {useGetSnapshotList} from 'services/Content/ContentQueries';
import {useFetchTemplatesForSnapshots} from 'services/Templates/TemplateQueries';
import { TemplateItem } from 'services/Templates/TemplateApi'
import {formatDateDDMMMYYYY} from 'helpers';
import { DELETE_ROUTE } from 'Routes/constants';
import { useGetSnapshotList } from 'services/Content/ContentQueries';
import { useFetchTemplatesForSnapshots } from 'services/Templates/TemplateQueries';
import { TemplateItem } from 'services/Templates/TemplateApi';
import { formatDateDDMMMYYYY } from 'helpers';

jest.mock('react-query', () => ({
...jest.requireActual('react-query'),
Expand All @@ -19,6 +20,7 @@ jest.mock('react-query', () => ({

jest.mock('react-router-dom', () => ({
useNavigate: jest.fn(),
useParams: () => ({ repoUUID: defaultContentItemWithSnapshot.uuid }),
useLocation: () => ({
search: `${DELETE_ROUTE}?snapshotUUID=${defaultSnapshotItem.uuid}`,
}),
Expand All @@ -29,7 +31,7 @@ jest.mock('../SnapshotListModal', () => ({
useSnapshotListOutletContext: () => ({
clearCheckedRepositories: () => undefined,
deletionContext: {
checkedRepositories: new Set<string>(defaultSnapshotItem.uuid),
checkedSnapshots: new Set<string>([defaultSnapshotItem.uuid]),
},
}),
}));
Expand All @@ -52,18 +54,17 @@ jest.mock('middleware/AppContext', () => ({
}),
}));

//
it('Render delete modal where repo is not included in any templates', () => {

it('Render delete modal where snapshot is not included in any templates', () => {
(useGetSnapshotList as jest.Mock).mockImplementation(() => ({
data: {
isLoading: false,
data: [defaultSnapshotItem],
},
}));
(useFetchTemplatesForSnapshots as jest.Mock).mockImplementation(() => ({
data: {
data: new Map<string, TemplateItem[]>([[defaultSnapshotItem.uuid, []]]),
},
isError: false,
data: new Map<string, TemplateItem[]>([[defaultSnapshotItem.uuid, []]]),
}));

const { queryByText } = render(
Expand All @@ -72,8 +73,33 @@ it('Render delete modal where repo is not included in any templates', () => {
</ReactQueryTestWrapper>,
);

expect(queryByText(formatDateDDMMMYYYY(defaultSnapshotItem.created_at))).toBeInTheDocument();
expect(queryByText(formatDateDDMMMYYYY(defaultSnapshotItem.created_at, true))).toBeInTheDocument();
expect(queryByText(defaultTemplateItem.name)).not.toBeInTheDocument();
expect(queryByText(defaultTemplateItem2.name)).not.toBeInTheDocument();
expect(queryByText('None')).toBeInTheDocument();
});

it('Render delete modal where snapshot is included in templates', () => {
(useGetSnapshotList as jest.Mock).mockImplementation(() => ({
data: {
isLoading: false,
data: [defaultSnapshotItem],
},
}));
(useFetchTemplatesForSnapshots as jest.Mock).mockImplementation(() => ({
isError: false,
data: new Map<string, TemplateItem[]>([[defaultSnapshotItem.uuid, [defaultTemplateItem, defaultTemplateItem2]]]),
}));

const { queryByText } = render(
<ReactQueryTestWrapper>
<DeleteSnapshotsModal />
</ReactQueryTestWrapper>,
);

expect(queryByText(formatDateDDMMMYYYY(defaultSnapshotItem.created_at, true))).toBeInTheDocument();
expect(queryByText(defaultTemplateItem.name)).toBeInTheDocument();
expect(queryByText(defaultTemplateItem2.name)).toBeInTheDocument();
expect(queryByText('Some snapshots have associated templates.')).toBeInTheDocument();
expect(queryByText('None')).not.toBeInTheDocument();
});
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,11 @@ export default function DeleteSnapshotsModal() {

useEffect(() => {
if (snapshots && templates) {
if (templates.values().some((t) => t.length)) {
setAffectsTemplates(true);
for (const t of templates.values()) {
if (t.length) {
setAffectsTemplates(true);
break;
}
}
setIsLoading(false);
}
Expand Down
21 changes: 14 additions & 7 deletions src/services/Templates/TemplateApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,18 +163,25 @@ export const getTemplateSnapshots: (
export const getTemplatesForSnapshots: (
snapshotUuids: string[],
) => Promise<Map<string, TemplateItem[]>> = async (snapshotUuids: string[]) => {
const map = new Map<string, TemplateItem[]>();
for (const s of snapshotUuids) {
const { data: templateResp } = await axios.get<TemplateCollectionResponse>(
const requests = snapshotUuids.map((s) =>
axios.get<TemplateCollectionResponse>(
`/api/content-sources/v1/templates/?${objectToUrlParams({
offset: '0',
limit: '-1',
snapshot_uuids: s,
})}`,
);
map.set(s, templateResp.data);
}
return map;
),
);

return axios.all(requests).then(
(responses) =>
new Map<string, TemplateItem[]>(
responses.map((response) => {
const uuid: string = response.request.responseURL.split('=').pop() ?? '';
return [uuid, response.data.data];
}),
),
);
};

export const EditTemplate: (request: EditTemplateRequest) => Promise<void> = async (request) => {
Expand Down

0 comments on commit ac35cac

Please sign in to comment.