aurelia / testing

Simplifies the testing of UI components by providing an elegant, fluent interface for arranging test setups along with a number of runtime debug/test helpers.
MIT License
40 stars 27 forks source link

Testing a routed component #37

Closed manuel-guilbault closed 5 years ago

manuel-guilbault commented 8 years ago

At the moment, is there a way to test a routed component? I tried the following:

component = new ComponentTester()
  .inView('<router-view></router-view>')
  .boundTo({
    configureRouter: config => {
      config.map([
        { route: '', moduleId: 'my-routed-component' }
      ]);
    }
  });

This throws an error because no viewModel is set on the container (the router's _findViewModel method returns undefined).

As far as I can tell, the only way to test a routed component is with end-to-end tests right now. Is there any plan to support this kind of feature?

EisenbergEffect commented 8 years ago

There is now :)

manuel-guilbault commented 8 years ago

Awesome :)

EisenbergEffect commented 8 years ago

@manuel-guilbault Can you describe to us what you would like to be able to do in such a test?

manuel-guilbault commented 8 years ago

Here's the features I would need:

Here's a typical test case I would write:

import {StageRoute} from 'aurelia-testing';
import {bootstrap} from 'aurelia-bootstrap';

describe('the list component', () => {
  let component;

  beforeEach(() => {
    component = StageRoute.component('list')
      .withResources('custom-element-needed-by-list')
      .withRouteParams({ /* Some route parameters passed to activate */ })
      .bootstrap(aurelia => {
        aurelia.use.standardConfiguration();
        //Here I would register an HttpClient mock or something similar in the DI container
      });
  });

  afterEach(() => {
    component.dispose();
  });

  it('should load data upon activation', done => {
    component.create(bootstrap)
      .then(() => {
        let elements = document.querySelectorAll('.container .item');
        // Here I would assert that the items were properly rendered
      })
      .then(done);
  });
});
massimocode commented 7 years ago

What the latest on this issue? We can't seem to find a way to call activate in our tests.

Edit: Oops, I found how to do it. component.viewModel.activate()

Alexander-Taran commented 5 years ago

@EisenbergEffect can be closed?

tomtomau commented 3 years ago

I've tried to roll our own abstractions here to test routed components but ran into the same issue describe above

(the router's _findViewModel method returns undefined)