Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

moduleForComponent issue with {{link-to}} helpers #52

Closed
supabok opened this issue May 18, 2014 · 35 comments
Closed

moduleForComponent issue with {{link-to}} helpers #52

supabok opened this issue May 18, 2014 · 35 comments

Comments

@supabok
Copy link

supabok commented May 18, 2014

It seems when attempting to test components that contain a link-to helper, tests fail due to the absence of a router.

Here's a quick bin illustrating the issue:
http://jsbin.com/gapoqizo/3/edit

@balinterdi
Copy link
Contributor

I also got stuck on this, here is what I tried:

moduleForComponent('super-component', "Unit - Component - Super", {
  needs: [
    'location:none',
    'router:main',
  ],

  setup: function () {
    router.reopen({
      location: 'none'
    });
  },
  teardown: function () {
    (...)
  }
});

The error that I got is Attempting to register an unknown factory: location:none. I checked the Ember source and registration happens when the App is launched:

    container.register('location:none', NoneLocation);

Where NoneLocation comes from import NoneLocation from "ember-routing/location/none_location"

That explains why this works flawlessly in integration tests but I am not sure how could this be made to work in unit tests. (NoneLocation is not accessible on the top-level Ember object or otherwise, to my knowledge).

@validkeys
Copy link

Hey Guys,

Running into this exact same problem.

The default test gives me the router error

`import { test, moduleForComponent } from 'ember-qunit'`

moduleForComponent 'iteration-card', 'IterationCardComponent', {
  # specify the other units that are required for this test
  # needs: ['helper:link-to', 'router:main', 'location:auto']
}

test 'it renders', ->
  expect 2

  # creates the component instance
  component = @subject()
  equal component.state, 'preRender'

  # appends the component to the page
  @append()
  equal component.state, 'inDOM'

screen shot 2014-07-19 at 3 57 40 pm

So I add router:main and get

moduleForComponent 'iteration-card', 'IterationCardComponent', {
  # specify the other units that are required for this test
  needs: ['router:main']
}

test 'it renders', ->
  expect 2

screen shot 2014-07-19 at 3 58 56 pm

Any ideas on what to do here? @balinterdi have you gotten any further?

@balinterdi
Copy link
Contributor

@validkeys No, I bumped into the same problems you did and haven't found a way around them (it's either the "unknown factory: location:none" or the "auto is not a valid implementation" error.

@tomdale
Copy link
Member

tomdale commented Jul 22, 2014

We're discussing this now at Ember Out East. This is really important to fix.

@supabok
Copy link
Author

supabok commented Jul 23, 2014

👍
It'd be nice if we could stub out the router and then add a mock url that 'll let us test the component executes the correct action+url

@rwjblue
Copy link
Member

rwjblue commented Jul 23, 2014

I believe that the plan is to use some sort of URL service to ensure that we can still use link-to without the full router being setup (but clearly it won't actually work since you can navigate elsewhere without a router).

@dustinfarris
Copy link

FWIW, I am running into this with an ember-cli addon that has it's own initializer.

When I try to run integration tests on my main project, i get the unknown factory error.

@dustinfarris
Copy link

Sample app: https://github.com/dustinfarris/sample-app
Sample addon: https://github.com/dustinfarris/sample-addon

To reproduce:

git clone git@github.com:dustinfarris/sample-app
cd sample-app && npm i && bower i
npm link ember-cli
ember t

Output:

version: 0.0.39-master-b4c66bf446
Built project successfully. Stored in "/Users/dustin/Examples/base/sample-app/tmp/class-tests_dist-qXwLsFxe.tmp".
not ok 1 PhantomJS 1.9 - Acceptance: ShowEnvironment: visiting /
    ---
        message: >
            Setup failed on visiting /: Attempting to register an unknown factory: `sampleAddon:myEnvironment`
    ...
ok 2 PhantomJS 1.9 - JSHint - sample-app: sample-app/app.js should pass jshint
ok 3 PhantomJS 1.9 - JSHint - sample-app/components: sample-app/components/show-environment.js should pass jshint
ok 4 PhantomJS 1.9 - JSHint - sample-app/initializers: sample-app/initializers/sample-addon.js should pass jshint
ok 5 PhantomJS 1.9 - JSHint - sample-app: sample-app/router.js should pass jshint
ok 6 PhantomJS 1.9 - JSHint - sample-app/tests/acceptance: sample-app/tests/acceptance/show-environment-test.js should pass jshint
ok 7 PhantomJS 1.9 - JSHint - sample-app/tests/helpers: sample-app/tests/helpers/resolver.js should pass jshint
ok 8 PhantomJS 1.9 - JSHint - sample-app/tests/helpers: sample-app/tests/helpers/start-app.js should pass jshint
ok 9 PhantomJS 1.9 - JSHint - sample-app/tests: sample-app/tests/test-helper.js should pass jshint

1..9
# tests 9
# pass  8
# fail  1

@dustinfarris
Copy link

@rwjblue should I open a new issue for the above, or is it sufficiently related?

@chrisgame
Copy link

@supabok @tomdale @rwjblue has this been fixed?

@devinus
Copy link
Member

devinus commented Jun 2, 2015

This is still a problem. Has anybody figured out how to unit test a component with a link-to helper?

@code0100fun
Copy link

@rwjblue, seems like this issue may have cropped up again.

@devinus @chrisgame, I was able to get around it by adding this to my component test setup:

moduleForComponent('my-component', 'Unit | Component | my component', {
  unit: true,
  setup: function () {
    this.registry.register('service:-routing', Ember.Object.extend({
      availableRoutes: function() { return ['index']; },
      hasRoute: function(name) { return name === 'index'; },
      isActiveForRoute: function() { return true; },
      generateURL: function() { return "/"; }
    }));
  }
});

pulled from here:
emberjs/ember.js@7cff945#diff-f75efe2797ee3418f61c49b66cdc35b1R21

@artsyca
Copy link

artsyca commented Jun 16, 2015

For what it's worth, we have had some success with implementing a stubbed helper in our testing:

beforeSetup: ->
  Ember.Handlebars.registerHelper('link-to', (value, options) ->
    options.fn?() # this will just show the block

@artsyca
Copy link

artsyca commented Jun 16, 2015

@code0100fun That looks awesome!

@ZebraFlesh
Copy link

Just noting that is this still an issue with ember 1.13.0. The work around from @code0100fun works nicely though.

@sir-dunxalot
Copy link

Also seeing this issue with addon test suites running with Ember Try, which pulls ember-beta and ember-canary. My workaround is to allow the beta and canary jobs to fail without failing the whole build but it would be great to see a fix for this before the next Ember release or it will break currently passing test suites.

@rwjblue
Copy link
Member

rwjblue commented Jun 20, 2015

There is a PR in Ember that fixes the error, and it should make it to the beta branch (for 2.0.0).

@jrjohnson
Copy link

@rwjblue - I couldn't find the PR to add this comment to, but this seems to be a breaking change in 1.13 (at least it broke my existing component tests). The workaround above works, but still seems like a regression.

@tomdale
Copy link
Member

tomdale commented Jun 22, 2015

@jrjohnson I think we plan to do a few more 1.13 point releases. I wasn't sufficiently confident in the fix that I wanted to rush it into a point release, but if it lasts in beta/canary without any issues, I'm definitely not opposed to backporting it to a future 1.13 point release.

@mmahalwy
Copy link

Seems like @code0100fun is what works!

@YoranBrondsema
Copy link

@rwjblue @tomdale Is there any news on when it will be possible to test components (unit and/or integration) that have {{link-to}} helpers? I checked the release notes of the latest Ember.js releases and it doesn't seem that there's a merged PR yet that implements this. Thanks!

@code0100fun
Copy link

You can use component integration tests for components with {{link-to}}s now (verified in 1.13.3). The apps actual routing service does not seem to be available though so the a tags will have no hrefs.

@YoranBrondsema
Copy link

@code0100fun Sorry I wasn't clear, but testing the hrefs is indeed what I am looking for to do.

@code0100fun
Copy link

I came up with a helper that will setup your apps router in integration tests. See this test for an example usage.

@rwjblue any way to do this without using Ember.__loader?

Edit: @rwjblue simplified the helper immensely!

@rwjblue
Copy link
Member

rwjblue commented Sep 27, 2015

emberjax/cast#1

@YoranBrondsema
Copy link

@rwjblue @code0100fun: Perfect, this is just what I needed, thanks a lot!

@YoranBrondsema
Copy link

@code0100fun @rwjblue I updated our application to Ember.js 2.1.1 and I am now getting an error message when running a component integration test that uses setupRouter as defined in emberjax/cast#1.

error

It fails at this line, where instance is undefined.

We updated to Ember.js 2.2 to see if it persisted and the same error still appears.

Are you aware of any examples of using the router for component integration tests for Ember.js >= 2.1?

@trevorrjohn
Copy link

@YoranBrondsema did you ever find a solution to the didCreateRootView issue? I am using Ember 2.4.0 and seeing it when I try to use the router.

@YoranBrondsema
Copy link

@trevorrjohn Unfortunately no. We needed to move on so decided to drop testing links in component integration tests.

@robwebdev
Copy link

@YoranBrondsema @trevorrjohn I was able to get around the error by registering a stubbed application instance https://github.com/robwebdev/ember-cli-static-site/blob/b91075a92bc30038086fa7ae59961975c32ebcb9/tests/integration/components/link-to-test.js#L13

@samtsai
Copy link

samtsai commented Oct 7, 2016

The actual {{link-to}} test works but getting some other strange issues. I'm seeing this error, anyone experiencing this:

Assertion after the finalassert.asyncwas resolved@ 71 ms

Source:     
    at Object.ok (http://localhost:4200/assets/test-support.js:3802:8)
    at Class.exception (http://localhost:4200/assets/test-support.js:13707:47)
    at adapterDispatch (http://localhost:4200/assets/vendor.js:47237:13)
    at Object.dispatchError (http://localhost:4200/assets/vendor.js:26181:7)
    at onerrorDefault (http://localhost:4200/assets/vendor.js:39239:19)
    at Object.trigger (http://localhost:4200/assets/vendor.js:65452:9)
    at http://localhost:4200/assets/vendor.js:66336:26
    at invoke (http://localhost:4200/assets/vendor.js:10872:14)
    at Object.flush (http://localhost:4200/assets/vendor.js:10940:9)
    at Object.flush (http://localhost:4200/assets/vendor.js:11064:15)
    at Object.end (http://localhost:4200/assets/vendor.js:11134:23)
    at http://localhost:4200/assets/vendor.js:11700:16

Also another error along with it:
TypeError: Cannot read property 'setup' of undefined
Running Ember: 2.9.0-beta.5

@samtsai
Copy link

samtsai commented Oct 7, 2016

Looks like by running: startRouting application route hooks are being triggered. The errors are more related to my application... Looking into it further.

@archit
Copy link

archit commented Oct 6, 2017

ping, seeing this issue in ember 2.12.2 still, after setting up the setupRouter helper.

@rwjblue
Copy link
Member

rwjblue commented Oct 17, 2017

Sorry for letting this linger so long, this issue is quite out of date and likely the underlying problem has been fixed. Initial implementation of the new ember-qunit API (from emberjs/rfcs#232) has landed and will be included in 3.0.0 release soon.

Closing as I do not believe that this issue can be addressed in the current ember-qunit@2 API.

@rwjblue rwjblue closed this as completed Oct 17, 2017
@YoranBrondsema
Copy link

YoranBrondsema commented Nov 10, 2017

For people who would like a solution now, on Ember.js 2.16 I can run component integration tests that have {{link-to}} just fine. Furthermore, by stubbing didTransition on the -routing service, I can test the link after a click on it. E.g.

moduleForComponent(
  'component-with-link-to', '...', {
    beforeEach() {
      this.inject.service('-routing');
    }
  }
);

test(`the link goes to the 'abc' route`, async function(assert) {
  this.render(hbs`
    {{component-with-link-to}}
  `);

  const doTransition = this.stub(this.get('-routing'), 'doTransition');

  await click('a');

  assert.ok(doTransition.calledWith('abc'));
});

This example uses Sinon.js for stubbing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests