asyncData test post/edit/_id.spec.js

This commit is contained in:
Ulf Gebhardt 2021-04-26 01:52:04 +02:00
parent 002c9f5786
commit a71e864049
No known key found for this signature in database
GPG Key ID: 81308EFE29ABFEBD

View File

@ -1,3 +1,4 @@
import Vuex from 'vuex'
import { mount } from '@vue/test-utils'
import _id from './_id.vue'
@ -6,24 +7,78 @@ const localVue = global.localVue
describe('post/_id.vue', () => {
let wrapper
let mocks
let store
let asyncData
let error
let userId
let authorId
beforeEach(() => {
mocks = {
$t: jest.fn(),
}
asyncData = false
error = jest.fn()
})
describe('mount', () => {
const Wrapper = () => {
return mount(_id, { mocks, localVue })
const Wrapper = async () => {
mocks = {
$t: jest.fn(),
$i18n: {
locale: () => 'en',
},
apolloProvider: {
defaultClient: {
query: jest.fn().mockResolvedValue({
data: {
Post: [
{ author: { id: authorId } }
]
}
}),
},
},
}
store = new Vuex.Store({
getters: {
'auth/user': () => {
return { id: userId }
},
},
})
if (asyncData) {
const data = _id.data ? _id.data() : {}
const aData = await _id.asyncData({
app: mocks,
store,
error,
params: { id: '123' },
})
_id.data = function() {
return { ...data, ...aData};
};
}
return mount(_id, { store, mocks, localVue })
}
beforeEach(() => {
wrapper = Wrapper()
it('renders', async () => {
asyncData = false
wrapper = await Wrapper()
expect(wrapper.findAll('.contribution-form')).toHaveLength(1)
})
it('renders', () => {
expect(wrapper.findAll('.contribution-form')).toHaveLength(1)
it('renders with asyncData of different users', async () => {
asyncData = true
authorId = 'some-author'
userId = 'some-user'
wrapper = await Wrapper()
expect(error).toBeCalledWith({"message": "error-pages.cannot-edit-post", "statusCode": 403})
})
it('renders with asyncData of same user', async () => {
asyncData = true
authorId = 'some-author'
userId = 'some-author'
wrapper = await Wrapper()
expect(error).not.toHaveBeenCalled()
})
})
})