* 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,
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;
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<IndexationContextProvider>(
+function renderIndexationContextProvider(props?: IndexationContextProviderProps) {
+ return renderComponent(
<IndexationContextProvider appState={mockAppState({ needIssueSync: true, ...props?.appState })}>
<TestComponent />
</IndexationContextProvider>
}
function TestComponent() {
- return <h1>TestComponent</h1>;
+ const state = useContext(IndexationContext);
+ return <div>{JSON.stringify(state)}</div>;
}
* 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';
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(
+ <IndexationNotification
+ currentUser={mockCurrentUser()}
+ indexationContext={{
+ status: { completedCount: 23, hasFailures: false, isCompleted: true, total: 42 },
+ }}
+ />
+ );
expect(IndexationNotificationHelper.shouldDisplayCompletedNotification).toHaveBeenCalled();
- expect(wrapper.state().notificationType).toBe(IndexationNotificationType.Completed);
});
it('should be displayed at startup', () => {
.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', () => {
.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(
+ <IndexationNotification
+ currentUser={mockLoggedInUser({ permissions: { global: [Permissions.Admin] } })}
+ indexationContext={{
+ status: { completedCount: 23, hasFailures: true, isCompleted: false, total: 42 },
+ }}
+ />
+ );
+
+ expect(byText('indexation.progression_with_error').get()).toBeInTheDocument();
+
+ rerender(
+ <IndexationNotification
+ currentUser={mockLoggedInUser({ permissions: { global: [Permissions.Admin] } })}
+ indexationContext={{
+ status: { completedCount: 23, hasFailures: true, isCompleted: true, total: 42 },
+ }}
+ />
+ );
+ 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(
+ <IndexationNotification
+ currentUser={mockLoggedInUser({ permissions: { global: [Permissions.Admin] } })}
+ indexationContext={{
+ status: { completedCount: 23, hasFailures: false, isCompleted: true, total: 42 },
+ }}
+ />
+ );
+ 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<IndexationNotification['props']>) {
- return shallow<IndexationNotification>(
+function renderIndexationNotification(props?: Partial<IndexationNotification['props']>) {
+ return renderComponent(
<IndexationNotification
- currentUser={mockCurrentUser()}
+ currentUser={mockLoggedInUser({ permissions: { global: [Permissions.Admin] } })}
indexationContext={{
status: { completedCount: 23, hasFailures: false, isCompleted: false, total: 42 },
}}
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
-import { shallow } from 'enzyme';
import * as React from 'react';
+import { renderComponent } from '../../../../helpers/testReactTestingUtils';
+import { byRole } from '../../../../helpers/testSelector';
import { PageUnavailableDueToIndexation } from '../PageUnavailableDueToIndexation';
it('should render correctly', () => {
- 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', () => {
value: { reload },
});
- const wrapper = shallowRender();
+ const { rerender } = renderPageUnavailableToIndexation();
expect(reload).not.toHaveBeenCalled();
- wrapper.setProps({
- indexationContext: {
- status: { hasFailures: true, isCompleted: true },
- },
- });
-
- wrapper.update();
+ rerender(
+ <PageUnavailableDueToIndexation
+ indexationContext={{
+ status: { hasFailures: true, isCompleted: true },
+ }}
+ />
+ );
expect(reload).not.toHaveBeenCalled();
});
value: { reload },
});
- const wrapper = shallowRender();
+ const { rerender } = renderPageUnavailableToIndexation();
expect(reload).not.toHaveBeenCalled();
- wrapper.setProps({
- indexationContext: {
- status: { hasFailures: false, isCompleted: true },
- },
- });
-
- wrapper.update();
+ rerender(
+ <PageUnavailableDueToIndexation
+ indexationContext={{
+ status: { hasFailures: false, isCompleted: true },
+ }}
+ />
+ );
expect(reload).toHaveBeenCalled();
});
-function shallowRender() {
- return shallow<PageUnavailableDueToIndexation>(
+function renderPageUnavailableToIndexation() {
+ return renderComponent(
<PageUnavailableDueToIndexation
indexationContext={{
status: { completedCount: 23, hasFailures: false, isCompleted: false, total: 42 },