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

Feature request: Stubbed ValidationController #63

Closed bulldetektor closed 6 years ago

bulldetektor commented 7 years ago

Unit testing ViewModels with ValidationRules are impossible if you're using WebPack or JSPM (and who isn't?). There's an closed, but IMHO un-resolved, issue for this over in the aurelia-validation repo (https://github.com/aurelia/validation/issues/377), but the only work-around that I've found to work is to disable ValidationRules when unit testing. But the validation of input is one of the core tenants to unit test, and we loose a lot of testability once we start using aurelia-validation.

Therefore; It would be really great if you could make an implementation of the ValidationController that could be used by unit tests and which honors the validation rules one has defined on the view model.

EisenbergEffect commented 7 years ago

@jdanyow Can you take a look into this request?

jdanyow commented 7 years ago

The unit tests for aurelia-validation use aurelia-testing. You should be able to copy one of them to get started.

https://github.com/aurelia/validation/blob/master/test/validate-binding-behavior.ts

bulldetektor commented 7 years ago

@jdanyow Thanks, but I've already tried to use the StageComponent etc. But I haven't got it running with mocha using jspm. Whatever I try I always end up with the "Did you forget to add ".plugin('aurelia-validation)" to your main.js?" error.

What I really want is to unit test my view model without depending on runtime initialization and loading of Aurelia. I want my validation rules to "just work" with as little plumbing and hazzle as possible...

sebandres commented 6 years ago

I have the exact same scenario/problem, I am trying to test my view model without having to spin up Aurelia (I just want to call the class itself) and I get the ".plugin('aurelia-validation)" to your main.js?" given validation rules seem to have been built to run only inside of Aurelia.

jereckley commented 6 years ago

To reiterate "Unit testing ViewModels with ValidationRules are impossible if you're using WebPack or JSPM"... the example link https://github.com/aurelia/validation/blob/master/test/validate-binding-behavior.ts doesn't seem to import ValidationRules. Kinda stuck not being able to test vm's/services importing ValidationRules. Thanks!

jereckley commented 6 years ago

As a quick follow up I got it all working... and quite nicely. It really just needed Aurelia's component testing setup as mentioned and the needed services mocked out. No problem with the ValidationRules anymore and @jdanyow your examples were most helpful and awesome. Thanks!

Alexander-Taran commented 6 years ago

Can be closed

TimVevida commented 6 years ago

@jereckley I keep struggeling to get aurelia-validation to work properly. In my case, the component test is a little more complicated, using a parent ViewModel which contains the validation which should be picked up in my component. Anyway, could you post a code sample of your working configuration?

jereckley commented 6 years ago

@TimVevida Sorry it took so long to respond... we(or a lot of us) lost power on the east coast for a bit. I also am working on a huge front end application and the tests are complex. I'm not sure posting them would help to much but if there is some way we can work on that together I'd be happy to. Honestly it's still super buggy and we kinda have put it(html state unit tests) on the back burner because the tests run so inconsistently. We are using Jest and it seems each "spin up" test needs its own test file but even then they pass/fail inconsistently. Let me know if you there is any other way I could help/collaborate...

TimVevida commented 6 years ago

Ah, it seems you have your own problems. Let me struggle with this a bit more.