import { equal, notEqual, deepEqual,deepEql } from 'assert'
import axios from 'axios'
import moxios from 'moxios'
import sinon from 'sinon'
import ServiceSubnetService from '@/domain/services/Business/ServiceSubnetService'

import {
  SERVICESUBNET_GETALL_INPUT,SERVICESUBNET_GETALL_OUTPUT,SERVICESUBNET_ADD_OUTPUT,
  SERVICESUBNET_ADD_INPUT,SERVICESUBNET_ADD_CLIENT,SERVICESUBNET_DELETE_CLIENT,
  SERVICESUBNET_DELETE_INPUT,SERVICESUBNET_DELETE_OUTPUT,SERVICESUBNET_MODDIFY_CLIENT,
  SERVICESUBNET_MODDIFY_INPUT,SERVICESUBNET_MODDIFY_OUTPUT
} from "../../../../mock/Business/ServiceSubnetMock";
import {ELINE_DELETE_CLIENT} 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('ServiceSubnetService (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) {
      ServiceSubnetService.getAllEveSubnetCollection();
      ServiceSubnetService.addEveSubnet(SERVICESUBNET_ADD_CLIENT);
      ServiceSubnetService.deleteEveSubnet(SERVICESUBNET_DELETE_CLIENT);
      ServiceSubnetService.modifyEveSubnet(SERVICESUBNET_MODDIFY_CLIENT);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        // console.log(moxios.requests);
        equal(moxios.requests.count(), 4);
        done()
      })
    });

    it('should getAllEveSubnetCollection', function (done) {
      ServiceSubnetService.getAllEveSubnetCollection().then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: SERVICESUBNET_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, SERVICESUBNET_GETALL_OUTPUT);
          done()
        })
      })
    });

    it('should addEveSubnet', function (done) {
      ServiceSubnetService.addEveSubnet(SERVICESUBNET_ADD_CLIENT).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: SERVICESUBNET_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, SERVICESUBNET_ADD_OUTPUT);
          done()
        })
      })
    })

    it('should deleteEveSubnet', function (done) {
      ServiceSubnetService.deleteEveSubnet(SERVICESUBNET_DELETE_CLIENT).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: SERVICESUBNET_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, SERVICESUBNET_DELETE_OUTPUT);
          done()
        })
      })
    });

    it('should modifyEveSubnet', function(done) {
      ServiceSubnetService.modifyEveSubnet(SERVICESUBNET_MODDIFY_CLIENT).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: SERVICESUBNET_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, SERVICESUBNET_MODDIFY_OUTPUT);
          done()
        })
      })
    })
  })
})
