import { equal, notEqual, deepEqual,deepEql } from 'assert'
import axios from 'axios'
import moxios from 'moxios'
import sinon from 'sinon'
import ServiceTypeService from '@/domain/services/Business/ServiceTypeService'
import {
  SERVICETYPE_GETALL_INPUT, SERVICETYPE_GETALL_OUTPUT,
  SERVICETYPE_ADD_CLIENT, SERVICETYPE_ADD_INPUT, SERVICETYPE_ADD_OUTPUT,
  SERVICETYPE_DELETE_CLIENT, SERVICETYPE_DELETE_INPUT, SERVICETYPE_DELETE_OUTPUT,
  SERVICETYPE_MODDIFY_CLIENT, SERVICETYPE_MODDIFY_INPUT, SERVICETYPE_MODDIFY_OUTPUT,
  SERVICETYPE_QUERY_CLIENT, SERVICETYPE_QUERY_INPUT, SERVICETYPE_QUERY_OUTPUT,
} from "../../../../mock/Business/ServiceTypeServiceMock";


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('ServiceTypeService (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) {
      ServiceTypeService.getAllServiceTypeCollection();
      ServiceTypeService.addServiceType(SERVICETYPE_ADD_CLIENT);
      ServiceTypeService.deleteServiceTypes(SERVICETYPE_DELETE_CLIENT);
      ServiceTypeService.modifyServiceType(SERVICETYPE_MODDIFY_CLIENT);
      ServiceTypeService.conditionQueryServiceType(SERVICETYPE_QUERY_CLIENT);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        // console.log(moxios.requests);
        equal(moxios.requests.count(), 5);
        done()
      })
    });

    // 模拟响应
    it('should getAllServiceTypeCollection', function (done) {
      ServiceTypeService.getAllServiceTypeCollection().then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: SERVICETYPE_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, SERVICETYPE_GETALL_OUTPUT);
          done()
        })
      })
    });

    it('should addServiceType', function (done) {
      ServiceTypeService.addServiceType(SERVICETYPE_ADD_CLIENT).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: SERVICETYPE_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, SERVICETYPE_ADD_OUTPUT);
          done()
        })
      })
    });

    it('should deleteServiceTypes', function (done) {
      ServiceTypeService.deleteServiceTypes(SERVICETYPE_DELETE_CLIENT).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: SERVICETYPE_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, SERVICETYPE_DELETE_OUTPUT);
          done()
        })
      })
    });

    it('should modifyServiceType', function (done) {
      ServiceTypeService.modifyServiceType(SERVICETYPE_MODDIFY_CLIENT).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: SERVICETYPE_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, SERVICETYPE_MODDIFY_OUTPUT);
          done()
        })
      })
    });

    it('should conditionQueryServiceType', function (done) {
      ServiceTypeService.conditionQueryServiceType(SERVICETYPE_QUERY_CLIENT).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: SERVICETYPE_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, SERVICETYPE_QUERY_OUTPUT);
          done()
        })
      })
    });

  })
});
