NotAvailableForReadOnlyUser.spec.tsx 3.0 KB

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