PageItemControl.spec.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import type {
  2. IPageInfoForEmpty,
  3. IPageInfoForOperation,
  4. } from '@growi/core/dist/interfaces';
  5. import { fireEvent, screen, within } from '@testing-library/dom';
  6. import { render } from '@testing-library/react';
  7. import { mock } from 'vitest-mock-extended';
  8. import { PageItemControl } from './PageItemControl';
  9. // mock for isIPageInfoForOperation and isIPageInfoForEmpty
  10. const mocks = vi.hoisted(() => ({
  11. isIPageInfoForOperationMock: vi.fn(),
  12. isIPageInfoForEmptyMock: vi.fn(),
  13. }));
  14. vi.mock('@growi/core/dist/interfaces', () => ({
  15. isIPageInfoForOperation: mocks.isIPageInfoForOperationMock,
  16. isIPageInfoForEmpty: mocks.isIPageInfoForEmptyMock,
  17. }));
  18. describe('PageItemControl.tsx', () => {
  19. describe('Should trigger onClickRenameMenuItem() when clicking the rename button', () => {
  20. it('without fetching PageInfo by useSWRxPageInfo', async () => {
  21. // setup
  22. const pageInfo = mock<IPageInfoForOperation>();
  23. const onClickRenameMenuItemMock = vi.fn();
  24. // return true when the argument is pageInfo in order to supress fetching
  25. mocks.isIPageInfoForOperationMock.mockImplementation((arg) => {
  26. if (arg === pageInfo) {
  27. return true;
  28. }
  29. });
  30. // return false for isIPageInfoForEmpty since we're using IPageInfoForOperation
  31. mocks.isIPageInfoForEmptyMock.mockReturnValue(false);
  32. const props = {
  33. pageId: 'dummy-page-id',
  34. isEnableActions: true,
  35. pageInfo,
  36. onClickRenameMenuItem: onClickRenameMenuItemMock,
  37. };
  38. render(<PageItemControl {...props} />);
  39. // when
  40. const button = within(
  41. screen.getByTestId('open-page-item-control-btn'),
  42. ).getByText(/more_vert/);
  43. fireEvent.click(button);
  44. const renameMenuItem = await screen.findByTestId('rename-page-btn');
  45. fireEvent.click(renameMenuItem);
  46. // then
  47. expect(onClickRenameMenuItemMock).toHaveBeenCalled();
  48. });
  49. it('with empty page (IPageInfoForEmpty)', async () => {
  50. // setup - Create an empty page mock with required properties
  51. const pageInfo: IPageInfoForEmpty = {
  52. emptyPageId: 'empty-page-id',
  53. isNotFound: false,
  54. isEmpty: true,
  55. isV5Compatible: true,
  56. isMovable: true, // Allow rename operation
  57. isDeletable: true,
  58. isAbleToDeleteCompletely: false,
  59. isRevertible: false,
  60. bookmarkCount: 0,
  61. isBookmarked: false,
  62. };
  63. const onClickRenameMenuItemMock = vi.fn();
  64. // return false for isIPageInfoForOperation since this is an empty page
  65. mocks.isIPageInfoForOperationMock.mockReturnValue(false);
  66. // return true when the argument is pageInfo (empty page)
  67. mocks.isIPageInfoForEmptyMock.mockImplementation((arg) => {
  68. if (arg === pageInfo) {
  69. return true;
  70. }
  71. return false;
  72. });
  73. const props = {
  74. pageId: 'dummy-page-id',
  75. isEnableActions: true,
  76. pageInfo,
  77. onClickRenameMenuItem: onClickRenameMenuItemMock,
  78. };
  79. render(<PageItemControl {...props} />);
  80. // when
  81. const button = within(
  82. screen.getByTestId('open-page-item-control-btn'),
  83. ).getByText(/more_vert/);
  84. fireEvent.click(button);
  85. const renameMenuItem = await screen.findByTestId('rename-page-btn');
  86. fireEvent.click(renameMenuItem);
  87. // then
  88. expect(onClickRenameMenuItemMock).toHaveBeenCalled();
  89. expect(onClickRenameMenuItemMock).toHaveBeenCalledWith(
  90. 'dummy-page-id',
  91. pageInfo,
  92. );
  93. });
  94. });
  95. });