From 7936d98050ffb9bfb6a0b719414c8de247d3bb69 Mon Sep 17 00:00:00 2001 From: Angelina Filippova Date: Sat, 25 May 2019 14:20:13 +0200 Subject: [PATCH] Add tests for fetching reports and scrolling event --- src/api/__mocks__/reports.js | 14 ++++ src/views/reports/components/TimelineItem.vue | 2 +- test/views/reports/index.test.js | 65 ++++--------------- 3 files changed, 27 insertions(+), 54 deletions(-) create mode 100644 src/api/__mocks__/reports.js diff --git a/src/api/__mocks__/reports.js b/src/api/__mocks__/reports.js new file mode 100644 index 00000000..2cdeca07 --- /dev/null +++ b/src/api/__mocks__/reports.js @@ -0,0 +1,14 @@ +const reports = [ + { created_at: '2019-05-21T21:35:33.000Z', account: { acct: 'benj', display_name: 'Benjamin Fame' }, actor: { acct: 'admin' }, state: 'open', id: '2', content: 'This is a report', statuses: [] }, + { created_at: '2019-05-20T22:45:33.000Z', account: { acct: 'alice', display_name: 'Alice Pool' }, actor: { acct: 'admin2' }, state: 'resolved', id: '1', content: 'Please block this user', statuses: [] }, + { created_at: '2019-05-18T13:01:33.000Z', account: { acct: 'nick', display_name: 'Nick Keys' }, actor: { acct: 'admin' }, state: 'closed', id: '3', content: '', statuses: [] }, + { created_at: '2019-05-21T21:35:33.000Z', account: { acct: 'benj', display_name: 'Benjamin Fame' }, actor: { acct: 'admin' }, state: 'open', id: '5', content: 'This is a report', statuses: [] }, + { created_at: '2019-05-20T22:45:33.000Z', account: { acct: 'alice', display_name: 'Alice Pool' }, actor: { acct: 'admin2' }, state: 'resolved', id: '7', content: 'Please block this user', statuses: [] }, + { created_at: '2019-05-18T13:01:33.000Z', account: { acct: 'nick', display_name: 'Nick Keys' }, actor: { acct: 'admin' }, state: 'closed', id: '6', content: '', statuses: [] }, + { created_at: '2019-05-18T13:01:33.000Z', account: { acct: 'nick', display_name: 'Nick Keys' }, actor: { acct: 'admin' }, state: 'closed', id: '4', content: '', statuses: [] } +] + +export async function fetchReports(limit, max_id, authHost, token) { + const filteredReports = max_id.length > 0 ? reports.slice(5) : reports.slice(0, 5) + return Promise.resolve({ data: { reports: filteredReports }}) +} diff --git a/src/views/reports/components/TimelineItem.vue b/src/views/reports/components/TimelineItem.vue index 4dd021aa..e91b7824 100644 --- a/src/views/reports/components/TimelineItem.vue +++ b/src/views/reports/components/TimelineItem.vue @@ -38,7 +38,7 @@ Actor: User's avatar {{ report.actor.acct }} diff --git a/test/views/reports/index.test.js b/test/views/reports/index.test.js index ce711d75..cb1f10da 100644 --- a/test/views/reports/index.test.js +++ b/test/views/reports/index.test.js @@ -4,14 +4,17 @@ import Element from 'element-ui' import Reports from '@/views/reports/index' import storeConfig from './store.conf' import { cloneDeep } from 'lodash' +import flushPromises from 'flush-promises' config.mocks["$t"] = () => {} +config.stubs['reports-filter'] = '
' +config.stubs['timeline-item'] = '
' const localVue = createLocalVue() localVue.use(Vuex) localVue.use(Element) -// jest.mock('@/api/reports') +jest.mock('@/api/reports') describe('Reports', () => { let store @@ -26,68 +29,24 @@ describe('Reports', () => { localVue }) - await wrapper.vm.$nextTick() + await flushPromises() const initialReports = store.state.reports.fetchedReports.length - expect(initialReports).toEqual(3) + expect(initialReports).toEqual(5) done() }) - it('shows notes', () => { + it('loads more reports on scroll', async (done) => { const wrapper = mount(Reports, { store, localVue }) - const note = wrapper.find('.el-collapse-item__content') - expect(note.isVisible()).toBe(false) - - const button = wrapper.find('.el-collapse-item__header') - button.trigger('click') - expect(note.isVisible()).toBe(true) - - button.trigger('click') - expect(note.isVisible()).toBe(false) - }) - - it('creates a note', () => { - const wrapper = mount(Reports, { - store, - localVue - }) - const noteTextArea = wrapper.find('textarea') - const notes = store.state.reports.fetchedReports[0].notes - - expect(noteTextArea.isVisible()).toBe(false) - - wrapper.find('.el-button--default').trigger('click') - expect(noteTextArea.isVisible()).toBe(true) - expect(notes.length).toBe(2) - - noteTextArea.setValue('new note') - wrapper.find('.submit-button').trigger('click') - const updatedNotes = store.state.reports.fetchedReports[0].notes - expect(updatedNotes.length).toBe(3) - - wrapper.find('.new-note .el-icon-close').trigger('click') - expect(noteTextArea.isVisible()).toBe(false) - }) - - it('deletes a note', () => { - store.dispatch('FetchReports') - expect(store.state.reports.fetchedReports[0].notes.length).toBe(3) - - store.dispatch('DeleteNote', { reportId: '1', noteId: '2' }) - expect(store.state.reports.fetchedReports[0].notes.length).toBe(2) - }) - - it('loads more reports on scroll', () => { - const wrapper = mount(Reports, { - store, - localVue - }) - expect(store.state.reports.fetchedReports.length).toEqual(3) + await flushPromises() + expect(store.state.reports.fetchedReports.length).toEqual(5) window.dispatchEvent(new CustomEvent('scroll', { detail: 2000 })) - expect(store.state.reports.fetchedReports.length).toEqual(6) + await flushPromises() + expect(store.state.reports.fetchedReports.length).toEqual(7) + done() }) })