NotAvailableForReadOnlyUser.spec.tsx 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import '@testing-library/jest-dom/vitest';
  2. import { render, screen } from '@testing-library/react';
  3. import {
  4. describe, it, expect, vi,
  5. } from 'vitest';
  6. import { NotAvailableIfReadOnlyUserNotAllowedToComment } from './NotAvailableForReadOnlyUser';
  7. const useIsReadOnlyUser = vi.hoisted(() => vi.fn().mockReturnValue({ data: true }));
  8. const useIsRomUserAllowedToComment = vi.hoisted(() => vi.fn().mockReturnValue({ data: true }));
  9. vi.mock('~/stores-universal/context', () => ({
  10. useIsReadOnlyUser,
  11. useIsRomUserAllowedToComment,
  12. }));
  13. describe('NotAvailableForReadOnlyUser.tsx', () => {
  14. it('renders NotAvailable component as enable when user is read-only and comments by rom users is allowed', async() => {
  15. useIsReadOnlyUser.mockReturnValue({ data: true });
  16. useIsRomUserAllowedToComment.mockReturnValue({ data: true });
  17. render(
  18. <NotAvailableIfReadOnlyUserNotAllowedToComment>
  19. <div data-testid="test-child">Test Child</div>
  20. </NotAvailableIfReadOnlyUserNotAllowedToComment>,
  21. );
  22. // when
  23. const element = screen.getByTestId('test-child');
  24. const wrapperElement = element.parentElement;
  25. // then
  26. expect(wrapperElement).not.toHaveAttribute('aria-hidden', 'true');
  27. });
  28. it('renders NotAvailable component as disable when user is read-only and comments by rom users is not allowed', async() => {
  29. useIsReadOnlyUser.mockReturnValue({ data: true });
  30. useIsRomUserAllowedToComment.mockReturnValue({ data: false });
  31. render(
  32. <NotAvailableIfReadOnlyUserNotAllowedToComment>
  33. <div data-testid="test-child">Test Child</div>
  34. </NotAvailableIfReadOnlyUserNotAllowedToComment>,
  35. );
  36. // when
  37. const element = screen.getByTestId('test-child');
  38. const wrapperElement = element.parentElement;
  39. // then
  40. expect(wrapperElement).toHaveAttribute('aria-hidden', 'true');
  41. });
  42. it('renders NotAvailable component as enable when user is not read-only and comments by rom users is allowed', async() => {
  43. useIsReadOnlyUser.mockReturnValue({ data: false });
  44. useIsRomUserAllowedToComment.mockReturnValue({ data: true });
  45. render(
  46. <NotAvailableIfReadOnlyUserNotAllowedToComment>
  47. <div data-testid="test-child">Test Child</div>
  48. </NotAvailableIfReadOnlyUserNotAllowedToComment>,
  49. );
  50. // when
  51. const element = screen.getByTestId('test-child');
  52. const wrapperElement = element.parentElement;
  53. // then
  54. expect(wrapperElement).not.toHaveAttribute('aria-hidden', 'true');
  55. });
  56. it('renders NotAvailable component as enable when user is not read-only and comments by rom users is not allowed', async() => {
  57. useIsReadOnlyUser.mockReturnValue({ data: false });
  58. useIsRomUserAllowedToComment.mockReturnValue({ data: false });
  59. render(
  60. <NotAvailableIfReadOnlyUserNotAllowedToComment>
  61. <div data-testid="test-child">Test Child</div>
  62. </NotAvailableIfReadOnlyUserNotAllowedToComment>,
  63. );
  64. // when
  65. const element = screen.getByTestId('test-child');
  66. const wrapperElement = element.parentElement;
  67. // then
  68. expect(wrapperElement).not.toHaveAttribute('aria-hidden', 'true');
  69. });
  70. });