// jshint esversion: 6
'use strict';

import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import React from 'react';
import { Wizard } from './Wizard';
import { mount } from '../../build/enzyme';

var MyWizardStep =
/*#__PURE__*/
function (_React$Component) {
  _inheritsLoose(MyWizardStep, _React$Component);

  function MyWizardStep() {
    return _React$Component.apply(this, arguments) || this;
  }

  var _proto = MyWizardStep.prototype;

  _proto.render = function render() {
    return React.createElement("div", null);
  };

  return MyWizardStep;
}(React.Component);

var WIZARD_STEPS = [React.createElement(MyWizardStep, {
  key: 0,
  id: 0
}), React.createElement(MyWizardStep, {
  key: 1,
  id: 1,
  disabled: true
}), React.createElement(MyWizardStep, {
  key: 2,
  id: 2
}), React.createElement(MyWizardStep, {
  key: 3,
  id: 3,
  validation: function validation() {
    return Promise.reject(new Error('Step Failure'));
  }
}), React.createElement(MyWizardStep, {
  key: 4,
  id: 4
}), React.createElement(MyWizardStep, {
  key: 5,
  id: 5,
  disabled: true
})];

var MyWizard =
/*#__PURE__*/
function (_Wizard) {
  _inheritsLoose(MyWizard, _Wizard);

  function MyWizard() {
    return _Wizard.apply(this, arguments) || this;
  }

  var _proto2 = MyWizard.prototype;

  _proto2.getSnapshotKey = function getSnapshotKey() {
    return this.props.snapshotKey;
  };

  _proto2.getSnapshotValue = function getSnapshotValue() {
    return this.props.data;
  };

  _proto2.getSteps = function getSteps() {
    return WIZARD_STEPS;
  };

  return MyWizard;
}(Wizard);

describe('Wizard', function () {
  var snapshotKey = function snapshotKey() {
    return 'snapshotKey' + Math.random();
  };

  var instance = function instance(wrapper) {
    return wrapper ? wrapper.instance() : mount(React.createElement(MyWizard, null)).instance();
  };

  it.skip('saves snapshot', function () {
    var data = {
      someKey: 'someData'
    };
    var ls = window.localStorage;
    var setItemMock = jest.fn(function () {
      return ls.setItem.apply(ls, arguments);
    });
    window.localStorage = {
      setItem: setItemMock
    };
    var key = snapshotKey();
    var wrapper = mount(React.createElement(MyWizard, {
      snapshotKey: key
    }));
    wrapper.setProps({
      data: data
    });
    expect(setItemMock.mock.calls.length).toBe(1);
    expect(setItemMock.mock.calls[0]).toEqual([key, JSON.stringify(data)]);
    window.localStorage = ls;
  });
  it('enables snapshot recording by default', function () {
    var inst = instance();
    expect(inst.snapshotRecording).toBe(true);
  });
  it('thows RangeError if index out of bounds', function () {
    var inst = instance();
    expect(function () {
      inst.setCurrentStepIndex(WIZARD_STEPS.length);
    }).toThrow(RangeError, /out of ranges/);
  });
  it('sibling step index in forward direction', function () {
    var inst = instance();
    var direction = 1;
    inst.goToStep(0);
    inst.goNext(direction);
    expect(inst.getCurrentStepIndex()).toBe(2);
  });
  it('back navigation', function () {
    var inst = instance();
    inst.goToStep(2);
    inst.goBack();
    expect(inst.getCurrentStepIndex()).toBe(0);
  }); // XXX: inconsistency return - null (see getStepIndexById -1)

  it('sibling step index - null', function () {
    var inst = instance();
    var direction = 1;
    inst.goToStep(4);
    expect(inst.getEnabledSiblingStepIndex(direction)).toBe(null);
  });
  it('keeps getMaxVisitedStepIndex', function () {
    var inst = instance();
    inst.goToStep(2);
    inst.goToStep(0);
    expect(inst.getMaxVisitedStepIndex()).toBe(2);
  });
  it('step validation', function () {
    var inst = instance();
    inst.goToStep(3); // step with hardcoded validation (failure)

    inst.goNext();
    expect(inst.getCurrentStepIndex()).toBe(3);
  });
});