scratch-www/test/unit/components/studio-comments.test.jsx

172 lines
6.5 KiB
React
Raw Permalink Normal View History

2021-04-22 10:59:57 -04:00
import React from 'react';
import {mountWithIntl} from '../../helpers/intl-helpers.jsx';
2021-04-22 10:59:57 -04:00
import {StudioComments} from '../../../src/views/studio/studio-comments.jsx';
// Replace customized studio comment with default comment to avoid redux issues in the test
jest.mock('../../../src/views/studio/studio-comment.js', () => (
jest.requireActual('../../../src/views/preview/comment/comment.jsx')
));
2021-04-22 10:59:57 -04:00
describe('Studio comments', () => {
2023-01-06 15:55:16 -05:00
const testComments = [{id: 123, author: {}, datetime_created: new Date().toISOString()}];
2021-04-22 10:59:57 -04:00
test('if there are no comments, they get loaded', () => {
const loadComments = jest.fn();
const component = mountWithIntl(
<StudioComments
2021-05-17 15:31:08 -04:00
hasFetchedSession={false}
2021-04-22 10:59:57 -04:00
comments={[]}
handleLoadMoreComments={loadComments}
/>
);
2021-05-17 15:31:08 -04:00
expect(loadComments).not.toHaveBeenCalled();
component.setProps({hasFetchedSession: true});
component.update();
2021-04-22 10:59:57 -04:00
expect(loadComments).toHaveBeenCalled();
// When updated to have comments, load is not called again
loadComments.mockClear();
2023-01-06 15:55:16 -05:00
component.setProps({comments: testComments});
2021-04-22 10:59:57 -04:00
component.update();
expect(loadComments).not.toHaveBeenCalled();
// When reset to have no comments again, load is called again
loadComments.mockClear();
component.setProps({comments: []});
component.update();
expect(loadComments).toHaveBeenCalled();
});
test('becoming an admin resets the comments', () => {
const resetComments = jest.fn();
const component = mountWithIntl(
<StudioComments
2021-05-17 15:31:08 -04:00
hasFetchedSession
2021-04-22 10:59:57 -04:00
isAdmin={false}
2023-01-06 15:55:16 -05:00
comments={testComments}
2021-04-22 10:59:57 -04:00
handleResetComments={resetComments}
/>
);
expect(resetComments).not.toHaveBeenCalled();
// When updated to isAdmin=true, reset is called
resetComments.mockClear();
component.setProps({isAdmin: true});
component.update();
expect(resetComments).toHaveBeenCalled();
// If updated back to isAdmin=false, reset is also called
// not currently possible in the UI, but if it was, we'd want to clear comments
resetComments.mockClear();
component.setProps({isAdmin: false});
component.update();
expect(resetComments).toHaveBeenCalled();
});
test('being an admin on initial render doesnt reset comments', () => {
// This ensures that comments don't get reloaded when changing tabs
const resetComments = jest.fn();
mountWithIntl(
<StudioComments
isAdmin
2021-05-17 15:31:08 -04:00
hasFetchedSession
2023-01-06 15:55:16 -05:00
comments={testComments}
2021-04-22 10:59:57 -04:00
handleResetComments={resetComments}
/>
);
expect(resetComments).not.toHaveBeenCalled();
});
2021-06-10 16:51:41 -04:00
test('Comments do not show when shouldShowCommentsList is false', () => {
2021-06-10 16:51:41 -04:00
const component = mountWithIntl(
<StudioComments
hasFetchedSession
isAdmin={false}
2023-01-06 15:55:16 -05:00
comments={testComments}
shouldShowCommentsList={false}
2021-06-10 16:51:41 -04:00
/>
);
expect(component.find('div.studio-compose-container').exists()).toBe(true);
expect(component.find('TopLevelComment').exists()).toBe(false);
});
test('Comments show when shouldShowCommentsList is true', () => {
2021-06-10 16:51:41 -04:00
const component = mountWithIntl(
<StudioComments
hasFetchedSession
isAdmin={false}
2023-01-06 15:55:16 -05:00
comments={testComments}
shouldShowCommentsList
2021-06-10 16:51:41 -04:00
/>
);
expect(component.find('div.studio-compose-container').exists()).toBe(true);
expect(component.find('TopLevelComment').exists()).toBe(true);
});
2021-06-17 16:27:50 -04:00
test('Single comment load more shows when shouldShowCommentsList is true', () => {
// Make the component think this is a single view.
global.window.location.hash = '#comments-6';
const component = mountWithIntl(
<StudioComments
hasFetchedSession
isAdmin={false}
2023-01-06 15:55:16 -05:00
comments={testComments}
2021-06-17 16:27:50 -04:00
shouldShowCommentsList
singleCommentId
/>
);
expect(component.find('div.studio-compose-container').exists()).toBe(true);
expect(component.find('TopLevelComment').exists()).toBe(true);
expect(component.find('Button').exists()).toBe(true);
expect(component.find('button.load-more-button').exists()).toBe(true);
global.window.location.hash = '';
});
test('Single comment does not show when shouldShowCommentsList is false', () => {
// Make the component think this is a single view.
global.window.location.hash = '#comments-6';
const component = mountWithIntl(
<StudioComments
hasFetchedSession
isAdmin={false}
2023-01-06 15:55:16 -05:00
comments={testComments}
2021-06-17 16:27:50 -04:00
shouldShowCommentsList={false}
singleCommentId
/>
);
expect(component.find('div.studio-compose-container').exists()).toBe(true);
expect(component.find('TopLevelComment').exists()).toBe(false);
expect(component.find('Button').exists()).toBe(false);
expect(component.find('button.load-more-button').exists()).toBe(false);
global.window.location.hash = '';
});
test('Comment status error shows when shoudlShowCommentsGloballyOffError is true', () => {
const component = mountWithIntl(
<StudioComments
hasFetchedSession={false}
isAdmin={false}
2023-01-06 15:55:16 -05:00
comments={testComments}
shouldShowCommentsGloballyOffError
/>
);
expect(component.find('div.studio-compose-container').exists()).toBe(true);
expect(component.find('CommentingStatus').exists()).toBe(true);
});
test('Comment status error does not show when shoudlShowCommentsGloballyOffError is false', () => {
2021-06-10 16:51:41 -04:00
const component = mountWithIntl(
<StudioComments
hasFetchedSession={false}
isAdmin={false}
2023-01-06 15:55:16 -05:00
comments={testComments}
shouldShowCommentsGloballyOffError={false}
2021-06-10 16:51:41 -04:00
/>
);
expect(component.find('div.studio-compose-container').exists()).toBe(true);
expect(component.find('CommentingStatus').exists()).toBe(false);
});
2021-04-22 10:59:57 -04:00
});