Skip to content

Commit

Permalink
added test cases and apis
Browse files Browse the repository at this point in the history
  • Loading branch information
akanshaaa19 committed Mar 5, 2025
1 parent e420a30 commit 5587c9d
Show file tree
Hide file tree
Showing 8 changed files with 379 additions and 46 deletions.
6 changes: 1 addition & 5 deletions src/components/UI/GoogleIntegration/GoogleIntegration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ interface GoogleIntegrationProps {
listItemName: string;
listItem: string;
redirectionLink: string;
title: string;
}

const permissionOptions = [
Expand All @@ -38,7 +37,6 @@ const permissionOptions = [
];

const GoogleIntegration = ({
type,
states,
icon,
formFields,
Expand All @@ -53,7 +51,6 @@ const GoogleIntegration = ({
listItemName,
listItem,
redirectionLink,
title,
}: GoogleIntegrationProps) => {
const { t } = useTranslation();

Expand All @@ -70,7 +67,6 @@ const GoogleIntegration = ({
<FormLayout
{...queries}
states={states}
title={title}
setPayload={setPayload}
setStates={setStates}
validationSchema={FormSchema}
Expand All @@ -83,7 +79,7 @@ const GoogleIntegration = ({
listItem={listItem}
icon={icon}
languageSupport={false}
backLinkButton="/sheet-integration"
backLinkButton={`/${redirectionLink}`}
/>
);
};
Expand Down
89 changes: 89 additions & 0 deletions src/containers/Certificates/Certificate.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { MockedProvider } from '@apollo/client/testing';
import { CERTIFICATE_LIST_MOCKS, CERTIFICATE_MOCKS } from 'mocks/Certificate';
import { MemoryRouter, Route, Routes } from 'react-router';
import CertificateList from './CertificatesList/CertificateList';
import Certificate from './Certificate';
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import * as Notification from 'common/notification';

const wrapper = (
<MockedProvider mocks={[...CERTIFICATE_LIST_MOCKS, ...CERTIFICATE_MOCKS]}>
<MemoryRouter initialEntries={['/certificates']}>
<Routes>
<Route path="/certificates" element={<CertificateList />} />
<Route path="/certificate/add" element={<Certificate />} />
<Route path="/certificate/:id/edit" element={<Certificate />} />
</Routes>
</MemoryRouter>
</MockedProvider>
);

const notificationSpy = vi.spyOn(Notification, 'setNotification');

describe('Certificate', () => {
test('should render Certificate list', async () => {
render(wrapper);

await waitFor(() => {
expect(screen.getByText('Certificates')).toBeInTheDocument();
});
});

test('should edit a certificate', async () => {
render(wrapper);

await waitFor(() => {
expect(screen.getByText('Certificates')).toBeInTheDocument();
});

fireEvent.click(screen.getAllByTestId('EditIcon')[0]);

await waitFor(() => {
expect(screen.getByText('Edit Certificate')).toBeInTheDocument();
});

fireEvent.change(screen.getAllByRole('textbox')[0], { target: { value: 'new label' } });
fireEvent.click(screen.getByText('Save'));

await waitFor(() => {
expect(notificationSpy).toBeCalledWith('Certificate edited successfully!');
});
});

test('should create a certificate', async () => {
render(wrapper);

await waitFor(() => {
expect(screen.getByText('Certificates')).toBeInTheDocument();
});

fireEvent.click(screen.getByTestId('newItemButton'));

await waitFor(() => {
expect(screen.getByText('Add a new Certificate')).toBeInTheDocument();
});

const inputs = screen.getAllByRole('textbox');

fireEvent.change(inputs[0], { target: { value: 'label' } });
fireEvent.change(inputs[1], { target: { value: 'description' } });

// testing url validation
fireEvent.change(inputs[2], { target: { value: 'url' } });
fireEvent.click(screen.getByText('Save'));
await waitFor(() => {
expect(screen.getByText('Invalid URL')).toBeInTheDocument();
});
fireEvent.change(inputs[2], {
target: {
value: 'https://docs.google.com/presentation/d/1fBrDFDCD2iwnaKg8sxKd45lRbqLuBFvsZbSH1sjm7aI/edit#slide=id.p',
},
});

fireEvent.click(screen.getByText('Save'));

await waitFor(() => {
expect(notificationSpy).toBeCalledWith('Certificate created successfully!');
});
});
});
33 changes: 22 additions & 11 deletions src/containers/Certificates/Certificate.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { Input } from 'components/UI/Form/Input/Input';
import GoogleIntegration from 'components/UI/GoogleIntegration/GoogleIntegration';
import { CREATE_SHEET, DELETE_SHEET, UPDATE_SHEET } from 'graphql/mutations/Sheet';
import { GET_SHEET } from 'graphql/queries/Sheet';
import { useState } from 'react';
import { useTranslation } from 'react-i18next';
import * as Yup from 'yup';
import CertificateIcon from 'assets/images/Certificate.svg?react';
import { GET_CERTIFICATE } from 'graphql/queries/Certificate';
import { CREATE_CERTIFICATE, DELETE_CERTIFICATE, UPDATE_CERTIFICATE } from 'graphql/mutations/Certificate';

const regex = /^https:\/\/docs\.google\.com\/presentation\/d\/[a-zA-Z0-9_-]+(?:\/.*)?$/;
const dialogMessage = "You won't be able to use this certificate again.";
const icon = <CertificateIcon />;

const queries = {
getItemQuery: GET_SHEET,
createItemQuery: CREATE_SHEET,
updateItemQuery: UPDATE_SHEET,
deleteItemQuery: DELETE_SHEET,
getItemQuery: GET_CERTIFICATE,
createItemQuery: CREATE_CERTIFICATE,
updateItemQuery: UPDATE_CERTIFICATE,
deleteItemQuery: DELETE_CERTIFICATE,
};

const Certificate = () => {
Expand Down Expand Up @@ -61,22 +61,33 @@ const Certificate = () => {
},
];

const setStates = () => {};
const setPayload = () => {};
const setStates = ({ label: labelvalue, description: descriptionValue, url: urlValue }: any) => {
setLabel(labelvalue);
setDescription(descriptionValue);
setUrl(urlValue);
};
const setPayload = ({ label: labelvalue, description: descriptionValue, url: urlValue }: any) => {
const payload = {
label: labelvalue,
description: descriptionValue,
url: urlValue,
};

return payload;
};

return (
<GoogleIntegration
type="slides"
title="Certificate"
states={states}
formSchema={formSchema}
formFields={formFields}
setPayload={setPayload}
setStates={setStates}
dialogMessage={dialogMessage}
listItemName="Certificate"
listItem="certificate"
redirectionLink="custom-certificates"
listItem="certificateTemplate"
redirectionLink="certificates"
icon={icon}
{...queries}
/>
Expand Down
72 changes: 43 additions & 29 deletions src/containers/Certificates/CertificatesList/CertificateList.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
import { useState } from 'react';
import { useTranslation } from 'react-i18next';

import CertificateIcon from 'assets/images/Certificate.svg?react';
import { GET_SHEET_COUNT, GET_SHEETS } from 'graphql/queries/Sheet';
import { DELETE_SHEET, SYNC_SHEET } from 'graphql/mutations/Sheet';
import { List } from 'containers/List/List';

import styles from './CertificateList.module.css';
import { certificatesInfo } from 'common/HelpData';
import { COUNT_CERTIFICATES, LIST_CERTIFICATES } from 'graphql/queries/Certificate';
import { DELETE_CERTIFICATE } from 'graphql/mutations/Certificate';
import { copyToClipboardMethod } from 'common/utils';
import { setNotification } from 'common/notification';
import CopyAllOutlined from 'assets/images/icons/Flow/Copy.svg?react';

const queries = {
countQuery: GET_SHEET_COUNT,
filterItemsQuery: GET_SHEETS,
deleteItemQuery: DELETE_SHEET,
countQuery: COUNT_CERTIFICATES,
filterItemsQuery: LIST_CERTIFICATES,
deleteItemQuery: DELETE_CERTIFICATE,
};

const getLabel = (label: string) => <p>{label}</p>;
const getDescription = (label?: string) => <p>Description of the Certificate</p>;
const getDescription = (description: string) => (
<p>{description.length < 100 ? description : `${description.slice(0, 100)}...`}</p>
);

const columnStyles = [styles.Label, styles.Description, styles.Actions];
const certificateIcon = <CertificateIcon className={styles.DarkIcon} />;

export const SheetIntegrationList = () => {
export const CertificateList = () => {
const { t } = useTranslation();

const [warnings, setWarnings] = useState<any>({});
const [showdialog, setShowDialog] = useState(false);

let dialog;

const getColumns: any = ({ label, description }: any) => ({
label: getLabel(label),
description: getDescription(description),
Expand All @@ -44,23 +43,38 @@ export const SheetIntegrationList = () => {
columnStyles,
};

const copyUuid = (_id: string, item: any) => {
if (item.id) {
copyToClipboardMethod(item.id);

Check warning on line 48 in src/containers/Certificates/CertificatesList/CertificateList.tsx

View check run for this annotation

Codecov / codecov/patch

src/containers/Certificates/CertificatesList/CertificateList.tsx#L48

Added line #L48 was not covered by tests
} else {
setNotification('Sorry! UUID not found', 'warning');

Check warning on line 50 in src/containers/Certificates/CertificatesList/CertificateList.tsx

View check run for this annotation

Codecov / codecov/patch

src/containers/Certificates/CertificatesList/CertificateList.tsx#L50

Added line #L50 was not covered by tests
}
};

const additionalAction = () => [
{
label: t('Copy UUID'),
icon: <CopyAllOutlined data-testid="copy-icon" />,
parameter: 'id',
dialog: copyUuid,
},
];

return (
<>
{dialog}
<List
helpData={certificatesInfo}
title={t('Certificates')}
listItem="sheets"
listItemName="Sheet"
pageLink="certificates"
listIcon={certificateIcon}
dialogMessage={dialogMessage}
{...queries}
{...columnAttributes}
button={{ show: true, label: t('Create') }}
/>
</>
<List
helpData={certificatesInfo}
title={t('Certificates')}
listItem="certificateTemplates"
listItemName="Certificate"
pageLink="certificate"
listIcon={certificateIcon}
dialogMessage={dialogMessage}
additionalAction={additionalAction}
button={{ show: true, label: t('Create') }}
{...queries}
{...columnAttributes}
/>
);
};

export default SheetIntegrationList;
export default CertificateList;
45 changes: 45 additions & 0 deletions src/graphql/mutations/Certificate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { gql } from '@apollo/client';

export const CREATE_CERTIFICATE = gql`
mutation CreateCertificateTemplate($input: CertificateTemplateInput!) {
createCertificateTemplate(input: $input) {
certificateTemplate {
label
id
}
errors {
key
message
}
}
}
`;

export const UPDATE_CERTIFICATE = gql`
mutation UpdateCertificateTemplate($id: ID!, $input: CertificateTemplateInput!) {
updateCertificateTemplate(id: $id, input: $input) {
certificateTemplate {
id
label
}
errors {
message
key
}
}
}
`;

export const DELETE_CERTIFICATE = gql`
mutation DeleteCertificateTemplate($id: ID!) {
deleteCertificateTemplate(id: $id) {
certificateTemplate {
label
}
errors {
key
message
}
}
}
`;
37 changes: 37 additions & 0 deletions src/graphql/queries/Certificate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { gql } from '@apollo/client';

export const LIST_CERTIFICATES = gql`
query CertificateTemplates($filter: CertificateTemplateFilter, $opts: Opts) {
certificateTemplates(filter: $filter, opts: $opts) {
id
description
label
type
url
}
}
`;

export const COUNT_CERTIFICATES = gql`
query RootQueryType($filter: CertificateTemplateFilter) {
countCertificateTemplates(filter: $filter)
}
`;

export const GET_CERTIFICATE = gql`
query CertificateTemplate($id: ID!) {
certificateTemplate(id: $id) {
certificateTemplate {
id
label
type
url
description
}
errors {
message
key
}
}
}
`;
Loading

0 comments on commit 5587c9d

Please sign in to comment.