<commit_before>import { module, test } from 'qunit';
import { typeIn, setupContext, teardownContext } from '@ember/test-helpers';
import { buildInstrumentedElement } from '../../helpers/events';
import { isIE11 } from '../../helpers/browser-detect';
import hasEmberVersion from '@ember/test-helpers/has-ember-version';

/*
 * Event order based on https://jsbin.com/zitazuxabe/edit?html,js,console,output
 */

let expectedEvents = [
  'focus',
  'focusin',
  'keydown',
  'keypress',
  'input',
  'keyup',
  'keydown',
  'keypress',
  'input',
  'keyup',
  'keydown',
  'keypress',
  'input',
  'keyup',
  'change',
];

if (isIE11) {
  expectedEvents = [
    'focusin',
    'keydown',
    'keypress',
    'keyup',
    'keydown',
    'keypress',
    'keyup',
    'keydown',
    'keypress',
    'keyup',
    'input',
    'change',
    'focus',
  ];
}

module('DOM Helper: typeIn', function(hooks) {
  if (!hasEmberVersion(2, 4)) {
    return;
  }

  let context, element;

  hooks.beforeEach(function() {
    context = {};
  });

  hooks.afterEach(async function() {
    element.setAttribute('data-skip-steps', true);

    if (element) {
      element.parentNode.removeChild(element);
    }
    if (context.owner) {
      await teardownContext(context);
    }

    document.getElementById('ember-testing').innerHTML = '';
  });

  test('filling in an input', async function(assert) {
    element = buildInstrumentedElement('input');
    await typeIn(element, 'foo');

    assert.verifySteps(expectedEvents);
    assert.strictEqual(document.activeElement, element, 'activeElement updated');
    assert.equal(element.value, 'foo');
  });

  test('it triggers key events with correct arguments', async function(assert) {
    element = buildInstrumentedElement('input', ['key', 'shiftKey']);
    await typeIn(element, 'F o');

    let chars = ['F', ' ', 'o'];
    let shiftKeys = [true, false, false];
    let expectedEventsWithArguments = expectedEvents.map(eventName => {
      // Only key events get the key arguments
      if (!['keydown', 'keypress', 'keyup'].includes(eventName)) {
        return `${eventName} undefined undefined`;
      }
      // After each keyup, the next character comes up
      let char = eventName === 'keyup' ? chars.shift() : chars[0];
      let shiftKey = eventName === 'keyup' ? shiftKeys.shift() : shiftKeys[0];

      return `${eventName} ${char.toUpperCase()} ${shiftKey}`;
    });

    assert.verifySteps(expectedEventsWithArguments);
  });

  test('filling in an input with a delay', async function(assert) {
    element = buildInstrumentedElement('input');
    await typeIn(element, 'foo', { delay: 150 });

    assert.verifySteps(expectedEvents);
    assert.strictEqual(document.activeElement, element, 'activeElement updated');
    assert.equal(element.value, 'foo');
  });

  test('filling in a textarea', async function(assert) {
    element = buildInstrumentedElement('textarea');
    await typeIn(element, 'foo');

    assert.verifySteps(expectedEvents);
    assert.strictEqual(document.activeElement, element, 'activeElement updated');
    assert.equal(element.value, 'foo');
  });

  test('filling in a non-fillable element', async function(assert) {
    element = buildInstrumentedElement('div');

    await setupContext(context);
    assert.rejects(typeIn(`#${element.id}`, 'foo'), /`typeIn` is only usable on form controls/);
  });

  test('rejects if selector is not found', async function(assert) {
    element = buildInstrumentedElement('div');

    await setupContext(context);

    assert.rejects(
      typeIn(`#foo-bar-baz-not-here-ever-bye-bye`, 'foo'),
      /Element not found when calling `typeIn\('#foo-bar-baz-not-here-ever-bye-bye'\)`/
    );
  });

  test('rejects if text to fill in is not provided', async function(assert) {
    element = buildInstrumentedElement('input');

    assert.rejects(typeIn(element), /Must provide `text` when calling `typeIn`/);
  });

  test('does not run sync', async function(assert) {
    element = buildInstrumentedElement('input');

    let promise = typeIn(element, 'foo');

    assert.verifySteps([]);

    await promise;

    assert.verifySteps(expectedEvents);
    assert.strictEqual(document.activeElement, element, 'activeElement updated');
    assert.equal(element.value, 'foo');
  });
});<commit_msg>added failing test for typeIn not waiting for other promises<commit_after>import { module, test } from 'qunit';
import { typeIn, setupContext, teardownContext } from '@ember/test-helpers';
import { buildInstrumentedElement } from '../../helpers/events';
import { isIE11 } from '../../helpers/browser-detect';
import { debounce } from '@ember/runloop';
import { Promise } from 'rsvp';
import hasEmberVersion from '@ember/test-helpers/has-ember-version';

/*
 * Event order based on https://jsbin.com/zitazuxabe/edit?html,js,console,output
 */

let expectedEvents = [
  'focus',
  'focusin',
  'keydown',
  'keypress',
  'input',
  'keyup',
  'keydown',
  'keypress',
  'input',
  'keyup',
  'keydown',
  'keypress',
  'input',
  'keyup',
  'change',
];

if (isIE11) {
  expectedEvents = [
    'focusin',
    'keydown',
    'keypress',
    'keyup',
    'keydown',
    'keypress',
    'keyup',
    'keydown',
    'keypress',
    'keyup',
    'input',
    'change',
    'focus',
  ];
}

module('DOM Helper: typeIn', function(hooks) {
  if (!hasEmberVersion(2, 4)) {
    return;
  }

  let context, element;

  hooks.beforeEach(function() {
    context = {};
  });

  hooks.afterEach(async function() {
    element.setAttribute('data-skip-steps', true);

    if (element) {
      element.parentNode.removeChild(element);
    }
    if (context.owner) {
      await teardownContext(context);
    }

    document.getElementById('ember-testing').innerHTML = '';
  });

  test('filling in an input', async function(assert) {
    element = buildInstrumentedElement('input');
    await typeIn(element, 'foo');

    assert.verifySteps(expectedEvents);
    assert.strictEqual(document.activeElement, element, 'activeElement updated');
    assert.equal(element.value, 'foo');
  });

  test('it triggers key events with correct arguments', async function(assert) {
    element = buildInstrumentedElement('input', ['key', 'shiftKey']);
    await typeIn(element, 'F o');

    let chars = ['F', ' ', 'o'];
    let shiftKeys = [true, false, false];
    let expectedEventsWithArguments = expectedEvents.map(eventName => {
      // Only key events get the key arguments
      if (!['keydown', 'keypress', 'keyup'].includes(eventName)) {
        return `${eventName} undefined undefined`;
      }
      // After each keyup, the next character comes up
      let char = eventName === 'keyup' ? chars.shift() : chars[0];
      let shiftKey = eventName === 'keyup' ? shiftKeys.shift() : shiftKeys[0];

      return `${eventName} ${char.toUpperCase()} ${shiftKey}`;
    });

    assert.verifySteps(expectedEventsWithArguments);
  });

  test('filling in an input with a delay', async function(assert) {
    element = buildInstrumentedElement('input');
    await typeIn(element, 'foo', { delay: 150 });

    assert.verifySteps(expectedEvents);
    assert.strictEqual(document.activeElement, element, 'activeElement updated');
    assert.equal(element.value, 'foo');
  });

  test('filling in a textarea', async function(assert) {
    element = buildInstrumentedElement('textarea');
    await typeIn(element, 'foo');

    assert.verifySteps(expectedEvents);
    assert.strictEqual(document.activeElement, element, 'activeElement updated');
    assert.equal(element.value, 'foo');
  });

  test('filling in a non-fillable element', async function(assert) {
    element = buildInstrumentedElement('div');

    await setupContext(context);
    assert.rejects(typeIn(`#${element.id}`, 'foo'), /`typeIn` is only usable on form controls/);
  });

  test('rejects if selector is not found', async function(assert) {
    element = buildInstrumentedElement('div');

    await setupContext(context);

    assert.rejects(
      typeIn(`#foo-bar-baz-not-here-ever-bye-bye`, 'foo'),
      /Element not found when calling `typeIn\('#foo-bar-baz-not-here-ever-bye-bye'\)`/
    );
  });

  test('rejects if text to fill in is not provided', async function(assert) {
    element = buildInstrumentedElement('input');

    assert.rejects(typeIn(element), /Must provide `text` when calling `typeIn`/);
  });

  test('does not run sync', async function(assert) {
    element = buildInstrumentedElement('input');

    let promise = typeIn(element, 'foo');

    assert.verifySteps([]);

    await promise;

    assert.verifySteps(expectedEvents);
    assert.strictEqual(document.activeElement, element, 'activeElement updated');
    assert.equal(element.value, 'foo');
  });

  test('does not wait for other promises to settle', async function(assert) {
    element = buildInstrumentedElement('input');

    let runcount = 0;
    let onInput = function() {
      return Promise.resolve().then(() => runcount++);
    };

    element.oninput = function() {
      // debounce 2 seconds for easy visualization in test
      debounce(onInput, 2000);
    };

    await typeIn(element, 'foo');

    assert.verifySteps(expectedEvents);
    assert.equal(runcount, 1, 'debounced function only called once');
  });
});
{'fraction_non_alphanumeric': 0.15703639701911654, 'fraction_numerical': 0.003132087698455557, 'mean_word_length': 3.751154438173422, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 2, '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': '101036', 'n_tokens_mistral': 2888, 'n_tokens_neox': 2832, 'n_words': 655}