/* eslint-env jasmine */
/* eslint-disable import/no-webpack-loader-syntax */
import Vue from 'vue'
import Vuex from 'vuex'
import RequestPaneComponentInjector from '!!vue-loader?inject!./RequestPaneComponent.vue'

Vue.use(Vuex);

describe('RequestPaneComponent', () => {
  describe('mounted', () => {
    let RequestPaneComponent = null;
    beforeEach(() => {
      RequestPaneComponent = RequestPaneComponentInjector({
        './../Parameters/ParametersComponent.vue': {},
        '../../shared/request-tasks/RequestTasks.js': {},
        './../Ace/AceComponent.vue': { render: () => { } }
      })
    });
    xit('should subscribe to the LOAD_REQUEST mutation', (done) => {
      const Ctor = Vue.extend(RequestPaneComponent);
      const mockTab = {
        request: {
          parameters: [],
          headers: [],
          body: 'Code Body'
        }
      };
      const store = new Vuex.Store({
        modules: {
          TabModule: {
            namespaced: true,
            mutations: {
              LOAD_REQUEST: () => { }
            }
          },
          SettingsModule: { }
        }
      });
      const vm = new Ctor({ store, propsData: { tab: mockTab } });
      vm.$refs.ace = { triggerUpdate: jasmine.createSpy() };
      store.commit('TabModule/LOAD_REQUEST');
      Vue.nextTick(() => {
        expect(vm.$refs.ace.triggerUpdate).toHaveBeenCalled();
        done()
      })
    });
    it('should not trigger the update if the ace component cannot be found', (done) => {
      const Ctor = Vue.extend(RequestPaneComponent);
      const mockTab = {
        request: {
          parameters: [],
          headers: []
        }
      };
      const store = new Vuex.Store({
        modules: {
          TabModule: {
            namespaced: true,
            mutations: {
              LOAD_REQUEST: () => {}
            }
          },
          SettingsModule: { }
        }
      });
      const vm = new Ctor({ store, propsData: { tab: mockTab } });
      vm.$refs.ace = undefined;
      store.commit('TabModule/LOAD_REQUEST');
      Vue.nextTick(() => {
        done()
      })
    })
  });
  describe('triggerResize', () => {
    let RequestPaneComponent = null;
    beforeEach(() => {
      RequestPaneComponent = RequestPaneComponentInjector({
        './../Parameters/ParametersComponent.vue': {},
        '../../shared/request-tasks/RequestTasks.js': {}
      })
    });
    it('should trigger ace to resize if available', () => {
      const Ctor = Vue.extend(RequestPaneComponent);
      const store = new Vuex.Store({
        mutations: {
          LOAD_REQUEST: () => {}
        }
      });
      const mockTab = { };
      const vm = new Ctor({ store, propsData: { tab: mockTab } });
      vm.$refs.ace = { triggerResize: jasmine.createSpy() };
      vm.triggerResize();
      expect(vm.$refs.ace.triggerResize).toHaveBeenCalled()
    });
    it('should not trigger ace to resize if not available', () => {
      const Ctor = Vue.extend(RequestPaneComponent);
      const store = new Vuex.Store({
        mutations: {
          LOAD_REQUEST: () => {}
        }
      });
      const mockTab = { };
      const vm = new Ctor({ store, propsData: { tab: mockTab } });
      vm.$refs.ace = undefined;
      vm.triggerResize()
    })
  });
  describe('onUpdateLanguage', () => {
    it('should update the request and add a header if one doesn\'t exist', () => {
      const MockRequestTasks = {
        determineHeader: jasmine.createSpy().and.returnValue('determinedHeader')
      };
      const RequestPaneComponent = RequestPaneComponentInjector({
        './../Parameters/ParametersComponent.vue': {},
        '../../shared/request-tasks/RequestTasks.js': MockRequestTasks
      });
      const Ctor = Vue.extend(RequestPaneComponent);
      const mockTab = {
        request: {
          headers: []
        }
      };
      const mockStore = {
        modules: {
          ProjectModule: {
            namespaced: true,
            mutations: {
              UPDATE_REQUEST: jasmine.createSpy()
            }
          },
          TabModule: {
            namespaced: true,
            mutations: {
              UPDATE_HEADER: jasmine.createSpy(),
              ADD_HEADER: jasmine.createSpy().and.callFake(() => mockTab.request.headers.push({ attribute: 'Content-Type', value: '' }))
            }
          }
        }
      };
      const store = new Vuex.Store(mockStore);
      const vm = new Ctor({ propsData: { tab: mockTab }, store });
      vm.onUpdateLanguage({ target: { value: 'JSON' } });
      expect(mockStore.modules.ProjectModule.mutations.UPDATE_REQUEST).toHaveBeenCalledWith({}, { request: mockTab.request, newRequest: { language: 'JSON' } });
      expect(mockStore.modules.TabModule.mutations.ADD_HEADER).toHaveBeenCalledWith({}, { tab: mockTab, header: { attribute: 'Content-Type' } });
      expect(mockStore.modules.TabModule.mutations.UPDATE_HEADER).toHaveBeenCalledWith({}, { header: mockTab.request.headers[0], newHeader: { value: 'determinedHeader' } })
    });
    it('should update the request and not add a header if one exists', () => {
      const MockRequestTasks = {
        determineHeader: jasmine.createSpy().and.returnValue('determinedHeader')
      };
      const RequestPaneComponent = RequestPaneComponentInjector({
        './../Parameters/ParametersComponent.vue': {},
        '../../shared/request-tasks/RequestTasks.js': MockRequestTasks
      });
      const Ctor = Vue.extend(RequestPaneComponent);
      const mockTab = {
        request: {
          headers: [{ attribute: 'Content-Type', value: 'something' }]
        }
      };
      const mockStore = {
        modules: {
          ProjectModule: {
            namespaced: true,
            mutations: {
              UPDATE_REQUEST: jasmine.createSpy()
            }
          },
          TabModule: {
            namespaced: true,
            mutations: {
              UPDATE_HEADER: jasmine.createSpy(),
              ADD_HEADER: jasmine.createSpy()
            }
          }
        }
      };
      const store = new Vuex.Store(mockStore);
      const vm = new Ctor({ propsData: { tab: mockTab }, store });
      vm.onUpdateLanguage({ target: { value: 'JSON' } });
      expect(mockStore.modules.ProjectModule.mutations.UPDATE_REQUEST).toHaveBeenCalledWith({}, { request: mockTab.request, newRequest: { language: 'JSON' } });
      expect(mockStore.modules.TabModule.mutations.ADD_HEADER).not.toHaveBeenCalled();
      expect(mockStore.modules.TabModule.mutations.UPDATE_HEADER).toHaveBeenCalledWith({}, { header: mockTab.request.headers[0], newHeader: { value: 'determinedHeader' } })
    })
  })
});
