Ember.js测试页会自动刷新

时间:2018-12-17 12:16:58

标签: javascript unit-testing ember.js

我正在尝试从此处进行示例集成测试:https://guides.emberjs.com/release/testing/testing-components/(测试操作)

我的问题是,由于某种原因,“测试”输出会不断自动刷新?

测试代码:

this.paginator.length=mock.length;

Component.js:

locationManager.showsBackgroundLocationIndicator = true

组件模板:

test('Can handle submit action', async function (assert) {
    /*
    * THIS TEST HAS PROBLEMS
    * THE PAGE CONSTANTLY REFRESHES FOR THIS TEST, NO IDEA WHY, NEED TO INVESTIGATE
    */
    assert.expect(1);

    // test double for the external action
    this.set('externalAction', (actual) => {
      const expected = {inputValue: 'test'};
      assert.deepEqual(actual, expected, 'submitted value is passed to external action');
    });

    await render(hbs`{{user-form inputValue="test" saveAction=(action externalAction)}}`);

    // click the button to submit the form
    await click('#submitButton');
  });

我认为这可能是因为模板中的表单一直在提交,但事实并非如此,因为它只应单击一次提交。任何帮助,不胜感激!

1 个答案:

答案 0 :(得分:3)

在@Lux的建议后写为评论;您需要执行以下操作来防止表单提交重新加载页面:

save(inputValue, event) {
  event.preventDefault()
  if (this.inputIsValid) {
    this.saveAction(inputValue); // pass action handling to route that uses component
  }
}

您收到event作为最后一个参数,并调用preventDefault告诉浏览器不要像通常那样处理事件。有关详细说明,请参见MDN