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

boundTo is ignored and class default values are used to render view #77

Closed sebandres closed 6 years ago

sebandres commented 6 years ago

I'm submitting a bug report

Please tell us about your environment:

Current behavior: Rendered component ignores a new bindingContext when using boundTo.

Expected/desired behavior: boundTo replaces the default values on the class and aurelia renders an output that matches the new boundTo binding context provided in order to simulate different scenarios for the same component.

Steps to reproduce

  1. git clone https://github.com/sebandres/skeleton-navigation.git aurelia-skeleton-navigation
  2. cd aurelia-skeleton-navigation/skeleton-typescript-webpack/
  3. npm i
  4. npm run test

I have forked the skeleton-navigation repo and added a boundTo call in welcome.spec.test on the skeleton-typescript-webpack project to prove the point. After the change both the snapshot test as well as the should render first name tests still pass when they should fail based on the different bindingContext.

tomtomau commented 6 years ago

It is my understanding that this is not a bug, but rather a simple misunderstanding of the examples :)

boundTo will simply create scoped data for use in the view.

By that I mean, the view you're using is

<welcome></welcome>

But if you changed to:

Hello ${firstName}, how are you?
<welcome></welcome>

Then this will render "Hello Peter, how are you?".

When the templating goes through, it sees ${firstName} and can replace this with a value because you've set a boundTo object with the same key.

But now you need to get it into your component!


In your welcome case, there are four properties on the Welcome component, but the framework doesn't make these bindable by default. You need to add the @bindable decoration to tell the framework to support it.

See here for an example usage for what you're trying to achieve

Likewise, be aware that you're not strictly testing just the component - you're actually testing a view, by using inView - that's fine, but that means you'll need to add first-name.bind etc to this view.

Also, for what its worth, technically in Aurelia, this Welcome object can be used as a component, but be aware that in the example in this repo, it's actually being used as a view model for a route, which is why it didn't have the @bindable() decoration.


Welcome to Aurelia :) (and Brisbane!)

sebandres commented 6 years ago

Hi @tomtomau!

Indeed your example does show how to use binding properly, I guess a more complex example might be in order for the docs to help newcomers to the framework :D

My goal was to test the component as whole (View and Code) to simulate different renderings of different inputs.

I did try something similar to what you posted unfortunately I must have not gotten the right combination of inView, bindable decorators and boundTo to make it work as I expected.

Thanks again and looking forward to doing a lot more Aurelia in the future!