mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2025-12-12 23:35:58 +00:00
by setting up localVue with all required plugins (such as styleguide and vuex) in a separate testSetup file we can avoid doing this individually in all component tests the testSetup is executed before each test suite, so each test file gets a fresh instance of localVue
194 lines
5.4 KiB
JavaScript
194 lines
5.4 KiB
JavaScript
import { mount } from '@vue/test-utils'
|
|
import flushPromises from 'flush-promises'
|
|
import MySocialMedia from './my-social-media.vue'
|
|
import Vuex from 'vuex'
|
|
|
|
const localVue = global.localVue
|
|
|
|
describe('my-social-media.vue', () => {
|
|
let wrapper
|
|
let mocks
|
|
let getters
|
|
const socialMediaUrl = 'https://freeradical.zone/@mattwr18'
|
|
const newSocialMediaUrl = 'https://twitter.com/mattwr18'
|
|
const faviconUrl = 'https://freeradical.zone/favicon.ico'
|
|
|
|
beforeEach(() => {
|
|
mocks = {
|
|
$t: jest.fn(),
|
|
$apollo: {
|
|
mutate: jest.fn(),
|
|
},
|
|
$toast: {
|
|
error: jest.fn(),
|
|
success: jest.fn(),
|
|
},
|
|
}
|
|
getters = {
|
|
'auth/user': () => {
|
|
return {}
|
|
},
|
|
}
|
|
})
|
|
|
|
describe('mount', () => {
|
|
let form, input, submitButton
|
|
const Wrapper = () => {
|
|
const store = new Vuex.Store({
|
|
getters,
|
|
})
|
|
return mount(MySocialMedia, { store, mocks, localVue })
|
|
}
|
|
|
|
describe('adding social media link', () => {
|
|
beforeEach(() => {
|
|
wrapper = Wrapper()
|
|
form = wrapper.find('form')
|
|
input = wrapper.find('input#addSocialMedia')
|
|
submitButton = wrapper.find('button')
|
|
})
|
|
|
|
it('requires the link to be a valid url', () => {
|
|
input.setValue('some value')
|
|
form.trigger('submit')
|
|
|
|
expect(mocks.$apollo.mutate).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('displays an error message when not saved successfully', async () => {
|
|
mocks.$apollo.mutate.mockRejectedValue({ message: 'Ouch!' })
|
|
input.setValue(newSocialMediaUrl)
|
|
form.trigger('submit')
|
|
|
|
await flushPromises()
|
|
|
|
expect(mocks.$toast.error).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
describe('success', () => {
|
|
beforeEach(() => {
|
|
mocks.$apollo.mutate.mockResolvedValue({
|
|
data: { CreateSocialMedia: { id: 's2', url: newSocialMediaUrl } },
|
|
})
|
|
input.setValue(newSocialMediaUrl)
|
|
form.trigger('submit')
|
|
})
|
|
|
|
it('sends the new url to the backend', () => {
|
|
const expected = expect.objectContaining({
|
|
variables: { url: newSocialMediaUrl },
|
|
})
|
|
|
|
expect(mocks.$apollo.mutate).toHaveBeenCalledWith(expected)
|
|
})
|
|
|
|
it('displays a success message', async () => {
|
|
await flushPromises()
|
|
|
|
expect(mocks.$toast.success).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('clears the form', async () => {
|
|
await flushPromises()
|
|
|
|
expect(input.value).toBe(undefined)
|
|
expect(submitButton.vm.$attrs.disabled).toBe(true)
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('given existing social media links', () => {
|
|
beforeEach(() => {
|
|
getters = {
|
|
'auth/user': () => ({
|
|
socialMedia: [{ id: 's1', url: socialMediaUrl }],
|
|
}),
|
|
}
|
|
|
|
wrapper = Wrapper()
|
|
form = wrapper.find('form')
|
|
})
|
|
|
|
describe('for each link it', () => {
|
|
it('displays the favicon', () => {
|
|
expect(wrapper.find(`img[src="${faviconUrl}"]`).exists()).toBe(true)
|
|
})
|
|
|
|
it('displays the url', () => {
|
|
expect(wrapper.find(`a[href="${socialMediaUrl}"]`).exists()).toBe(true)
|
|
})
|
|
|
|
it('displays the edit button', () => {
|
|
expect(wrapper.find('a[name="edit"]').exists()).toBe(true)
|
|
})
|
|
|
|
it('displays the delete button', () => {
|
|
expect(wrapper.find('a[name="delete"]').exists()).toBe(true)
|
|
})
|
|
})
|
|
|
|
it('does not accept a duplicate url', () => {
|
|
input = wrapper.find('input#addSocialMedia')
|
|
|
|
input.setValue(socialMediaUrl)
|
|
form.trigger('submit')
|
|
|
|
expect(mocks.$apollo.mutate).not.toHaveBeenCalled()
|
|
})
|
|
|
|
describe('editing social media link', () => {
|
|
beforeEach(() => {
|
|
const editButton = wrapper.find('a[name="edit"]')
|
|
editButton.trigger('click')
|
|
input = wrapper.find('input#editSocialMedia')
|
|
})
|
|
|
|
it('disables adding new links while editing', () => {
|
|
const addInput = wrapper.find('input#addSocialMedia')
|
|
|
|
expect(addInput.exists()).toBe(false)
|
|
})
|
|
|
|
it('sends the new url to the backend', () => {
|
|
const expected = expect.objectContaining({
|
|
variables: { id: 's1', url: newSocialMediaUrl },
|
|
})
|
|
input.setValue(newSocialMediaUrl)
|
|
form.trigger('submit')
|
|
|
|
expect(mocks.$apollo.mutate).toHaveBeenCalledWith(expected)
|
|
})
|
|
|
|
it('allows the user to cancel editing', () => {
|
|
const cancelButton = wrapper.find('button#cancel')
|
|
cancelButton.trigger('click')
|
|
|
|
expect(wrapper.find('input#editSocialMedia').exists()).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('deleting social media link', () => {
|
|
beforeEach(() => {
|
|
const deleteButton = wrapper.find('a[name="delete"]')
|
|
deleteButton.trigger('click')
|
|
})
|
|
|
|
it('sends the link id to the backend', () => {
|
|
const expected = expect.objectContaining({
|
|
variables: { id: 's1' },
|
|
})
|
|
|
|
expect(mocks.$apollo.mutate).toHaveBeenCalledTimes(1)
|
|
expect(mocks.$apollo.mutate).toHaveBeenCalledWith(expected)
|
|
})
|
|
|
|
it('displays a success message', async () => {
|
|
await flushPromises()
|
|
|
|
expect(mocks.$toast.success).toHaveBeenCalledTimes(1)
|
|
})
|
|
})
|
|
})
|
|
})
|
|
})
|