diff --git a/webapp/components/comments/CommentForm/CommentForm.test.js b/webapp/components/comments/CommentForm/CommentForm.test.js new file mode 100644 index 000000000..6453f689a --- /dev/null +++ b/webapp/components/comments/CommentForm/CommentForm.test.js @@ -0,0 +1,74 @@ +import { config, mount, createLocalVue } from '@vue/test-utils' +import CommentForm from './index.vue' +import Vue from 'vue' +import Styleguide from '@human-connection/styleguide' + +const localVue = createLocalVue() + +localVue.use(Styleguide) + +config.stubs['no-ssr'] = '' + +describe('CommentForm.vue', () => { + let mocks + let wrapper + let form + let propsData + let submitBtn + let cancelBtn + + beforeEach(() => { + mocks = { + $t: jest.fn(), + $apollo: { + mutate: jest + .fn() + .mockRejectedValue({ message: 'Ouch!' }) + .mockResolvedValueOnce({ data: { CreateComment: { contentExcerpt: 'this is a comment' } } }) + }, + $toast: { + error: jest.fn(), + success: jest.fn() + }, + $root: { + $emit: jest.fn() + } + }, + propsData = { + post: { id: 1 } + } + }) + + describe('mount', () => { + const Wrapper = () => { + return mount(CommentForm, { mocks, localVue, propsData }) + } + + beforeEach(() => { + wrapper = Wrapper() + }) + + it('calls the apollo mutation when form is submitted', () => { + wrapper.vm.updateEditorContent('ok') + form = wrapper.find('form') + form.trigger('submit') + expect(mocks.$apollo.mutate).toHaveBeenCalledTimes(1) + }) + + it("calls clear method when the cancel button is clicked", () => { + const spy = jest.spyOn(wrapper.vm, 'clear') + wrapper.vm.updateEditorContent('ok') + cancelBtn = wrapper.find('.cancelBtn') + cancelBtn.trigger('click') + expect(spy).toHaveBeenCalledTimes(1) + }) + + it('shows a success toaster if the mutation resolves', () => { + wrapper.vm.updateEditorContent('ok') + form = wrapper.find('form') + form.trigger('submit') + expect(mocks.$root.$emit).toHaveBeenCalledTimes(1) + expect(mocks.$toast.success).toHaveBeenCalledTimes(1) + }) + }) +}) \ No newline at end of file diff --git a/webapp/components/CommentForm/index.vue b/webapp/components/comments/CommentForm/index.vue similarity index 96% rename from webapp/components/CommentForm/index.vue rename to webapp/components/comments/CommentForm/index.vue index 6019b0965..4377e2e2c 100644 --- a/webapp/components/CommentForm/index.vue +++ b/webapp/components/comments/CommentForm/index.vue @@ -21,6 +21,7 @@ :disabled="disabled" ghost @click.prevent="clear" + class="cancelBtn" > {{ $t('actions.cancel') }} @@ -91,6 +92,7 @@ export default { } }) .then(res => { + // console.log(this.$toast.success.mockResolvedValue()) this.$root.$emit('addComment', res.data.CreateComment) this.$refs.editor.clear() this.$toast.success(this.$t('post.comment.submitted')) diff --git a/webapp/components/CommentList/index.vue b/webapp/components/comments/CommentList/index.vue similarity index 100% rename from webapp/components/CommentList/index.vue rename to webapp/components/comments/CommentList/index.vue diff --git a/webapp/graphql/PostCommentsQuery.js b/webapp/graphql/PostCommentsQuery.js index 2c37f2933..c0ab1320a 100644 --- a/webapp/graphql/PostCommentsQuery.js +++ b/webapp/graphql/PostCommentsQuery.js @@ -5,7 +5,6 @@ export default app => { return gql(` query Post($slug: String!) { Post(slug: $slug) { - commentsCount comments(orderBy: createdAt_asc) { id contentExcerpt diff --git a/webapp/pages/post/_id/_slug/index.vue b/webapp/pages/post/_id/_slug/index.vue index 692dbe69e..87538eb6d 100644 --- a/webapp/pages/post/_id/_slug/index.vue +++ b/webapp/pages/post/_id/_slug/index.vue @@ -112,8 +112,8 @@ import HcTag from '~/components/Tag' import ContentMenu from '~/components/ContentMenu' import HcUser from '~/components/User' import HcShoutButton from '~/components/ShoutButton.vue' -import HcCommentForm from '~/components/CommentForm' -import HcCommentList from '~/components/CommentList' +import HcCommentForm from '~/components/comments/CommentForm' +import HcCommentList from '~/components/comments/CommentList' export default { transition: {