Wolfgang Huß 379f7d1ed7 Merge branch 'master' of https://github.com/Human-Connection/Human-Connection into 1455-fix-update-comment-list
# Conflicts:
#	webapp/components/Comment.vue
#	webapp/components/CommentForm/CommentForm.spec.js
#	webapp/components/CommentForm/CommentForm.vue
#	webapp/components/EditCommentForm/EditCommentForm.vue
#	webapp/store/editor.js
2019-09-11 07:58:40 +02:00

105 lines
2.5 KiB
JavaScript

import { mount, createLocalVue } from '@vue/test-utils'
import CommentForm from './CommentForm'
import Styleguide from '@human-connection/styleguide'
import MutationObserver from 'mutation-observer'
global.MutationObserver = MutationObserver
const localVue = createLocalVue()
localVue.use(Styleguide)
describe('CommentForm.vue', () => {
let mocks
let wrapper
let propsData
let cancelBtn
let cancelMethodSpy
beforeEach(() => {
mocks = {
$t: jest.fn(),
$i18n: {
locale: () => 'en',
},
$apollo: {
mutate: jest
.fn()
.mockResolvedValueOnce({
data: {
CreateComment: {
contentExcerpt: 'this is a comment',
},
},
})
.mockRejectedValue({
message: 'Ouch!',
}),
},
$toast: {
error: jest.fn(),
success: jest.fn(),
},
$filters: {
removeHtml: a => a,
},
}
propsData = {
post: {
id: 1,
},
}
})
describe('mount', () => {
const Wrapper = () => {
return mount(CommentForm, {
mocks,
localVue,
propsData,
})
}
beforeEach(() => {
wrapper = Wrapper()
cancelMethodSpy = jest.spyOn(wrapper.vm, 'clear')
})
it('calls the apollo mutation when form is submitted', async () => {
wrapper.vm.updateEditorContent('this is a comment')
await wrapper.find('form').trigger('submit')
expect(mocks.$apollo.mutate).toHaveBeenCalledTimes(1)
})
it('calls clear method when the cancel button is clicked', () => {
wrapper.vm.updateEditorContent('ok')
cancelBtn = wrapper.find('.cancelBtn')
cancelBtn.trigger('click')
expect(cancelMethodSpy).toHaveBeenCalledTimes(1)
})
describe('mutation resolves', () => {
beforeEach(async () => {
wrapper.vm.updateEditorContent('this is a comment')
wrapper.find('form').trigger('submit')
})
it('shows a success toaster', async () => {
await mocks.$apollo.mutate
expect(mocks.$toast.success).toHaveBeenCalledTimes(1)
})
it('clears the editor', () => {
expect(cancelMethodSpy).toHaveBeenCalledTimes(1)
})
describe('mutation fails', () => {
it('shows the error toaster', async () => {
await wrapper.find('form').trigger('submit')
await mocks.$apollo.mutate
expect(mocks.$toast.error).toHaveBeenCalledTimes(1)
})
})
})
})
})