import { equal, notEqual, deepEqual,deepEql } from 'assert'
import axios from 'axios'
import moxios from 'moxios'
import sinon from 'sinon'
import ElineTemplateService from '@/domain/services/Business/ElineTemplateService'
import {
  ELINETEMPLATE_ADD_CLIENT, ELINETEMPLATE_ADD_INPUT, ELINETEMPLATE_ADD_OUTPUT,
  ELINETEMPLATE_DELETE_CLIENT, ELINETEMPLATE_DELETE_INPUT, ELINETEMPLATE_DELETE_OUTPUT,
  ELINETEMPLATE_GETALL_INPUT, ELINETEMPLATE_GETALL_OUTPUT,
  ELINETEMPLATE_MODDIFY_CLIENT, ELINETEMPLATE_MODDIFY_INPUT, ELINETEMPLATE_MODDIFY_OUTPUT,
  ELINETEMPLATE_QUERY_CLIENT, ELINETEMPLATE_QUERY_INPUT, ELINETEMPLATE_QUERY_OUTPUT,
} from "../../../../mock/Business/ElineTemplateServiceMock";
import {
  ELINE_ADD_CLIENT,
  ELINE_GETALL_INPUT,
  ELINE_GETALL_OUTPUT,
  ELINE_QUERY_INPUT, ELINE_QUERY_OUTPUT
} from "../../../../mock/Business/ElineServiceMock";

describe('moxios', function () {
  it('should install', function () {
    let defaultAdapter = axios.defaults.adapter
    moxios.install()
    notEqual(axios.defaults.adapter, defaultAdapter)
    moxios.uninstall()
  })

  it('should uninstall', function () {
    let defaultAdapter = axios.defaults.adapter
    moxios.install()
    moxios.uninstall()
    equal(axios.defaults.adapter, defaultAdapter)
  })

  describe('ElineTemplateService (Rest)', function () {
    let onFulfilled
    let onRejected

    beforeEach(function () {
      moxios.install()
      // sinon.js 中 spy 主要用来监视函数的调用情况，
      // sinon 对待监视的函数进行 wrap 包装，
      // 因此可以通过它清楚的知道，该函数被调用过几次，传入什么参数返回什么结果，甚至是抛出的异常情况。
      onFulfilled = sinon.spy()
      onRejected = sinon.spy()
    })

    afterEach(function () {
      moxios.uninstall()
    })

    // 拦截请求
    it('should intercept requests', function (done) {
      ElineTemplateService.getAllElineTemplateCollection();
      ElineTemplateService.addElineTemplate(ELINETEMPLATE_ADD_CLIENT);
      ElineTemplateService.deleteElineTemplates(ELINETEMPLATE_DELETE_CLIENT);
      ElineTemplateService.modifyElineTemplate(ELINETEMPLATE_MODDIFY_CLIENT);
      ElineTemplateService.conditionQueryElineTemplate(ELINETEMPLATE_QUERY_CLIENT);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        // console.log(moxios.requests);
        equal(moxios.requests.count(), 5);
        done()
      })
    });

// 模拟响应
    it('should getAllElineTemplateCollection', function (done) {
      ElineTemplateService.getAllElineTemplateCollection().then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: ELINETEMPLATE_GETALL_INPUT
        }).then(function () {
          // console.log(onFulfilled.getCall(0));
          let response = onFulfilled.getCall(0).args[0];
          equal(onFulfilled.called, true);
          // equal(response.status, 200);
          // console.log(response);
          deepEqual(response, ELINETEMPLATE_GETALL_OUTPUT);
          done()
        })
      })
    });

    it('should addElineTemplate', function (done) {
      ElineTemplateService.addElineTemplate(ELINETEMPLATE_ADD_CLIENT).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: ELINETEMPLATE_ADD_INPUT
        }).then(function () {
          // console.log(onFulfilled.getCall(0));
          let response = onFulfilled.getCall(0).args[0];
          //console.log(response);
          equal(onFulfilled.called, true);
          // equal(response.status, 200);
          deepEqual(response, ELINETEMPLATE_ADD_OUTPUT);
          done()
        })
      })
    });

    it('should deleteElineTemplate', function (done) {
      ElineTemplateService.deleteElineTemplates(ELINETEMPLATE_DELETE_CLIENT).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: ELINETEMPLATE_DELETE_INPUT
        }).then(function () {
          // console.log(onFulfilled.getCall(0));
          let response = onFulfilled.getCall(0).args[0];
          //console.log(response);
          equal(onFulfilled.called, true);
          // equal(response.status, 200);
          deepEqual(response, ELINETEMPLATE_DELETE_OUTPUT);
          done()
        })
      })
    });

    it('should modifyElineTemplate', function (done) {
      ElineTemplateService.modifyElineTemplate(ELINETEMPLATE_MODDIFY_CLIENT).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: ELINETEMPLATE_MODDIFY_INPUT
        }).then(function () {
          // console.log(onFulfilled.getCall(0));
          let response = onFulfilled.getCall(0).args[0];
          //console.log(response);
          equal(onFulfilled.called, true);
          // equal(response.status, 200);
          deepEqual(response, ELINETEMPLATE_MODDIFY_OUTPUT);
          done()
        })
      })
    });

    it('should conditionQueryElineTemplate', function (done) {
      ElineTemplateService.conditionQueryElineTemplate(ELINETEMPLATE_QUERY_CLIENT).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: ELINETEMPLATE_QUERY_INPUT
        }).then(function () {
          // console.log(onFulfilled.getCall(0));
          let response = onFulfilled.getCall(0).args[0];
          //console.log(response);
          equal(onFulfilled.called, true);
          // equal(response.status, 200);
          deepEqual(response, ELINETEMPLATE_QUERY_OUTPUT);
          done()
        })
      })
    });

  })
});
