From 97934e23f9c5a04e05207aaf457146ce979b5567 Mon Sep 17 00:00:00 2001 From: Pavel Mineev Date: Wed, 26 May 2021 15:38:23 +0300 Subject: [PATCH] fix tests --- .../app/components/comment/comment.test.tsx | 155 ++++++++---------- 1 file changed, 64 insertions(+), 91 deletions(-) diff --git a/frontend/app/components/comment/comment.test.tsx b/frontend/app/components/comment/comment.test.tsx index 28883461..d0a29b43 100644 --- a/frontend/app/components/comment/comment.test.tsx +++ b/frontend/app/components/comment/comment.test.tsx @@ -1,6 +1,6 @@ import { h } from 'preact'; -import { mount as enzymeMount } from 'enzyme'; -import { IntlProvider, IntlShape } from 'react-intl'; +import { mount } from 'enzyme'; +import { useIntl, IntlProvider } from 'react-intl'; import enMessages from 'locales/en.json'; import type { User, Comment as CommentType, PostInfo } from 'common/types'; @@ -9,18 +9,19 @@ import { sleep } from 'utils/sleep'; import { Comment, CommentProps } from './comment'; -const mount = (component: T) => - enzymeMount( - - {component} - - ); +function mountComment(props: CommentProps) { + function Wrapper(updateProps: Partial = {}) { + const intl = useIntl(); -const intl = { - formatMessage(message: { defaultMessage: string }) { - return message.defaultMessage || ''; - }, -} as IntlShape; + return ( + + + + ); + } + + return mount(); +} const DefaultProps: Partial = { CommentForm: null, @@ -46,14 +47,12 @@ const DefaultProps: Partial = { id: 'testuser', picture: 'somepicture-url', } as User, - intl, -}; +} as CommentProps; describe('', () => { describe('voting', () => { it('should be disabled for an anonymous user', () => { - const props = { ...DefaultProps, user: { id: 'anonymous_1' } } as CommentProps; - const wrapper = mount(); + const wrapper = mountComment({ ...DefaultProps, user: { id: 'anonymous_1' } } as CommentProps); const voteButtons = wrapper.find('.comment__vote'); expect(voteButtons.length).toEqual(2); @@ -67,8 +66,7 @@ describe('', () => { it('should be enabled for an anonymous user when it was allowed from server', () => { StaticStore.config.anon_vote = true; - const props = { ...DefaultProps, user: { id: 'anonymous_1' } } as CommentProps; - const wrapper = mount(); + const wrapper = mountComment({ ...DefaultProps, user: { id: 'anonymous_1' } } as CommentProps); const voteButtons = wrapper.find('.comment__vote'); expect(voteButtons.length).toEqual(2); @@ -79,7 +77,7 @@ describe('', () => { }); it('disabled on user info widget', () => { - const element = mount(); + const element = mountComment({ ...DefaultProps, view: 'user' } as CommentProps); const voteButtons = element.find('.comment__vote'); expect(voteButtons.length).toStrictEqual(2); @@ -91,11 +89,10 @@ describe('', () => { }); it('disabled on read only post', () => { - const element = mount( - - ); + const element = mountComment({ + ...DefaultProps, + post_info: { ...DefaultProps.post_info, read_only: true }, + } as CommentProps); const voteButtons = element.find('.comment__vote'); expect(voteButtons.length).toStrictEqual(2); @@ -107,10 +104,7 @@ describe('', () => { }); it('disabled for deleted comment', () => { - const element = mount( - // ahem - - ); + const element = mountComment({ ...DefaultProps, data: { ...DefaultProps.data, delete: true } } as CommentProps); const voteButtons = element.find('.comment__vote'); expect(voteButtons.length).toStrictEqual(2); @@ -122,17 +116,13 @@ describe('', () => { }); it('disabled for guest', () => { - const element = mount( - - ); + const element = mountComment({ + ...DefaultProps, + user: { + id: 'someone', + picture: 'somepicture-url', + }, + } as CommentProps); const voteButtons = element.find('.comment__vote'); expect(voteButtons.length).toStrictEqual(2); @@ -144,7 +134,7 @@ describe('', () => { }); it('disabled for own comment', () => { - const element = mount(); + const element = mountComment({ ...DefaultProps, user: null } as CommentProps); const voteButtons = element.find('.comment__vote'); expect(voteButtons.length).toStrictEqual(2); @@ -157,13 +147,11 @@ describe('', () => { it('disabled for already upvoted comment', async () => { const voteSpy = jest.fn(async () => undefined); - const element = mount( - - ); + const element = mountComment({ + ...DefaultProps, + data: { ...DefaultProps.data, vote: +1 } as CommentProps['data'], + putCommentVote: voteSpy, + } as CommentProps); const voteButtons = element.find('.comment__vote'); expect(voteButtons.length).toStrictEqual(2); @@ -181,13 +169,14 @@ describe('', () => { it('disabled for already downvoted comment', async () => { const voteSpy = jest.fn(async () => undefined); - const element = mount( - - ); + const element = mountComment({ + ...DefaultProps, + data: { + ...DefaultProps.data, + vote: -1, + }, + putCommentVote: voteSpy, + } as CommentProps); const voteButtons = element.find('.comment__vote'); expect(voteButtons.length).toStrictEqual(2); @@ -206,9 +195,7 @@ describe('', () => { describe('admin controls', () => { it('for admin if shows admin controls', () => { - const element = mount( - - ); + const element = mountComment({ ...DefaultProps, user: { ...DefaultProps.user, admin: true } } as CommentProps); const controls = element.find('.comment__controls').children(); @@ -221,9 +208,7 @@ describe('', () => { }); it('for regular user it shows only "hide"', () => { - const element = mount( - - ); + const element = mountComment({ ...DefaultProps, user: { ...DefaultProps.user, admin: false } } as CommentProps); const controls = element.find('.comment__controls').children(); expect(controls.length).toBe(1); @@ -231,32 +216,28 @@ describe('', () => { }); it('verification badge clickable for admin', () => { - const element = mount( - - ); + const element = mountComment({ ...DefaultProps, user: { ...DefaultProps.user, admin: true } } as CommentProps); const controls = element.find('.comment__verification').first(); expect(controls.hasClass('comment__verification_clickable')).toEqual(true); }); it('verification badge not clickable for regular user', () => { - const element = mount( - - ); + const element = mountComment({ + ...DefaultProps, + data: { ...DefaultProps.data, user: { ...DefaultProps.data!.user, verified: true } }, + } as CommentProps); const controls = element.find('.comment__verification').first(); expect(controls.hasClass('comment__verification_clickable')).toEqual(false); }); it('should be editable', () => { + StaticStore.config.edit_duration = 300; + const initTime = new Date().toString(); const changedTime = new Date(Date.now() + 10 * 1000).toString(); - const props: Partial = { + const props = { ...DefaultProps, user: DefaultProps.user as User, data: { @@ -269,30 +250,25 @@ describe('', () => { orig: 'test', } as CommentType, repliesCount: 0, - }; - StaticStore.config.edit_duration = 300; - const WrappedComponent = (props: CommentProps) => ( - - - - ); - const component = enzymeMount(); + } as CommentProps; + const component = mountComment(props); + const comment = component.find(Comment); - expect((component.find(`Comment`).state('editDeadline') as Date).getTime()).toBe( + expect((comment.state('editDeadline') as Date).getTime()).toBe( new Date(new Date(initTime).getTime() + 300 * 1000).getTime() ); - component.setProps({ - data: { ...props.data, time: changedTime }, - }); + component.setProps({ data: { ...props.data, time: changedTime } }); - expect((component.find(`Comment`).state('editDeadline') as Date).getTime()).toBe( + expect((comment.state('editDeadline') as Date).getTime()).toBe( new Date(new Date(changedTime).getTime() + 300 * 1000).getTime() ); }); it('shoud not be editable', () => { - const props: Partial = { + StaticStore.config.edit_duration = 300; + + const component = mountComment({ ...DefaultProps, user: DefaultProps.user as User, data: { @@ -303,10 +279,7 @@ describe('', () => { time: new Date(new Date().getDate() - 300).toString(), orig: 'test', } as CommentType, - }; - StaticStore.config.edit_duration = 300; - - const component = mount(); + } as CommentProps); expect(component.find('Comment').state('editDeadline')).toBe(null); });