import axios from "axios";
import moxios from "moxios";
import {deepEqual, equal, notEqual} from "assert";
import sinon from "sinon";
import AlarmFilterConfigService from "../../../../src/domain/services/AlarmFilterConfigService";
import {
  ALARMFILTER_ADD_CLIENT,
  ALARMFILTER_ADD_INPUT,
  ALARMFILTER_ADD_OUTPUT,
  ALARMFILTER_DELETE_CLIENT,
  ALARMFILTER_DELETE_INPUT, ALARMFILTER_DELETE_OUTPUT,
  ALARMFILTER_GETALL_INPUT,
  ALARMFILTER_GETALL_OUTPUT,
  ALARMFILTER_MODIFY_CLIENT,
  ALARMFILTER_MODIFY_INPUT,
  ALARMFILTER_MODIFY_OUTPUT
} from "../../../mock/deviceState/AlarmFilterConfigServiceMock";



describe('AlarmFilterConfigService_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('AlarmFilterConfigService (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) {
      AlarmFilterConfigService.getAllAlarmFilterList();
      AlarmFilterConfigService.modifyAlarmFilter(ALARMFILTER_MODIFY_CLIENT);
      AlarmFilterConfigService.addAlarmFilter(ALARMFILTER_ADD_CLIENT);
      AlarmFilterConfigService.deleteAlarmFilter(ALARMFILTER_DELETE_CLIENT);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        // console.log(moxios.requests);
        equal(moxios.requests.count(), 4);
        done()
      })
    });

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

    it('should modifyAlarmFilter', function (done) {
      AlarmFilterConfigService.modifyAlarmFilter(ALARMFILTER_MODIFY_CLIENT).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: ALARMFILTER_MODIFY_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, ALARMFILTER_MODIFY_OUTPUT);
          done()
        })
      })
    });

    it('should addAlarmFilter', function (done) {
      AlarmFilterConfigService.addAlarmFilter(ALARMFILTER_ADD_CLIENT).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: ALARMFILTER_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, ALARMFILTER_ADD_OUTPUT);
          done()
        })
      })
    });

    /*it('should deleteAlarmFilter', function (done) {
      AlarmFilterConfigService.deleteAlarmFilter(ALARMFILTER_DELETE_CLIENT).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: ALARMFILTER_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, ALARMFILTER_DELETE_OUTPUT);
          done()
        })
      })
    });*/




  })
});
