From 736caa399a4d41c9831affb3a535c34b40dec669 Mon Sep 17 00:00:00 2001
From: Eliot Berriot <contact@eliotberriot.com>
Date: Thu, 11 Jan 2018 22:32:37 +0100
Subject: [PATCH] Unit tests for radios store

---
 front/package.json                         |  1 +
 front/test/unit/karma.conf.js              |  2 +-
 front/test/unit/specs/store/queue.spec.js  | 14 +++-
 front/test/unit/specs/store/radios.spec.js | 86 ++++++++++++++++++++++
 front/test/unit/utils.js                   | 29 +++++---
 5 files changed, 118 insertions(+), 14 deletions(-)
 create mode 100644 front/test/unit/specs/store/radios.spec.js

diff --git a/front/package.json b/front/package.json
index aa5024d2..66ff72d9 100644
--- a/front/package.json
+++ b/front/package.json
@@ -20,6 +20,7 @@
     "js-logger": "^1.3.0",
     "jwt-decode": "^2.2.0",
     "lodash": "^4.17.4",
+    "moxios": "^0.4.0",
     "semantic-ui-css": "^2.2.10",
     "vue": "^2.3.3",
     "vue-lazyload": "^1.1.4",
diff --git a/front/test/unit/karma.conf.js b/front/test/unit/karma.conf.js
index 47b46880..193aaff7 100644
--- a/front/test/unit/karma.conf.js
+++ b/front/test/unit/karma.conf.js
@@ -12,7 +12,7 @@ module.exports = function (config) {
     //    http://karma-runner.github.io/0.13/config/browsers.html
     // 2. add it to the `browsers` array below.
     browsers: ['PhantomJS'],
-    frameworks: ['mocha', 'sinon-chai', 'phantomjs-shim'],
+    frameworks: ['mocha', 'sinon-stub-promise', 'sinon-chai', 'phantomjs-shim'],
     reporters: ['spec', 'coverage'],
     files: [
       '../../node_modules/es6-promise/dist/es6-promise.auto.js',
diff --git a/front/test/unit/specs/store/queue.spec.js b/front/test/unit/specs/store/queue.spec.js
index 2e12a254..8a79c07b 100644
--- a/front/test/unit/specs/store/queue.spec.js
+++ b/front/test/unit/specs/store/queue.spec.js
@@ -1,9 +1,21 @@
+var sinon = require('sinon')
 import _ from 'lodash'
 
 import store from '@/store/queue'
 import { testAction } from '../../utils'
 
 describe('store/queue', () => {
+  var sandbox
+
+  beforeEach(function () {
+    // Create a sandbox for the test
+    sandbox = sinon.sandbox.create()
+  })
+
+  afterEach(function () {
+    // Restore all the things made through the sandbox
+    sandbox.restore()
+  })
   describe('mutations', () => {
     it('currentIndex', () => {
       const state = {}
@@ -302,7 +314,7 @@ describe('store/queue', () => {
       }, done)
     })
     it('shuffle', (done) => {
-      let _shuffle = sinon.stub(_, 'shuffle')
+      let _shuffle = sandbox.stub(_, 'shuffle')
       let tracks = [1, 2, 3]
       let shuffledTracks = [2, 3, 1]
       _shuffle.returns(shuffledTracks)
diff --git a/front/test/unit/specs/store/radios.spec.js b/front/test/unit/specs/store/radios.spec.js
new file mode 100644
index 00000000..3ff8a05e
--- /dev/null
+++ b/front/test/unit/specs/store/radios.spec.js
@@ -0,0 +1,86 @@
+var sinon = require('sinon')
+import moxios from 'moxios'
+import store from '@/store/radios'
+import { testAction } from '../../utils'
+
+describe('store/radios', () => {
+  var sandbox
+
+  beforeEach(function () {
+    sandbox = sinon.sandbox.create()
+    moxios.install()
+  })
+  afterEach(function () {
+    sandbox.restore()
+    moxios.uninstall()
+  })
+
+  describe('mutations', () => {
+    it('current', () => {
+      const state = {}
+      store.mutations.current(state, 1)
+      expect(state.current).to.equal(1)
+    })
+    it('running', () => {
+      const state = {}
+      store.mutations.running(state, false)
+      expect(state.running).to.equal(false)
+    })
+  })
+  describe('actions', () => {
+    it('start', (done) => {
+      moxios.stubRequest('radios/sessions/', {
+        status: 200,
+        response: {id: 2}
+      })
+      testAction({
+        action: store.actions.start,
+        payload: {type: 'favorites', objectId: 0, customRadioId: null},
+        expectedMutations: [
+          {
+            type: 'current',
+            payload: {
+              type: 'favorites',
+              objectId: 0,
+              customRadioId: null,
+              session: 2
+            }
+          },
+          { type: 'running', payload: true }
+        ],
+        expectedActions: [
+          { type: 'populateQueue' }
+        ]
+      }, done)
+    })
+    it('stop', (done) => {
+      testAction({
+        action: store.actions.stop,
+        expectedMutations: [
+          { type: 'current', payload: null },
+          { type: 'running', payload: false }
+        ]
+      }, done)
+    })
+    it('populateQueue', (done) => {
+      moxios.stubRequest('radios/tracks/', {
+        status: 201,
+        response: {track: {id: 1}}
+      })
+      testAction({
+        action: store.actions.populateQueue,
+        params: {state: {running: true, current: {session: 1}}},
+        expectedActions: [
+          { type: 'queue/append', payload: {track: {id: 1}}, options: {root: true} }
+        ]
+      }, done)
+    })
+    it('populateQueue does nothing when not running', (done) => {
+      testAction({
+        action: store.actions.populateQueue,
+        params: {state: {running: false}},
+        expectedActions: []
+      }, done)
+    })
+  })
+})
diff --git a/front/test/unit/utils.js b/front/test/unit/utils.js
index e67c7687..a48e4d30 100644
--- a/front/test/unit/utils.js
+++ b/front/test/unit/utils.js
@@ -33,7 +33,6 @@ export const testAction = ({action, payload, params, expectedMutations, expected
   // mock dispatch
   const dispatch = (type, payload, options) => {
     const a = expectedActions[actionsCount]
-
     try {
       expect(a.type).to.equal(type)
       if (payload) {
@@ -52,17 +51,23 @@ export const testAction = ({action, payload, params, expectedMutations, expected
     }
   }
 
-  // call the action with mocked store and arguments
-  action({ commit, dispatch, ...params }, payload)
-
-  // check if no mutations should have been dispatched
-  if (expectedMutations.length === 0) {
-    expect(mutationsCount).to.equal(0)
-  }
-  if (expectedActions.length === 0) {
-    expect(actionsCount).to.equal(0)
+  let end = function () {
+    // check if no mutations should have been dispatched
+    if (expectedMutations.length === 0) {
+      expect(mutationsCount).to.equal(0)
+    }
+    if (expectedActions.length === 0) {
+      expect(actionsCount).to.equal(0)
+    }
+    if (isOver()) {
+      done()
+    }
   }
-  if (isOver()) {
-    done()
+  // call the action with mocked store and arguments
+  let promise = action({ commit, dispatch, ...params }, payload)
+  if (promise) {
+    promise.then(end)
+  } else {
+    end()
   }
 }
-- 
GitLab