From f15d6b21b8f0e5a42313f3568fd25913af88a6c9 Mon Sep 17 00:00:00 2001
From: Dominik Seeger <dominik.seeger@gmx.net>
Date: Fri, 4 Jan 2019 12:20:58 +0100
Subject: [PATCH] added tests for DataExport component

---
 .../tests/unit/components/DataExport.spec.ts  | 97 +++++++++++++++++++
 1 file changed, 97 insertions(+)
 create mode 100644 frontend/tests/unit/components/DataExport.spec.ts

diff --git a/frontend/tests/unit/components/DataExport.spec.ts b/frontend/tests/unit/components/DataExport.spec.ts
new file mode 100644
index 00000000..41f965a2
--- /dev/null
+++ b/frontend/tests/unit/components/DataExport.spec.ts
@@ -0,0 +1,97 @@
+import Vuex from 'vuex'
+import { mount, createLocalVue } from '@vue/test-utils'
+import DataExport from '@/components/export/DataExport.vue'
+import sinon from 'sinon'
+import chai from 'chai'
+import VueRouter from 'vue-router'
+
+import * as api from '@/api'
+chai.should()
+
+let localVue = createLocalVue()
+localVue.use(Vuex)
+localVue.use(VueRouter)
+let router = new VueRouter()
+
+// @ts-ignore
+global.requestAnimationFrame = cb => cb()
+
+describe('DataExport Component Unit Tests', () => {
+  let store: any = null
+  let consoleTemp = {
+    warn: console.warn,
+    error: console.error
+  }
+
+  // for creating test data
+  let students = [{
+    Matrikel: 1000000,
+    Name: 'test',
+    Username: 'test',
+    Sum: 100,
+    Exam: 'test',
+    Password: 'test',
+    Scores: { type: 'test', score: 100 }
+  }]
+
+  before(function () {
+    console.warn = function () {}
+    console.error = function () {}
+  })
+
+  after(function () {
+    console.warn = consoleTemp.warn
+    console.error = consoleTemp.error
+  })
+
+  beforeEach(() => {
+    store = new Vuex.Store({
+      state: {}
+    })
+  })
+
+  afterEach(() => {
+    sinon.restore()
+  })
+
+  it('should download CSV when selected', () => {
+    let wrapper = mount(DataExport, { localVue: localVue, store })
+    wrapper.vm.$data.exportType = 'CSV'
+    let spy = sinon.spy()
+    // @ts-ignore
+    sinon.replace(wrapper.vm, 'jsonToCSV', spy)
+    // @ts-ignore
+    sinon.replace(wrapper.vm, 'createDownloadPopup', sinon.spy())
+    // @ts-ignore
+    wrapper.vm.optionalConvertAndCreatePopup(students)
+    spy.called.should.equal(true)
+  })
+  it('should download JSON when selected', () => {
+    let wrapper = mount(DataExport, { localVue: localVue, store })
+    wrapper.vm.$data.exportType = 'JSON'
+    let spy = sinon.spy()
+    // @ts-ignore
+    sinon.replace(wrapper.vm, 'jsonToCSV', spy)
+    // @ts-ignore
+    sinon.replace(wrapper.vm, 'createDownloadPopup', sinon.spy())
+    // @ts-ignore
+    wrapper.vm.optionalConvertAndCreatePopup(students)
+    spy.called.should.equal(false)
+  })
+  it('should download obfuscated data when no mapping was selected', async () => {
+    let wrapper = mount(DataExport, { localVue: localVue, store })
+    let stub = sinon.stub().returns(Promise.resolve({ data: students }))
+    let spy = sinon.spy()
+    // @ts-ignore replace ax.post because of fetch in getExportFile
+    sinon.replace(api.default, 'post', stub)
+    // @ts-ignore
+    sinon.replace(wrapper.vm, 'optionalConvertAndCreatePopup', spy)
+    // @ts-ignore
+    await wrapper.vm.getExportFile('data')
+    spy.called.should.equal(true)
+    spy.calledWithExactly(students).should.equal(true)
+  })
+  it('should download deobfuscated data when no mapping was selected', async () => {
+
+  })
+})
-- 
GitLab