import axios from "axios";
import moxios from "moxios";
import {deepEqual, equal, notEqual} from "assert";
import sinon from "sinon";
import {CABINET_GETALL_INPUT, CABINET_GETALL_OUTPUT} from "../../../mock/deviceState/CabinetServiceMock";
import {LOCATION_GETALL_INPUT, LOCATION_GETALL_OUTPUT,
  LOCATION_ADD_CLIENT, LOCATION_ADD_INPUT, LOCATION_ADD_OUTPUT,
  LOCATION_DELETE_CLIENT, LOCATION_DELETE_INPUT, LOCATION_DELETE_OUTPUT,
  LOCATION_MODDIFY_CLIENT, LOCATION_MODDIFY_INPUT, LOCATION_MODDIFY_OUTPUT,
  LOCATION_QUERY_CLIENT, LOCATION_QUERY_INPUT, LOCATION_QUERY_OUTPUT,
  LOCATION_DOWNLOAD_CLIENT,LOCATION_UPLOAD_CLIENT} from "../../../mock/deviceState/LocationServiceMock";
import LocationService from '@/domain/services/LocationService'
import {
  SUBNET_ADD_CLIENT,
  SUBNET_ADD_INPUT, SUBNET_ADD_OUTPUT, SUBNET_DELETE_CLIENT,
  SUBNET_DELETE_INPUT, SUBNET_DELETE_OUTPUT,
  SUBNET_MODDIFY_CLIENT, SUBNET_MODDIFY_INPUT, SUBNET_MODDIFY_OUTPUT
} from "../../../mock/deviceState/SubnetServiceMock";

describe('Location_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('LocationService (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) {
      LocationService.getAllLocationCollection();
      LocationService.addLocation(LOCATION_ADD_CLIENT);
      LocationService.deleteLocations(LOCATION_DELETE_CLIENT);
      LocationService.modifyLocation(LOCATION_MODDIFY_CLIENT);
      LocationService.conditionQueryLocation(LOCATION_QUERY_CLIENT);
      LocationService.downloadLocation(LOCATION_DOWNLOAD_CLIENT);
      LocationService.uploadLocation(LOCATION_UPLOAD_CLIENT);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        // console.log(moxios.requests);
        equal(moxios.requests.count(), 7);
        done()
      })
    });

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

    it('should addLocation', function (done) {
      LocationService.addLocation(LOCATION_ADD_CLIENT).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: LOCATION_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, LOCATION_ADD_OUTPUT);
          done()
        })
      })
    });

    it('should deleteLocations', function (done) {
      LocationService.deleteLocations(LOCATION_DELETE_CLIENT).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: LOCATION_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, LOCATION_DELETE_OUTPUT);
          done()
        })
      })
    });

    it('should modifyLocation', function (done) {
      LocationService.modifyLocation(LOCATION_MODDIFY_CLIENT).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: LOCATION_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, LOCATION_MODDIFY_OUTPUT);
          done()
        })
      })
    });

    it('should conditionQueryLocation', function (done) {
      LocationService.conditionQueryLocation(LOCATION_QUERY_CLIENT).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: LOCATION_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, LOCATION_QUERY_OUTPUT);
          done()
        })
      })
    });


  })
});
