From bc2471c5df4ae772f41c4fe63159a2641a681b8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Sch=C3=A4fer?= Date: Sun, 10 Mar 2019 15:14:58 +0100 Subject: [PATCH] Refactor ReportModal --- components/Modal/DisableModal.vue | 2 +- components/Modal/ReportModal.spec.js | 150 +++++++++++++++++++++++++ components/{ => Modal}/ReportModal.vue | 76 +++++-------- components/ReportModal.spec.js | 124 -------------------- locales/de.json | 1 + locales/en.json | 1 + 6 files changed, 184 insertions(+), 170 deletions(-) create mode 100644 components/Modal/ReportModal.spec.js rename components/{ => Modal}/ReportModal.vue (55%) delete mode 100644 components/ReportModal.spec.js diff --git a/components/Modal/DisableModal.vue b/components/Modal/DisableModal.vue index 90b13d542..fe1bc8df3 100644 --- a/components/Modal/DisableModal.vue +++ b/components/Modal/DisableModal.vue @@ -9,7 +9,7 @@ @@ -55,65 +52,54 @@ export default { components: { SweetalertIcon }, + props: { + isOpen: { + type: Boolean, + default: false + }, + resource: { + type: Object, + default() { + return { id: null, type: 'contribution', name: '' } + } + } + }, data() { return { success: false, loading: false, - disabled: false + disabled: !this.isOpen } }, computed: { - data() { - return this.$store.getters['modal/data'] || {} - }, title() { - if (!this.data.context) return '' - return this.$t(`report.${this.data.context}.title`) + return this.$t(`report.${this.resource.type}.title`) }, message() { - if (!this.data.context) return '' - return this.$t(`report.${this.data.context}.message`, { name: this.name }) - }, - name() { - return this.$filters.truncate(this.data.name, 30) - }, - isOpen() { - return this.$store.getters['modal/open'] === 'report' - } - }, - watch: { - isOpen(open) { - if (open) { - this.success = false - this.disabled = false - this.loading = false - } + const name = this.$filters.truncate(this.resource.name, 30) + return this.$t(`report.${this.resource.type}.message`, { name }) } }, methods: { - close() { - this.$store.commit('modal/SET_OPEN', {}) - }, - async report() { + async confirm() { this.loading = true this.disabled = true try { await this.$apollo.mutate({ mutation: gql` - mutation($id: ID!, $description: String) { - report(id: $id, description: $description) { + mutation($id: ID!) { + report(id: $id) { id } } `, - variables: { - id: this.data.id, - description: '-' - } + variables: { id: this.resource.id } }) this.success = true - this.$toast.success('Thanks for reporting!') - setTimeout(this.close, 1500) + this.$toast.success(this.$t('report.success')) + setTimeout(() => { + this.$emit('close') + }, 1500) } catch (err) { this.$toast.error(err.message) this.disabled = false diff --git a/components/ReportModal.spec.js b/components/ReportModal.spec.js deleted file mode 100644 index d138ea61e..000000000 --- a/components/ReportModal.spec.js +++ /dev/null @@ -1,124 +0,0 @@ -import { shallowMount, render, mount, createLocalVue } from '@vue/test-utils' -import ReportModal from './ReportModal.vue' -import Vue from 'vue' -import Vuex from 'vuex' -import { getters, mutations } from '../store/modal' -import Styleguide from '@human-connection/styleguide' - -const localVue = createLocalVue() - -localVue.use(Vuex) -localVue.use(Styleguide) - -describe('ReportModal.vue', () => { - let Wrapper - let store - let state - let mocks - let $apollo - - beforeEach(() => { - $apollo = { - mutate: jest.fn().mockResolvedValue(null) - } - state = { - open: 'report', - data: {} - } - }) - - describe('mount', () => { - const Wrapper = () => { - mocks = { - $t: () => {}, - $toast: { - success: () => {}, - error: () => {} - }, - $apollo - } - store = new Vuex.Store({ - state, - getters: { - 'modal/open': getters.open, - 'modal/data': getters.data - }, - mutations: { - 'modal/SET_OPEN': mutations.SET_OPEN - } - }) - return mount(ReportModal, { store, mocks, localVue }) - } - - it('renders', () => { - expect(Wrapper().is('div')).toBe(true) - }) - - describe('modal visible', () => { - describe('shows a report', () => { - let wrapper - beforeEach(() => { - state = { - open: 'report', - data: {} - } - wrapper = Wrapper() - }) - - describe('click confirm button', () => { - const clickAction = async () => { - const confirmButton = wrapper.find('.ds-button-danger') - await confirmButton.trigger('click') - } - - it('calls report mutation', async () => { - await clickAction() - expect($apollo.mutate).toHaveBeenCalled() - }) - - it('hides modal', async () => { - expect(wrapper.find('.ds-modal-wrapper').isEmpty()).toEqual(false) - await clickAction() - expect(wrapper.find('.ds-modal-wrapper').isEmpty()).toEqual(true) - }) - - it('disables buttons', async () => { - const expected = { disabled: 'disabled' } - let cancelButton = wrapper.findAll('footer button').at(0) - let confirmButton = wrapper.findAll('footer button').at(1) - expect(cancelButton.attributes()).toEqual( - expect.not.objectContaining(expected) - ) - expect(confirmButton.attributes()).toEqual( - expect.not.objectContaining(expected) - ) - await clickAction() - expect(cancelButton.attributes()).toEqual( - expect.objectContaining(expected) - ) - expect(confirmButton.attributes()).toEqual( - expect.objectContaining(expected) - ) - }) - }) - }) - }) - }) - - describe('shallowMount', () => { - const Wrapper = () => { - mocks = { - $t: () => {}, - $apollo - } - store = new Vuex.Store({}) - return shallowMount(ReportModal, { store, mocks, localVue }) - } - - describe('isOpen', () => { - it('defaults to false', () => { - expect(Wrapper().vm.isOpen).toBe(false) - }) - }) - }) -}) diff --git a/locales/de.json b/locales/de.json index 6e1119e4b..609c7806b 100644 --- a/locales/de.json +++ b/locales/de.json @@ -159,6 +159,7 @@ "report": { "submit": "Meldung senden", "cancel": "Abbrechen", + "success": "Vielen Dank für das Melden!", "user": { "title": "Nutzer melden", "type": "Nutzer", diff --git a/locales/en.json b/locales/en.json index 263f2f09a..fd4a99c78 100644 --- a/locales/en.json +++ b/locales/en.json @@ -159,6 +159,7 @@ "report": { "submit": "Send Report", "cancel": "Cancel", + "success": "Thanks for reporting!", "user": { "title": "Report User", "type": "User",