UserInfo.spec.tsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import React from 'react';
  2. import { screen } from '@testing-library/react';
  3. import { render } from 'lib/testHelpers';
  4. import UserInfo from 'components/NavBar/UserInfo/UserInfo';
  5. import { useUserInfo } from 'lib/hooks/useUserInfo';
  6. import userEvent from '@testing-library/user-event';
  7. jest.mock('lib/hooks/useUserInfo', () => ({
  8. useUserInfo: jest.fn(),
  9. }));
  10. describe('UserInfo', () => {
  11. const renderComponent = () => render(<UserInfo />);
  12. it('should render the userInfo with correct data', () => {
  13. const username = 'someName';
  14. (useUserInfo as jest.Mock).mockImplementation(() => ({ username }));
  15. renderComponent();
  16. expect(screen.getByText(username)).toBeInTheDocument();
  17. });
  18. it('should render the userInfo during click opens the dropdown', async () => {
  19. const username = 'someName';
  20. Object.defineProperty(window, 'basePath', {
  21. value: '',
  22. writable: true,
  23. });
  24. (useUserInfo as jest.Mock).mockImplementation(() => ({ username }));
  25. renderComponent();
  26. const dropdown = screen.getByText(username);
  27. await userEvent.click(dropdown);
  28. const logout = screen.getByText('Log out');
  29. expect(logout).toBeInTheDocument();
  30. });
  31. it('should render correct url during basePath initialization', async () => {
  32. const username = 'someName';
  33. const baseUrl = '/path';
  34. Object.defineProperty(window, 'basePath', {
  35. value: baseUrl,
  36. writable: true,
  37. });
  38. (useUserInfo as jest.Mock).mockImplementation(() => ({ username }));
  39. renderComponent();
  40. const logout = screen.getByText('Log out');
  41. expect(logout).toBeInTheDocument();
  42. });
  43. it('should not render anything if the username does not exists', () => {
  44. (useUserInfo as jest.Mock).mockImplementation(() => ({
  45. username: undefined,
  46. }));
  47. renderComponent();
  48. expect(screen.queryByRole('listbox')).not.toBeInTheDocument();
  49. });
  50. });