daniellmb / angular-test-patterns

A High-Quality Guide for Testing Angular 1.x
MIT License
867 stars 100 forks source link

Unit test a directive: add an isolated scope test #8

Closed yanivefraim closed 9 years ago

yanivefraim commented 9 years ago

I think that using an isolated scope is more common scenario than ng-model. I would add it to the scenario + not sure that ng-model should be mandatory. (I am willing to add a PR here!)

daniellmb commented 9 years ago

I totally agree, looking forward to reviewing your PR

yanivefraim commented 9 years ago

Hey, A small question regarding this feature - do you think I should leave the current directive with a mandatory ngModel, and add a new directive that uses isolated scope? If so, should I leave it in the save directive test-file? Another option is to add the isolated scope to the current directive and remove ngModel...

daniellmb commented 9 years ago

@yanivefraim hmmm, it may be useful to have both examples. With the addition of .isolateScope() in version v1.2.3 perhaps a new file would be the cleanest way to present the differences in testing (isolateDirective.md ?). What pattern are you going to suggest developers use for testing isolate scope? One pattern I've seen is moving logic such as adding a $watch scope into the directive controller so it can be more easily unit tested.

yanivefraim commented 9 years ago

@daniellmb ok, a new file (isolateDirective.md) sounds cool. One thing we have to take into consideration is how those files (such as 'controllerAs.md') will look like on the main readme.md page. Regarding a test pattern - I uses isolated scope test like this:

var isolated = element.isolateScope(); expect(isolated.item).toBeDefined();

Not sure what did you mean when said: "adding a $watch scope into the directive controller" (??)

daniellmb commented 9 years ago

@yanivefraim Probably more of a TDD OCD edge-case, but for times when you want to unit test that a directive is adding a $watch as expected: http://stackoverflow.com/questions/17371836/how-to-unit-test-isolated-scope-directive-in-angularjs

I agree we may need to rethink how we present the patterns with more than one variant. Maybe something like this: