/* eslint-env mocha */
const Vue = require('vue');
const Vuex = require('vuex');
const assert = require('assert');
const _ = require('lodash');
const s = require('../../src/state/store');
const getters = require('../../src/state/getters');
const coreActions = require('../../src/state/actions');
const kolibri = require('kolibri');

Vue.use(Vuex);

function createStore() {
  return new Vuex.Store({
    state: _.cloneDeep(s.initialState),
    mutations: s.mutations,
    getters,
    actions: coreActions,
  });
}

describe('Vuex store for core module', () => {
  it('handleError action updates core state', () => {
    const store = createStore();
    coreActions.handleError(store, 'catastrophic failure');
    assert.equal(store.state.core.error, 'catastrophic failure');
    assert.equal(store.state.core.loading, false);
    assert.equal(store.state.core.title, 'Error');
  });

  it('handleApiError action updates core state', () => {
    const store = createStore();
    const apiError = { message: 'Too Bad' };
    coreActions.handleApiError(store, apiError);
    assert(store.state.core.error.match(/Too Bad/));
    assert.equal(store.state.core.loading, false);
    assert.equal(store.state.core.title, 'Error');
  });

  describe('kolibriLogin', () => {
    // this prevents kolibriLogin from refreshing page
    const oldHandler = window.onbeforeunload;

    before(() => {
      window.onbeforeunload = () => true;
    });

    after(() => {
      window.onbeforeunload = oldHandler;
    });

    it('happy path', (done) => {
      // mock the SessionResource
      kolibri.resources = {
        SessionResource: {
          createModel: () => ({
            save: () => Promise.resolve({
              // just sending subset of sessionPayload
              id: '123',
              username: 'e_fermi',
              kind: ['cool-guy-user'],
            })
          })
        }
      };

      const store = createStore();

      function runAssertions() {
        const { session } = store.state.core;
        assert.equal(session.id, '123');
        assert.equal(session.username, 'e_fermi');
        assert.deepEqual(session.kind, ['cool-guy-user']);
      }

      function cleanup() {
        delete kolibri.resources;
      }

      coreActions.kolibriLogin(store, {})
        .then(runAssertions)
        .then(cleanup)
        .then(done, done);
    });
  });
});

@@ -1267,58 +1267,18 @@
     
-// this prevents kolibriLogin from refreshing page
+let store;
 %0A   
@@ -1332,32 +1332,89 @@
  before(() =%3E %7B%0A
+      // this prevents kolibriLogin from refreshing page%0A
       window.onb
@@ -1516,79 +1516,96 @@
     
-%7D);%0A%0A    it('happy path', (done) =%3E %7B%0A      // mock the SessionResource
+  delete kolibri.resources;%0A    %7D);%0A%0A    beforeEach(() =%3E %7B%0A      store = createStore();
 %0A   
@@ -1628,25 +1628,91 @@
 rces = %7B
+%7D;
 %0A    
-    
+%7D);%0A%0A    it('successful login', (done) =%3E %7B%0A      kolibri.resources.
 SessionR
@@ -1718,17 +1718,18 @@
 Resource
-:
+ =
  %7B%0A     
@@ -1731,18 +1731,16 @@
         
- 
- 
 createMo
@@ -1753,18 +1753,16 @@
 ) =%3E (%7B%0A
-  
         
@@ -1805,18 +1805,16 @@
         
- 
- 
 // just 
@@ -1846,18 +1846,16 @@
 Payload%0A
-  
         
@@ -1881,18 +1881,16 @@
         
-  
 username
@@ -1902,18 +1902,16 @@
 fermi',%0A
-  
         
@@ -1945,31 +1945,28 @@
 ,%0A          
-  %7D)%0A  
+%7D),%0A
         %7D)%0A 
@@ -1967,62 +1967,17 @@
   %7D)
-%0A        %7D%0A      %7D;%0A%0A      const store = createStore()
+,%0A      %7D
 ;%0A%0A 
@@ -2218,78 +2218,8 @@
  %7D%0A%0A
-      function cleanup() %7B%0A        delete kolibri.resources;%0A      %7D%0A%0A
     
@@ -2303,15 +2303,363 @@
 hen(
-cleanup
+done, done);%0A    %7D);%0A%0A    it('failed login (401)', (done) =%3E %7B%0A      kolibri.resources.SessionResource = %7B%0A        createModel: () =%3E (%7B%0A          save: () =%3E Promise.reject(%7B status: %7B code: 401 %7D %7D),%0A        %7D),%0A      %7D;%0A%0A      coreActions.kolibriLogin(store, %7B%7D)%0A        .then(() =%3E %7B%0A          assert.equal(store.state.core.loginError, 401);%0A        %7D
 )%0A  

Add test case for login failed with 401