mirror of
https://github.com/IT4Change/Ocelot-Social.git
synced 2025-12-13 07:45:56 +00:00
Start writing component tests
- for some reason the error toaster is not being called even though the .catch is being called
This commit is contained in:
parent
c3cfe90aa2
commit
7e1f168ba3
92
webapp/components/CommentForm/CommentForm.test.js
Normal file
92
webapp/components/CommentForm/CommentForm.test.js
Normal file
@ -0,0 +1,92 @@
|
||||
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'] = '<span><slot /></span>'
|
||||
|
||||
describe('CommentForm.vue', () => {
|
||||
let mocks
|
||||
let wrapper
|
||||
let form
|
||||
let propsData
|
||||
let cancelBtn
|
||||
let spy
|
||||
|
||||
beforeEach(() => {
|
||||
mocks = {
|
||||
$t: jest.fn(),
|
||||
$apollo: {
|
||||
mutate: jest
|
||||
.fn()
|
||||
.mockResolvedValueOnce({ data: { CreateComment: { contentExcerpt: 'this is a comment' } } })
|
||||
.mockRejectedValue({ message: 'Ouch!' })
|
||||
},
|
||||
$toast: {
|
||||
error: jest.fn(),
|
||||
success: jest.fn()
|
||||
}
|
||||
},
|
||||
propsData = {
|
||||
post: { id: 1 }
|
||||
}
|
||||
})
|
||||
|
||||
describe('mount', () => {
|
||||
const Wrapper = () => {
|
||||
return mount(CommentForm, { mocks, localVue, propsData })
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
wrapper = Wrapper()
|
||||
spy = jest.spyOn(wrapper.vm, 'clear')
|
||||
})
|
||||
|
||||
it('calls the apollo mutation when form is submitted', () => {
|
||||
wrapper.vm.updateEditorContent('this is a comment')
|
||||
form = wrapper.find('form')
|
||||
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(spy).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
describe('mutation resolves', () => {
|
||||
beforeEach(async () => {
|
||||
wrapper.vm.updateEditorContent('this is a comment')
|
||||
form = wrapper.find('form')
|
||||
form.trigger('submit')
|
||||
})
|
||||
|
||||
it('shows a success toaster', async () => {
|
||||
await mocks.$apollo.mutate
|
||||
expect(mocks.$toast.success).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('clears the editor', () => {
|
||||
expect(spy).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('emits a method call with the returned comment', () => {
|
||||
expect(wrapper.emitted().addComment[0]).toEqual([{ contentExcerpt: 'this is a comment' }])
|
||||
})
|
||||
|
||||
describe('mutation fails', () => {
|
||||
it('shows the error toaster', async () => {
|
||||
wrapper.find('form').trigger('submit')
|
||||
await mocks.$apollo.mutate
|
||||
expect(mocks.$toast.error).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -18,6 +18,7 @@
|
||||
<ds-flex-item :width="{ base: '0%', md: '50%', sm: '0%', xs: '0%' }" />
|
||||
<ds-flex-item :width="{ base: '40%', md: '20%', sm: '30%', xs: '30%' }">
|
||||
<ds-button
|
||||
class="cancelBtn"
|
||||
:disabled="disabled"
|
||||
ghost
|
||||
@click.prevent="clear"
|
||||
@ -92,7 +93,7 @@ export default {
|
||||
})
|
||||
.then(res => {
|
||||
this.$emit('addComment', res.data.CreateComment)
|
||||
this.$refs.editor.clear()
|
||||
this.clear()
|
||||
this.$toast.success(this.$t('post.comment.submitted'))
|
||||
})
|
||||
.catch(err => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user