import {
  describe,
  expect,
  it
} from 'vitest';

import { shouldSelectOnFocus, shouldSubmitOnEnter } from './form-field-focus.rules';

describe('shouldSelectOnFocus', () => {
  it('selects when a text input has a value', () => {
    expect(shouldSelectOnFocus({ type: 'text',
      value: 'alice' })).toBe(true);
  });

  it('skips empty values', () => {
    expect(shouldSelectOnFocus({ type: 'text',
      value: '' })).toBe(false);
  });

  it('skips password fields by default', () => {
    expect(shouldSelectOnFocus({ type: 'password',
      value: 'secret' })).toBe(false);
  });

  it('can select password fields when skipPassword is false', () => {
    expect(shouldSelectOnFocus({ type: 'password',
      value: 'secret' }, false)).toBe(true);
  });
});

describe('shouldSubmitOnEnter', () => {
  it('submits from text inputs', () => {
    expect(shouldSubmitOnEnter({
      key: 'Enter',
      isComposing: false,
      target: { tagName: 'INPUT',
        type: 'text' }
    })).toBe(true);
  });

  it('does not submit from textareas', () => {
    expect(shouldSubmitOnEnter({
      key: 'Enter',
      isComposing: false,
      target: { tagName: 'TEXTAREA' }
    })).toBe(false);
  });

  it('does not submit from selects', () => {
    expect(shouldSubmitOnEnter({
      key: 'Enter',
      isComposing: false,
      target: { tagName: 'SELECT' }
    })).toBe(false);
  });

  it('ignores IME composition', () => {
    expect(shouldSubmitOnEnter({
      key: 'Enter',
      isComposing: true,
      target: { tagName: 'INPUT',
        type: 'text' }
    })).toBe(false);
  });
});
