emberjs / ember-test-helpers

Test-framework-agnostic helpers for testing Ember.js applications
Apache License 2.0
187 stars 257 forks source link

fillIn or typeIn don't work with past hook action #510

Open villander opened 5 years ago

villander commented 5 years ago

on tests the value is forced and hook paste don't prevent a invalid value.

//input-mask.js
import TextField from '@ember/component/text-field';

export default TextField.extend({
  type: 'text',
  value: null,
  regex: null,

  onchange() { },

  keyPress(e) {
    return this.get('regex') ? new RegExp(this.get('regex')).test(String.fromCharCode(e.which)) : true;
  },

  paste(e) {
    let paste = e.originalEvent.target.value;
    return this.get('regex') ? new RegExp(this.get('regex')).test(paste) : true;
  },

  input() {
    this.get('onchange')(this.get('value'));
  },
});
//input-mask-test.js

import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render, fillIn, triggerEvent } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';

module('Integration | Component | input-mask', function(hooks) {
  setupRenderingTest(hooks);

  test('regex with invalid value', async function(assert) {
    await render(hbs`{{input-mask regex='[a-z]'}}`);
    await fillIn('input', '42');
    await triggerEvent('input', 'paste');
    assert.equal(this.element.querySelector('input').value, ''); // return 42
  });
});
rwjblue commented 5 years ago

I'm sorry, but I don't really understand what you are asking here. Can you describe what you expected to happen (and possibly why)? What did happen?

villander commented 5 years ago

sorry @rwjblue. I need tests this input with paste event, but when i use fillIn helper the value appears in the input, but using the application the value should not even be inserted into the input.

Basically I need to test the past event on my input.

ghost commented 5 years ago

@villander if you need to test the paste event, you can use triggerEvent:

const getDataSpy = sinon.spy();
await fillIn('input', 'hello world paste');
await triggerEvent('input', 'paste', {
    clipboardData: {
        getData: getDataSpy
    }
});