import WsInputPillsController from './ws-input-pills.controller';

const BACKSPACE = 8;
const DELIMITER = 44;
const REGULAR_CHARACTER = 65;

describe('WsInputPillsController', () => {
  let subject;
  let mockEvent;

  beforeEach(() => {
    subject = new WsInputPillsController();
    subject.onModelChange = () => {};
    subject.placeholderText = 'Initial placeholder';
  });

  describe('keydownHandler', () => {
    beforeEach(() => {
      mockEvent = new Event('keydown');
      mockEvent.keyCode = BACKSPACE;
      mockEvent.which = BACKSPACE;
    });

    describe('when the input field is empty', () => {
      beforeEach(() => {
        subject.pillInput = null;
        subject.pills = ['foo', 'bar'];
      });

      it('removes the previous pill when user presses BACKSPACE', () => {
        subject.keydownHandler(mockEvent);

        expect(subject.pills.length).toEqual(1);
      });
    });

    describe('when the user is in the middle of typing', () => {
      beforeEach(() => {
        subject.pillInput = 'Still-typing';
        subject.pills = ['foo', 'bar'];
        subject.pillInput = 'input';
      });

      it('does nothing extra when user presses BACKSPACE', () => {
        subject.keydownHandler(mockEvent);

        expect(subject.pills.length).toEqual(2);
      });
    });
  });

  describe('keypressHandler', () => {
    beforeEach(() => {
      subject.pills = ['foo', 'bar'];
      subject.pillInput = 'input';
    });

    describe('when the user is typing "regular" characters', () => {
      beforeEach(() => {
        mockEvent = new Event('keypress');
        mockEvent.keyCode = REGULAR_CHARACTER;
        mockEvent.which = REGULAR_CHARACTER;
      });

      it('does nothing extra', () => {
        subject.keypressHandler(mockEvent);

        expect(subject.pills.length).toEqual(2);
      });
    });

    describe('when the user presses a special delimiter (comma, enter, or space)', () => {
      beforeEach(() => {
        mockEvent = new Event('keypress');
        mockEvent.keyCode = DELIMITER;
        mockEvent.which = DELIMITER;
      });

      it('creates a new pill from the input value', () => {
        subject.keypressHandler(mockEvent);

        expect(subject.pills.length).toEqual(3);
      });
    });
  });
});
