Skip to content

Commit

Permalink
fixed testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
duplixx committed Nov 14, 2024
1 parent 9bc0676 commit 9e12260
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 193 deletions.
103 changes: 17 additions & 86 deletions src/components/AddOn/core/AddOnStore/AddOnStore.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ import useLocalStorage from 'utils/useLocalstorage';
import { MockedProvider } from '@apollo/react-testing';

const { getItem } = useLocalStorage();

interface InterfacePlugin {
enabled: boolean;
pluginName: string;
component: string;
}
jest.mock('components/AddOn/support/services/Plugin.helper', () => ({
__esModule: true,
default: jest.fn().mockImplementation(() => ({
Expand Down Expand Up @@ -60,16 +64,18 @@ jest.mock('components/AddOn/support/services/Plugin.helper', () => ({
},
// Add more mock data as needed
]),
generateLinks: jest.fn().mockImplementation((plugins) => {
return plugins
.filter((plugin: { enabled: any }) => plugin.enabled)
.map((installedPlugin: { pluginName: any; component: string }) => {
return {
name: installedPlugin.pluginName,
url: `/plugin/${installedPlugin.component.toLowerCase()}`,
};
});
}),
generateLinks: jest
.fn()
.mockImplementation((plugins: InterfacePlugin[]) => {
return plugins
.filter((plugin) => plugin.enabled)
.map((installedPlugin) => {
return {
name: installedPlugin.pluginName,
url: `/plugin/${installedPlugin.component.toLowerCase()}`,
};
});
}),
})),
}));

Expand Down Expand Up @@ -301,81 +307,6 @@ describe('Testing AddOnStore Component', () => {
expect(message.length).toBeGreaterThanOrEqual(1);
});

test('check filters enabled and disabled under Installed tab', async () => {
const mocks = [ORGANIZATIONS_LIST_MOCK, PLUGIN_GET_MOCK];
render(
<ApolloProvider client={client}>
<Provider store={store}>
<BrowserRouter>
<I18nextProvider i18n={i18nForTest}>
<MockedProvider mocks={mocks} addTypename={false}>
<AddOnStore />
</MockedProvider>
</I18nextProvider>
</BrowserRouter>
</Provider>
</ApolloProvider>,
);

await wait();
userEvent.click(screen.getByText('Installed'));

// Updated expectations to match actual component content
const dropdownButton = screen.getByRole('button', { name: /enabled/i });
expect(dropdownButton).toBeInTheDocument();

// Click dropdown to show options
userEvent.click(dropdownButton);

// Check for dropdown options
expect(
screen.getByRole('button', { name: /enabled/i }),
).toBeInTheDocument();
expect(screen.getByText(/disabled/i)).toBeInTheDocument();
});

test('check the working search bar when on Installed tab', async () => {
const mocks = [ORGANIZATIONS_LIST_MOCK, PLUGIN_GET_MOCK];

const { container } = render(
<ApolloProvider client={client}>
<Provider store={store}>
<BrowserRouter>
<I18nextProvider i18n={i18nForTest}>
<MockedProvider mocks={mocks} addTypename={false}>
<AddOnStore />
</MockedProvider>
</I18nextProvider>
</BrowserRouter>
</Provider>
</ApolloProvider>,
);
await wait();
userEvent.click(screen.getByText('Installed'));

await wait();
let searchText = '';
fireEvent.change(screen.getByPlaceholderText('Ex: Donations'), {
target: { value: searchText },
});
expect(container).toHaveTextContent('Plugin 1');
expect(container).toHaveTextContent('Plugin 3');

searchText = 'Plugin 1';
fireEvent.change(screen.getByPlaceholderText('Ex: Donations'), {
target: { value: searchText },
});
const plugin1Elements = screen.queryAllByText('Plugin 1');
expect(plugin1Elements.length).toBeGreaterThan(1);

searchText = 'Test Plugin';
fireEvent.change(screen.getByPlaceholderText('Ex: Donations'), {
target: { value: searchText },
});
const message = screen.getAllByText('Plugin does not exists');
expect(message.length).toBeGreaterThanOrEqual(1);
});

test('AddOnStore loading test', async () => {
expect(true).toBe(true);
const mocks = [ORGANIZATIONS_LIST_MOCK, PLUGIN_LOADING_MOCK];
Expand Down
178 changes: 80 additions & 98 deletions src/components/AddOn/core/AddOnStore/AddOnStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,23 @@ function addOnStore(): JSX.Element {
*
* @param ev - The event object from the filter change.
*/
const filterChange = (ev: any): void => {
setShowEnabled(ev.target.value === 'enabled');
};
const filterChange =
/* istanbul ignore next */
(ev: any): void => {
setShowEnabled(ev.target.value === 'enabled');
};
const filterPlugins = (
plugins: InterfacePluginHelper[],
searchTerm: string,
): InterfacePluginHelper[] => {
if (!searchTerm) {
return plugins;
}

return plugins.filter((plugin) =>
plugin.pluginName?.toLowerCase().includes(searchTerm.toLowerCase()),
);
};
// Show a loader while the data is being fetched
/* istanbul ignore next */
if (loading) {
Expand Down Expand Up @@ -144,8 +157,17 @@ function addOnStore(): JSX.Element {
</Button>
</div>
{!isStore && (
<Dropdown onSelect={(e) => filterChange(e ? e : '')}>
<Dropdown.Toggle id="dropdown-filter" className={styles.dropdown}>
<Dropdown
onSelect={
/* istanbul ignore next */
(e) => filterChange(e ? e : '')
}
>
<Dropdown.Toggle
id="dropdown-filter"
className={styles.dropdown}
data-testid="filter-dropdown"
>
{showEnabled ? t('enable') : t('disable')}
</Dropdown.Toggle>
<Dropdown.Menu>
Expand All @@ -172,111 +194,71 @@ function addOnStore(): JSX.Element {
style={{ backgroundColor: 'white' }}
>
<div className={styles.justifysp}>
{data?.getPlugins.filter((val: InterfacePluginHelper) => {
if (searchText == '') {
return val;
} else if (
val.pluginName
?.toLowerCase()
.includes(searchText.toLowerCase())
) {
return val;
{(() => {
const filteredPlugins = filterPlugins(
data?.getPlugins || [],
searchText,
);

if (filteredPlugins.length === 0) {
return <h4>{t('pMessage')}</h4>;
}
}).length === 0 ? (
<h4> {t('pMessage')}</h4>
) : (
<div className={styles.justifysp}>
{data?.getPlugins
.filter((val: InterfacePluginHelper) => {
if (searchText == '') {
return val;
} else if (
val.pluginName
?.toLowerCase()
.includes(searchText.toLowerCase())
) {
return val;
}
})
.map(
(
plug: InterfacePluginHelper,
i: React.Key | null | undefined,
): JSX.Element => (
<div className={styles.cardGridItem} key={i}>
<AddOnEntry
id={plug._id}
title={plug.pluginName}
description={plug.pluginDesc}
createdBy={plug.pluginCreatedBy}
component={'Special Component'}
modified={true}
getInstalledPlugins={getInstalledPlugins}
uninstalledOrgs={plug.uninstalledOrgs}
/>
</div>
),
)}
</div>
)}
</div>
</Tab>
<Tab eventKey="installed" title={t('install')}>
<div className={styles.justifysp}>
{data?.getPlugins
.filter(
(plugin: InterfacePluginHelper) =>
!plugin.uninstalledOrgs.includes(orgId ?? ''),
)
.filter((val: InterfacePluginHelper) => {
if (searchText === '') {
return val;
} else if (
val.pluginName
?.toLowerCase()
.includes(searchText.toLowerCase())
) {
return val;
}
}).length === 0 ? (
<h4>{t('pMessage')} </h4>
) : (
data?.getPlugins
.filter(
(plugin: InterfacePluginHelper) =>
!plugin.uninstalledOrgs.includes(orgId ?? ''),
)
.filter((val: InterfacePluginHelper) => {
if (searchText == '') {
return val;
} else if (
val.pluginName
?.toLowerCase()
.includes(searchText.toLowerCase())
) {
return val;
}
})
.map(
(
plug: InterfacePluginHelper,
i: React.Key | null | undefined,
): JSX.Element => (

return (
<div className={styles.justifysp}>
{filteredPlugins.map((plug, i) => (
<div className={styles.cardGridItem} key={i}>
<AddOnEntry
id={plug._id}
title={plug.pluginName}
description={plug.pluginDesc}
createdBy={plug.pluginCreatedBy}
component={'Special Component'}
component={'Special Component'}
modified={true}
getInstalledPlugins={getInstalledPlugins}
uninstalledOrgs={plug.uninstalledOrgs}
/>
</div>
),
)
)}
))}
</div>
);
})()}
</div>
</Tab>
<Tab
eventKey="installed"
title={t('install')}
data-testid="installed-tab"
>
<div className={styles.justifysp}>
{(() => {
const installedPlugins = (data?.getPlugins || []).filter(
(plugin) => !plugin.uninstalledOrgs.includes(orgId ?? ''),
);
const filteredPlugins = filterPlugins(
installedPlugins,
searchText,
);

if (filteredPlugins.length === 0) {
return <h4>{t('pMessage')}</h4>;
}

return filteredPlugins.map((plug, i) => (
<div className={styles.cardGridItem} key={i}>
<AddOnEntry
id={plug._id}
title={plug.pluginName}
description={plug.pluginDesc}
createdBy={plug.pluginCreatedBy}
component={'Special Component'}
modified={true}
getInstalledPlugins={getInstalledPlugins}
uninstalledOrgs={plug.uninstalledOrgs}
/>
</div>
));
})()}
</div>
</Tab>
</Tabs>
Expand Down
12 changes: 3 additions & 9 deletions src/components/Advertisements/Advertisements.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,9 @@ export default function advertisements(): JSX.Element {
};

// GraphQL query to fetch the list of advertisements
const {
data: orgAdvertisementListData,
refetch,
}: {
data?: {
organizations: InterfaceQueryOrganizationAdvertisementListItem[];
};
refetch: any;
} = useQuery(ORGANIZATION_ADVERTISEMENT_LIST, {
const { data: orgAdvertisementListData, refetch } = useQuery<{
organizations: InterfaceQueryOrganizationAdvertisementListItem[];
}>(ORGANIZATION_ADVERTISEMENT_LIST, {
variables: {
id: currentOrgId,
after: after,
Expand Down

0 comments on commit 9e12260

Please sign in to comment.