Ember Component Integration Tests: `link-to` href empty

风流意气都作罢 提交于 2019-12-05 01:37:06

Turns out you can just look up the router and tell it to start routing in your test setup and it will work. From this commit from @rwjblue:

// tests/helpers/setup-router.js

export default function({ container }) {
  const router = container.lookup('router:main');
  router.startRouting(true);
}


// tests/integration/components/my-component-test.js

import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';
import setupRouter from '../../helpers/setup-router';

moduleForComponent('my-component', 'Integration | Component | my component', {
  integration: true,
  setup() {
    setupRouter(this);
  }
});

If you just want to check the href is ok, it could be better use setupRouter instead of startRouting. startRouting tries to handle the initial transition to the initial URL, and that could be problematic.

// tests/helpers/setup-router.js
export default function({ container }) {
const router = container.lookup('router:main');
   router.setupRouter();
}

https://github.com/emberjs/ember.js/blob/d487061228a966d8aac6fa94a8d69abfc3f1f257/packages/ember-routing/lib/system/router.js#L202

Here's how to do it in Ember > 3

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

module('Integration | Component | my-component', function (hooks) {
  setupRenderingTest(hooks);

  test('it has a link', async function (assert) {
    this.owner.lookup('router:main').setupRouter();
    await render(hbs`{{my-component}}`);
    assert.equal(this.element.querySelector('a').getAttribute('href'), 'some-url');
  });
});

What version of Ember are you using? I remember seeing this previously and now it seems to work in my app (though I'm using 1.13.8).

Seems to be adding the href's in my application, and based on this computed property it should be bound to the view if the tagName is "a".

Other than upgrading Ember, it's probably best to create an ember-fiddle or repro and file a bug if it persists.

You may also want to take a look at https://github.com/intercom/ember-href-to.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!