From 0216cd57e8231d62011409e15a230d5e6211831c Mon Sep 17 00:00:00 2001 From: Kevin Silva Date: Fri, 25 Aug 2023 13:44:06 +0200 Subject: [PATCH] SONAR-20255 - Indexation migration enzyme to rtl --- .../IndexationContextProvider-test.tsx | 44 +++---- .../__tests__/IndexationNotification-test.tsx | 118 +++++++++++------- .../PageUnavailableDueToIndexation-test.tsx | 43 +++---- .../IndexationContextProvider-test.tsx.snap | 25 ---- ...geUnavailableDueToIndexation-test.tsx.snap | 35 ------ 5 files changed, 113 insertions(+), 152 deletions(-) delete mode 100644 server/sonar-web/src/main/js/app/components/indexation/__tests__/__snapshots__/IndexationContextProvider-test.tsx.snap delete mode 100644 server/sonar-web/src/main/js/app/components/indexation/__tests__/__snapshots__/PageUnavailableDueToIndexation-test.tsx.snap diff --git a/server/sonar-web/src/main/js/app/components/indexation/__tests__/IndexationContextProvider-test.tsx b/server/sonar-web/src/main/js/app/components/indexation/__tests__/IndexationContextProvider-test.tsx index d5f09e7d59b..71e9474b659 100644 --- a/server/sonar-web/src/main/js/app/components/indexation/__tests__/IndexationContextProvider-test.tsx +++ b/server/sonar-web/src/main/js/app/components/indexation/__tests__/IndexationContextProvider-test.tsx @@ -18,10 +18,13 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -import { mount } from 'enzyme'; import * as React from 'react'; +import { useContext } from 'react'; import { mockAppState } from '../../../../helpers/testMocks'; +import { renderComponent } from '../../../../helpers/testReactTestingUtils'; +import { byText } from '../../../../helpers/testSelector'; import { IndexationStatus } from '../../../../types/indexation'; +import { IndexationContext } from '../IndexationContext'; import { IndexationContextProvider, IndexationContextProviderProps, @@ -32,29 +35,21 @@ beforeEach(() => jest.clearAllMocks()); jest.mock('../IndexationNotificationHelper'); -it('should render correctly and start polling if issue sync is needed', () => { - const wrapper = mountRender(); - - expect(wrapper).toMatchSnapshot(); +it('should render correctly, start polling if issue sync is needed and stop when unmounted', () => { + const { unmount } = renderIndexationContextProvider(); expect(IndexationNotificationHelper.startPolling).toHaveBeenCalled(); + unmount(); + expect(IndexationNotificationHelper.stopPolling).toHaveBeenCalled(); }); it('should not start polling if no issue sync is needed', () => { const appState = mockAppState({ needIssueSync: false }); - const wrapper = mountRender({ appState }); - + renderIndexationContextProvider({ appState }); expect(IndexationNotificationHelper.startPolling).not.toHaveBeenCalled(); - - const expectedStatus: IndexationStatus = { - hasFailures: false, - isCompleted: true, - }; - - expect(wrapper.state().status).toEqual(expectedStatus); }); it('should update the state on new status', () => { - const wrapper = mountRender(); + renderIndexationContextProvider(); const triggerNewStatus = jest.mocked(IndexationNotificationHelper.startPolling).mock .calls[0][0] as (status: IndexationStatus) => void; @@ -64,21 +59,15 @@ it('should update the state on new status', () => { isCompleted: true, }; - triggerNewStatus(newStatus); - - expect(wrapper.state().status).toEqual(newStatus); -}); + expect(byText('null').get()).toBeInTheDocument(); -it('should stop polling when component is destroyed', () => { - const wrapper = mountRender(); - - wrapper.unmount(); + triggerNewStatus(newStatus); - expect(IndexationNotificationHelper.stopPolling).toHaveBeenCalled(); + expect(byText('{"status":{"hasFailures":false,"isCompleted":true}}').get()).toBeInTheDocument(); }); -function mountRender(props?: IndexationContextProviderProps) { - return mount( +function renderIndexationContextProvider(props?: IndexationContextProviderProps) { + return renderComponent( @@ -86,5 +75,6 @@ function mountRender(props?: IndexationContextProviderProps) { } function TestComponent() { - return

TestComponent

; + const state = useContext(IndexationContext); + return
{JSON.stringify(state)}
; } diff --git a/server/sonar-web/src/main/js/app/components/indexation/__tests__/IndexationNotification-test.tsx b/server/sonar-web/src/main/js/app/components/indexation/__tests__/IndexationNotification-test.tsx index 2ca99d7af36..da8f78d54f1 100644 --- a/server/sonar-web/src/main/js/app/components/indexation/__tests__/IndexationNotification-test.tsx +++ b/server/sonar-web/src/main/js/app/components/indexation/__tests__/IndexationNotification-test.tsx @@ -17,11 +17,11 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ - -import { shallow } from 'enzyme'; import * as React from 'react'; -import { mockCurrentUser } from '../../../../helpers/testMocks'; -import { IndexationNotificationType } from '../../../../types/indexation'; +import { mockCurrentUser, mockLoggedInUser } from '../../../../helpers/testMocks'; +import { renderComponent } from '../../../../helpers/testReactTestingUtils'; +import { byText } from '../../../../helpers/testSelector'; +import { Permissions } from '../../../../types/permissions'; import { IndexationNotification } from '../IndexationNotification'; import IndexationNotificationHelper from '../IndexationNotificationHelper'; @@ -30,21 +30,23 @@ beforeEach(() => jest.clearAllMocks()); jest.mock('../IndexationNotificationHelper'); describe('Completed banner', () => { - it('should be displayed', () => { + it('should be displayed and call helper when updated', () => { jest .mocked(IndexationNotificationHelper.shouldDisplayCompletedNotification) .mockReturnValueOnce(true); - const wrapper = shallowRender(); + const { rerender } = renderIndexationNotification(); - wrapper.setProps({ - indexationContext: { - status: { hasFailures: false, isCompleted: true }, - }, - }); + rerender( + + ); expect(IndexationNotificationHelper.shouldDisplayCompletedNotification).toHaveBeenCalled(); - expect(wrapper.state().notificationType).toBe(IndexationNotificationType.Completed); }); it('should be displayed at startup', () => { @@ -52,14 +54,13 @@ describe('Completed banner', () => { .mocked(IndexationNotificationHelper.shouldDisplayCompletedNotification) .mockReturnValueOnce(true); - const wrapper = shallowRender({ + renderIndexationNotification({ indexationContext: { status: { hasFailures: false, isCompleted: true }, }, }); expect(IndexationNotificationHelper.shouldDisplayCompletedNotification).toHaveBeenCalled(); - expect(wrapper.state().notificationType).toBe(IndexationNotificationType.Completed); }); it('should be hidden once completed without failure', () => { @@ -69,59 +70,88 @@ describe('Completed banner', () => { .mocked(IndexationNotificationHelper.shouldDisplayCompletedNotification) .mockReturnValueOnce(true); - const wrapper = shallowRender({ + renderIndexationNotification({ indexationContext: { status: { hasFailures: false, isCompleted: true }, }, }); - expect(wrapper.state().notificationType).toBe(IndexationNotificationType.Completed); expect(IndexationNotificationHelper.markCompletedNotificationAsDisplayed).toHaveBeenCalled(); jest.runAllTimers(); - expect(wrapper.state().notificationType).toBeUndefined(); + expect(IndexationNotificationHelper.markCompletedNotificationAsDisplayed).toHaveBeenCalled(); jest.useRealTimers(); }); -}); -it('should display the completed-with-failure banner', () => { - const wrapper = shallowRender({ - indexationContext: { - status: { hasFailures: true, isCompleted: true }, - }, + it('should start progress > progress with failure > complete with failure', () => { + const { rerender } = renderIndexationNotification({ + indexationContext: { + status: { completedCount: 23, hasFailures: false, isCompleted: false, total: 42 }, + }, + }); + + expect(byText('indexation.progression.23.42').get()).toBeInTheDocument(); + + rerender( + + ); + + expect(byText('indexation.progression_with_error').get()).toBeInTheDocument(); + + rerender( + + ); + expect(byText('indexation.completed_with_error').get()).toBeInTheDocument(); }); - expect(wrapper.state().notificationType).toBe(IndexationNotificationType.CompletedWithFailure); -}); + it('should start progress > success > disappear', () => { + const { rerender } = renderIndexationNotification({ + indexationContext: { + status: { completedCount: 23, hasFailures: false, isCompleted: false, total: 42 }, + }, + }); + + expect(byText('indexation.progression.23.42').get()).toBeInTheDocument(); -it('should display the progress banner', () => { - const wrapper = shallowRender({ - indexationContext: { - status: { completedCount: 23, hasFailures: false, isCompleted: false, total: 42 }, - }, + rerender( + + ); + expect(IndexationNotificationHelper.shouldDisplayCompletedNotification).toHaveBeenCalled(); }); - expect(IndexationNotificationHelper.markCompletedNotificationAsToDisplay).toHaveBeenCalled(); - expect(wrapper.state().notificationType).toBe(IndexationNotificationType.InProgress); -}); + it('should not see notification if not admin', () => { + renderIndexationNotification({ + indexationContext: { + status: { completedCount: 23, hasFailures: false, isCompleted: false, total: 42 }, + }, + currentUser: mockLoggedInUser(), + }); -it('should display the progress-with-failure banner', () => { - const wrapper = shallowRender({ - indexationContext: { - status: { completedCount: 23, hasFailures: true, isCompleted: false, total: 42 }, - }, + expect(byText('indexation.progression.23.42').query()).not.toBeInTheDocument(); }); - - expect(IndexationNotificationHelper.markCompletedNotificationAsToDisplay).toHaveBeenCalled(); - expect(wrapper.state().notificationType).toBe(IndexationNotificationType.InProgressWithFailure); }); -function shallowRender(props?: Partial) { - return shallow( +function renderIndexationNotification(props?: Partial) { + return renderComponent( { - const wrapper = shallowRender(); + renderPageUnavailableToIndexation(); - expect(wrapper).toMatchSnapshot(); + expect(byRole('link', { name: 'learn_more' }).get()).toBeInTheDocument(); }); it('should not refresh the page once the indexation is complete if there were failures', () => { @@ -36,17 +37,17 @@ it('should not refresh the page once the indexation is complete if there were fa value: { reload }, }); - const wrapper = shallowRender(); + const { rerender } = renderPageUnavailableToIndexation(); expect(reload).not.toHaveBeenCalled(); - wrapper.setProps({ - indexationContext: { - status: { hasFailures: true, isCompleted: true }, - }, - }); - - wrapper.update(); + rerender( + + ); expect(reload).not.toHaveBeenCalled(); }); @@ -59,23 +60,23 @@ it('should refresh the page once the indexation is complete if there were NO fai value: { reload }, }); - const wrapper = shallowRender(); + const { rerender } = renderPageUnavailableToIndexation(); expect(reload).not.toHaveBeenCalled(); - wrapper.setProps({ - indexationContext: { - status: { hasFailures: false, isCompleted: true }, - }, - }); - - wrapper.update(); + rerender( + + ); expect(reload).toHaveBeenCalled(); }); -function shallowRender() { - return shallow( +function renderPageUnavailableToIndexation() { + return renderComponent( - -

- TestComponent -

-
-
-`; diff --git a/server/sonar-web/src/main/js/app/components/indexation/__tests__/__snapshots__/PageUnavailableDueToIndexation-test.tsx.snap b/server/sonar-web/src/main/js/app/components/indexation/__tests__/__snapshots__/PageUnavailableDueToIndexation-test.tsx.snap deleted file mode 100644 index 2d3cf8c163a..00000000000 --- a/server/sonar-web/src/main/js/app/components/indexation/__tests__/__snapshots__/PageUnavailableDueToIndexation-test.tsx.snap +++ /dev/null @@ -1,35 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`should render correctly 1`] = ` -
- - - indexation.page_unavailable.description - - - learn_more - , - } - } - /> - - - -
-`; -- 2.39.5