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

Unable to test bindable property changes in a view model #69

Closed
suneelv opened this issue Jul 2, 2017 · 4 comments
Closed

Unable to test bindable property changes in a view model #69

suneelv opened this issue Jul 2, 2017 · 4 comments
Labels

Comments

@suneelv
Copy link

suneelv commented Jul 2, 2017

I'm submitting a bug report

  • Library Version:
    1.0.0-beta.3.0.1

Please tell us about your environment:

  • Operating System:
    OSX

  • Node Version:
    7.9.0

  • NPM Version:
    4.2.0
  • JSPM OR Webpack AND Version
    webpack 2.6.1
  • Browser:
    all | Chrome XX | Firefox XX | Edge XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView

  • Language:
    ESNext

Current behavior:

propertyChanged method is never called in a unit test. Here is a sample custom element view model

import {customElement, bindable} from 'aurelia-framework';

@customElement('welcome-element')
export class WelcomeElement {

  @bindable name;
  
  constructor() {

  }

  bind() {
    this.showName = `Hello ${this.name}`;
  }

  nameChanged() {
    this.showName = `Hello ${this.name}`;
  }
}

Here is my sample test case:

import {Container} from 'aurelia-dependency-injection';
import {WelcomeElement} from 'welcome-element';

describe('WelcomeComponent', () => {
  let vm;
  let container;

  beforeEach(() => {
    container = new Container();
    vm = container.get(WelcomeElement);
  });

  it('should change showName when name is changed', (done) => {
    vm.name = 'world';
    setTimeout(() => {
      expect(vm.showName).toEqual('Hello world');
      done();
    });
  });
});

After I set the name of the view model, the nameChanged method is not called and test fails.

Expected/desired behavior:

  • What is the expected behavior?

nameChanged method should be called when the bindable property name is changed.

  • What is the motivation / use case for changing the behavior?
@suneelv suneelv changed the title Unable to test bindable properties in a view model Unable to test bindable property changes in a view model Jul 2, 2017
@EisenbergEffect
Copy link
Contributor

You need to actually use the component testing library to test components. See docs here: http://aurelia.io/hub.html#/doc/article/aurelia/testing/latest/testing-components

@suneelv
Copy link
Author

suneelv commented Jul 3, 2017

I don't want to test the custom element's view. Is it not possible to just test a custom element's view model?
http://patrickwalters.net/unit-testing-your-es6-view-models-my-best-practices-for-aurelia/
I know the above article is old, but it seems like a reasonable way to test view models.

If that is not an option, then it would be great if we can do shallow rendering (#20).

@EisenbergEffect
Copy link
Contributor

Bindables require runtime infrastructure. That's why you have to use the testing framework. It has nothing to do with the view.

@suneelv
Copy link
Author

suneelv commented Jul 3, 2017

@EisenbergEffect thanks for the info. I will comment on the other referenced issue as that is the reason which made us try to test our custom elements in this way

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

No branches or pull requests

2 participants