Skip to content

Commit

Permalink
Improved Code Coverage in src/components/UserPortal/OrganizationNavba…
Browse files Browse the repository at this point in the history
…r/OrganizationNavbar.tsx (#3127)
  • Loading branch information
aadhil2k4 authored Jan 3, 2025
1 parent bb7e4d7 commit 2215c0a
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ import { vi } from 'vitest';
* 9. **Plugin removal on uninstallation**: Ensures plugins are removed when uninstalled for the organization.
* 10. **Rendering plugins when not uninstalled**: Ensures plugins render if not uninstalled.
* 11. **No changes for unmatched plugin**: Ensures no changes when an unrecognized plugin update occurs.
* 12. **Should handle logout properly**: Ensures that local storage is cleared and user is redirected to home screen when logout button is clicked
* 13. **Should navigate to home page on home link click**: Ensures that browser history is correctly updated and navigates to oraganization home page.
* 14. **Should use fallback "en" when cookies.get returns null: Ensures component fallsback to "en" language when i18next cookie is absent.
*
* Mocked GraphQL queries and subscriptions simulate backend behavior.
*/
Expand Down Expand Up @@ -473,4 +476,96 @@ describe('Testing OrganizationNavbar Component [User Portal]', () => {

await wait();
});

it('Should handle logout properly', async () => {
const mockStorage = {
clear: vi.fn(),
getItem: vi.fn((key: 'name' | 'talawaPlugins') => {
const items = {
name: JSON.stringify('Test User'),
talawaPlugins: JSON.stringify([]),
};
return items[key] || null;
}),
setItem: vi.fn(),
removeItem: vi.fn(),
length: 0,
key: vi.fn(),
};
Object.defineProperty(window, 'localStorage', {
value: mockStorage,
});
const mockLocation = {
replace: vi.fn(),
};
Object.defineProperty(window, 'location', {
value: mockLocation,
writable: true,
});
render(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<OrganizationNavbar {...navbarProps} />
</I18nextProvider>
</Provider>
</BrowserRouter>
</MockedProvider>,
);
await wait();
userEvent.click(screen.getByTestId('personIcon'));
userEvent.click(screen.getByTestId('logoutBtn'));
expect(mockStorage.clear).toHaveBeenCalled();
expect(mockLocation.replace).toHaveBeenCalledWith('/');
});

it('Should navigate to home page on home link click', async () => {
const history = createMemoryHistory({
initialEntries: ['/initial'],
});
render(
<MockedProvider addTypename={false} link={link}>
<Router location={history.location} navigator={history}>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<OrganizationNavbar {...navbarProps} />
</I18nextProvider>
</Provider>
</Router>
</MockedProvider>,
);
const homeLink = screen.getByText('Home');
expect(homeLink).toBeInTheDocument();
userEvent.click(homeLink);
await wait();
expect(history.location.pathname).toBe(
`/user/organization/${organizationId}`,
);
});
});

describe('Testing OrganizationNavbar Cookie Fallback', () => {
beforeEach(() => {
vi.clearAllMocks();
});

it('should use fallback "en" when cookies.get returns null', async () => {
vi.spyOn(cookies, 'get').mockReturnValue(
null as unknown as { [key: string]: string },
);
render(
<MockedProvider>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<OrganizationNavbar currentPage="home" />
</I18nextProvider>
</Provider>
</BrowserRouter>
</MockedProvider>,
);
expect(cookies.get).toHaveBeenCalledWith('i18next');
expect(screen.getByText('Home')).toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ function organizationNavbar(props: InterfaceNavbarProps): JSX.Element {
});

const [currentLanguageCode, setCurrentLanguageCode] = React.useState(
/* istanbul ignore next */
cookies.get('i18next') || 'en',
);

Expand All @@ -71,7 +70,6 @@ function organizationNavbar(props: InterfaceNavbarProps): JSX.Element {
/**
* Handles user logout by clearing local storage and redirecting to the home page.
*/
/* istanbul ignore next */
const handleLogout = (): void => {
localStorage.clear();
window.location.replace('/');
Expand Down Expand Up @@ -142,6 +140,7 @@ function organizationNavbar(props: InterfaceNavbarProps): JSX.Element {
console.log(`Plugin ${pluginName} is not present.`);
}
} else {
/* istanbul ignore else -- @preserve */
if (pluginIndexToRemove != -1) {
plugins[pluginIndexToRemove].view = true;
setItem('talawaPlugins', JSON.stringify(plugins));
Expand Down Expand Up @@ -174,10 +173,7 @@ function organizationNavbar(props: InterfaceNavbarProps): JSX.Element {
<Nav className="me-auto flex-grow-1 pe-3 pt-1" variant="dark">
<Nav.Link
active={props.currentPage === 'home'}
onClick={
/* istanbul ignore next */
(): void => navigate(homeLink)
}
onClick={(): void => navigate(homeLink)}
>
{t('home')}
</Nav.Link>
Expand Down

0 comments on commit 2215c0a

Please sign in to comment.