Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tests for src/components/AddOn/core/AddOnEntry/AddOnEntry.tsx #1106

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions src/components/AddOn/core/AddOnEntry/AddOnEntry.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ import { store } from 'state/store';
import { BACKEND_URL } from 'Constant/constant';
import i18nForTest from 'utils/i18nForTest';
import { I18nextProvider } from 'react-i18next';
import { debug } from 'jest-preview';
import userEvent from '@testing-library/user-event';
import { MockedProvider, wait } from '@apollo/react-testing';
import { StaticMockLink } from 'utils/StaticMockLink';
import { ADD_ON_ENTRY_MOCK } from './AddOnEntryMocks';
import { ToastContainer } from 'react-toastify';

const link = new StaticMockLink(ADD_ON_ENTRY_MOCK, true);

const httpLink = new HttpLink({
uri: BACKEND_URL,
Expand Down Expand Up @@ -95,4 +103,85 @@ describe('Testing AddOnEntry', () => {
expect(getByText('Test addon description')).toBeInTheDocument();
expect(getByText('Test User')).toBeInTheDocument();
});
it('Uninstall Button works correctly', async () => {
const props = {
id: '1',
title: 'Test Addon',
description: 'Test addon description',
createdBy: 'Test User',
component: 'string',
installed: true,
configurable: true,
modified: true,
isInstalled: true,
uninstalledOrgs: [],
enabled: true,
getInstalledPlugins: (): { sample: string } => {
return { sample: 'sample' };
},
};

const { findByText, getByTestId } = render(
<MockedProvider addTypename={false} link={link}>
<Provider store={store}>
<BrowserRouter>
<I18nextProvider i18n={i18nForTest}>
<ToastContainer />
{<AddOnEntry {...props} />}
</I18nextProvider>
</BrowserRouter>
</Provider>
</MockedProvider>
);
await wait(100);
const btn = getByTestId('AddOnEntry_btn_install');
await userEvent.click(btn);
await wait(100);
expect(btn.innerHTML).toMatch(/Install/i);
expect(
await findByText('This feature is now removed from your organization')
).toBeInTheDocument();
await userEvent.click(btn);
await wait(100);

expect(btn.innerHTML).toMatch(/Uninstall/i);
expect(
await findByText('This feature is now enabled in your organization')
).toBeInTheDocument();
});

it('Check if uninstalled orgs includes current org', async () => {
const props = {
id: '1',
title: 'Test Addon',
description: 'Test addon description',
createdBy: 'Test User',
component: 'string',
installed: true,
configurable: true,
modified: true,
isInstalled: true,
uninstalledOrgs: ['undefined'],
enabled: true,
getInstalledPlugins: (): { sample: string } => {
return { sample: 'sample' };
},
};

const { getByTestId } = render(
<MockedProvider addTypename={false} link={link}>
<Provider store={store}>
<BrowserRouter>
<I18nextProvider i18n={i18nForTest}>
{<AddOnEntry {...props} />}
</I18nextProvider>
</BrowserRouter>
</Provider>
</MockedProvider>
);
await wait(100);
const btn = getByTestId('AddOnEntry_btn_install');
expect(btn.innerHTML).toMatch(/install/i);
debug();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is debug() used here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used the debug() function to debug the test case using jest-preview but forgot to remove it, thank you for pointing this out. I'm removing it now.

});
});
25 changes: 25 additions & 0 deletions src/components/AddOn/core/AddOnEntry/AddOnEntryMocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { UPDATE_INSTALL_STATUS_PLUGIN_MUTATION } from 'GraphQl/Mutations/mutations';

// Mock data representing the response structure of the mutation
const updatePluginStatus = {
_id: '123',
pluginName: 'Sample Plugin',
pluginCreatedBy: 'John Doe',
pluginDesc: 'This is a sample plugin description.',
uninstalledOrgs: [],
};

// Creating the mock entry
export const ADD_ON_ENTRY_MOCK = [
{
request: {
query: UPDATE_INSTALL_STATUS_PLUGIN_MUTATION,
variables: { id: '1', orgId: 'undefined' },
},
result: {
data: {
updatePluginStatus: updatePluginStatus,
},
},
},
];
Loading