<commit_before>/* global jest describe beforeEach it expect */

jest.dontMock('../components/ActionArea');
jest.dontMock('../components/DatePicker');
jest.dontMock('../components/DisabledDay');
jest.dontMock('../components/Day');
jest.dontMock('../utils/inject-style');
jest.dontMock('../utils/date-helpers');
jest.dontMock('../config/i18n');
jest.dontMock('../style/date-picker');

import React from 'react';
import ReactDOM from 'react-dom';
import TestUtils from 'react-addons-test-utils';

// Babel would move an import in front of the jest.dontMock. That's why require
// is used instead of import.
const DatePicker = require('../components/DatePicker').default;
const injectStyle = require('../utils/inject-style');

describe('DatePicker', () => {
  it('should initialise props as expected', () => {
    const datePicker = TestUtils.renderIntoDocument(
      <DatePicker />
    );
    expect(datePicker.props.tabIndex).toBe(0);
    expect(datePicker.props['aria-label']).toBe('datepicker');
    expect(datePicker.props.disabled).toBe(false);
    expect(datePicker.props.readOnly).toBe(false);
    expect(datePicker.props.showOtherMonthDate).toBe(true);
  });

  it('should have undefined date value if value is not passed in props', () => {
    const datePicker = TestUtils.renderIntoDocument(
      <DatePicker />
    );
    expect(datePicker.state.selectedDate).toBeUndefined();
  });

  it('should change date when a day is focused and enter key is pressed', () => {
    const datePicker = TestUtils.renderIntoDocument(
      <DatePicker className="date_picker_wrapper"/>
    );

    expect(datePicker.state.selectedDate).toBeUndefined();
    const dayPickerWrapper = TestUtils.scryRenderedDOMComponentsWithClass(datePicker, 'date_picker_wrapper')[0];
    TestUtils.Simulate.focus(dayPickerWrapper);
    TestUtils.Simulate.keyDown(dayPickerWrapper, { key: 'Enter' });
    expect(datePicker.state.selectedDate).toBeGreaterThan(0);
  });

  describe('injecting styles', () => {
    beforeEach(() => {
      injectStyle.injectStyles = jest.genMockFunction();
    });

    it('should inject styles for hover, active & foucs', () => {
      TestUtils.renderIntoDocument(
        <DatePicker />
      );

      expect(injectStyle.injectStyles.mock.calls.length).toBe(1);
      const styles = injectStyle.injectStyles.mock.calls[0][0];
      expect(styles[0].id).toContain('prevMonthNav-style-id');
      expect(styles[1].id).toContain('nextMonthNav-style-id');
      expect(styles[2].id).toContain('wrapper-style-id');
    });
  });

  it('should select / deselect date when space key is pressed', () => {
    let dateSelected;
    const datePicker = TestUtils.renderIntoDocument(
      <DatePicker
        onUpdate={ (obj) => dateSelected = obj.value }
        className="date_picker_wrapper"
      />
    );

    expect(datePicker.state.selectedDate).toBeUndefined();

    // const day = TestUtils.scryRenderedDOMComponentsWithClass(datePicker, 'day_test')[8];
    const dayPickerWrapper = TestUtils.scryRenderedDOMComponentsWithClass(datePicker, 'date_picker_wrapper')[0];
    TestUtils.Simulate.focus(dayPickerWrapper);
    TestUtils.Simulate.keyDown(dayPickerWrapper, { key: ' ' });
    expect(datePicker.state.selectedDate).toBeGreaterThan(0);
    expect(dateSelected).toBeGreaterThan(0);

    // expect(dateSelected.getDay()).toBeGreaterThan(0);
    TestUtils.Simulate.keyDown(dayPickerWrapper, { key: ' ' });
    expect(datePicker.state.selectedDate).toBeUndefined();
    expect(dateSelected).toBeUndefined();
  });

  it('should not change date when a day is focused and enter key is pressed if component is disabled or readOnly', () => {
    const disabledDatePicker = TestUtils.renderIntoDocument(
      <DatePicker dayProps={{ className: 'day_test' }} disabled/>
    );
    expect(disabledDatePicker.state.selectedDate).toBeUndefined();
    let day = TestUtils.scryRenderedDOMComponentsWithClass(disabledDatePicker, 'day_test')[8];
    TestUtils.Simulate.focus(day);
    TestUtils.Simulate.keyDown(day, { key: 'Enter' });
    expect(disabledDatePicker.state.selectedDate).toBeUndefined();

    const readOnlyDatePicker = TestUtils.renderIntoDocument(
      <DatePicker dayProps={{ className: 'day_test' }} readOnly/>
    );
    expect(readOnlyDatePicker.state.selectedDate).toBeUndefined();
    day = TestUtils.scryRenderedDOMComponentsWithClass(readOnlyDatePicker, 'day_test')[8];
    TestUtils.Simulate.focus(day);
    TestUtils.Simulate.keyDown(day, { key: 'Enter' });
    expect(readOnlyDatePicker.state.selectedDate).toBeUndefined();
  });

  it('should change focusedDateKey on mouse down + up', () => {
    const datePicker = TestUtils.renderIntoDocument(
      <DatePicker dayProps={{ className: 'day_test' }}/>
    );

    expect(datePicker.state.focusedDateKey).toBeUndefined();
    const day = TestUtils.scryRenderedDOMComponentsWithClass(datePicker, 'day_test')[8];
    TestUtils.Simulate.mouseDown(day, { button: 0 });
    TestUtils.Simulate.mouseUp(day, { button: 0 });
    expect(datePicker.state.focusedDateKey).toBeDefined();
  });

  it('should reduce focusedDateKey by 1 when arrowLeft is pressed', () => {
    const datePicker = TestUtils.renderIntoDocument(
      <DatePicker className="date_picker"/>
    );

    expect(datePicker.state.focusedDateKey).toBeUndefined();<commit_msg>test(DatePicker): minor indentation change<commit_after>/* global jest describe beforeEach it expect */

jest.dontMock('../components/ActionArea');
jest.dontMock('../components/DatePicker');
jest.dontMock('../components/DisabledDay');
jest.dontMock('../components/Day');
jest.dontMock('../utils/inject-style');
jest.dontMock('../utils/date-helpers');
jest.dontMock('../config/i18n');
jest.dontMock('../style/date-picker');

import React from 'react';
import ReactDOM from 'react-dom';
import TestUtils from 'react-addons-test-utils';

// Babel would move an import in front of the jest.dontMock. That's why require
// is used instead of import.
const DatePicker = require('../components/DatePicker').default;
const injectStyle = require('../utils/inject-style');

describe('DatePicker', () => {
  it('should initialise props as expected', () => {
    const datePicker = TestUtils.renderIntoDocument(
      <DatePicker />
    );
    expect(datePicker.props.tabIndex).toBe(0);
    expect(datePicker.props['aria-label']).toBe('datepicker');
    expect(datePicker.props.disabled).toBe(false);
    expect(datePicker.props.readOnly).toBe(false);
    expect(datePicker.props.showOtherMonthDate).toBe(true);
  });

  it('should have undefined date value if value is not passed in props', () => {
    const datePicker = TestUtils.renderIntoDocument(
      <DatePicker />
    );
    expect(datePicker.state.selectedDate).toBeUndefined();
  });

  it('should change date when a day is focused and enter key is pressed', () => {
    const datePicker = TestUtils.renderIntoDocument(
      <DatePicker className="date_picker_wrapper"/>
    );

    expect(datePicker.state.selectedDate).toBeUndefined();
    const dayPickerWrapper = TestUtils.scryRenderedDOMComponentsWithClass(datePicker, 'date_picker_wrapper')[0];
    TestUtils.Simulate.focus(dayPickerWrapper);
    TestUtils.Simulate.keyDown(dayPickerWrapper, { key: 'Enter' });
    expect(datePicker.state.selectedDate).toBeGreaterThan(0);
  });

  describe('injecting styles', () => {
    beforeEach(() => {
      injectStyle.injectStyles = jest.genMockFunction();
    });

    it('should inject styles for hover, active & foucs', () => {
      TestUtils.renderIntoDocument(
        <DatePicker />
      );

      expect(injectStyle.injectStyles.mock.calls.length).toBe(1);
      const styles = injectStyle.injectStyles.mock.calls[0][0];
      expect(styles[0].id).toContain('prevMonthNav-style-id');
      expect(styles[1].id).toContain('nextMonthNav-style-id');
      expect(styles[2].id).toContain('wrapper-style-id');
    });
  });

  it('should select / deselect date when space key is pressed', () => {
    let dateSelected;
    const datePicker = TestUtils.renderIntoDocument(
      <DatePicker
        onUpdate={ (obj) => dateSelected = obj.value }
        className="date_picker_wrapper"
      />
    );

    expect(datePicker.state.selectedDate).toBeUndefined();

    // const day = TestUtils.scryRenderedDOMComponentsWithClass(datePicker, 'day_test')[8];
    const dayPickerWrapper = TestUtils.scryRenderedDOMComponentsWithClass(datePicker, 'date_picker_wrapper')[0];
    TestUtils.Simulate.focus(dayPickerWrapper);
    TestUtils.Simulate.keyDown(dayPickerWrapper, { key: ' ' });
    expect(datePicker.state.selectedDate).toBeGreaterThan(0);
    expect(dateSelected).toBeGreaterThan(0);

    // expect(dateSelected.getDay()).toBeGreaterThan(0);
    TestUtils.Simulate.keyDown(dayPickerWrapper, { key: ' ' });
    expect(datePicker.state.selectedDate).toBeUndefined();
    expect(dateSelected).toBeUndefined();
  });

  it('should not change date when a day is focused and enter key is pressed if component is disabled or readOnly',
    () => {
      const disabledDatePicker = TestUtils.renderIntoDocument(
        <DatePicker dayProps={{ className: 'day_test' }} disabled/>
      );
      expect(disabledDatePicker.state.selectedDate).toBeUndefined();
      let day = TestUtils.scryRenderedDOMComponentsWithClass(disabledDatePicker, 'day_test')[8];
      TestUtils.Simulate.focus(day);
      TestUtils.Simulate.keyDown(day, { key: 'Enter' });
      expect(disabledDatePicker.state.selectedDate).toBeUndefined();

      const readOnlyDatePicker = TestUtils.renderIntoDocument(
        <DatePicker dayProps={{ className: 'day_test' }} readOnly/>
      );
      expect(readOnlyDatePicker.state.selectedDate).toBeUndefined();
      day = TestUtils.scryRenderedDOMComponentsWithClass(readOnlyDatePicker, 'day_test')[8];
      TestUtils.Simulate.focus(day);
      TestUtils.Simulate.keyDown(day, { key: 'Enter' });
      expect(readOnlyDatePicker.state.selectedDate).toBeUndefined();
    });

  it('should change focusedDateKey on mouse down + up', () => {
    const datePicker = TestUtils.renderIntoDocument(
      <DatePicker dayProps={{ className: 'day_test' }}/>
    );

    expect(datePicker.state.focusedDateKey).toBeUndefined();
    const day = TestUtils.scryRenderedDOMComponentsWithClass(datePicker, 'day_test')[8];
    TestUtils.Simulate.mouseDown(day, { button: 0 });
    TestUtils.Simulate.mouseUp(day, { button: 0 });
    expect(datePicker.state.focusedDateKey).toBeDefined();
  });

  it('should reduce focusedDateKey by 1 when arrowLeft is pressed', () => {
    const datePicker = TestUtils.renderIntoDocument(
      <DatePicker className="date_picker"/>
    );

    expect(datePicker.state.focusedDateKey).toBeUndefined();
{'fraction_non_alphanumeric': 0.1434177333457909, 'fraction_numerical': 0.0041109969167523125, 'mean_word_length': 4.346653346653347, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6518323', 'n_tokens_mistral': 3175, 'n_tokens_neox': 3126, 'n_words': 536}