Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

docs : update unit-testing-component-controllers #15974

Closed
wants to merge 1 commit into from

Conversation

musclor
Copy link
Contributor

@musclor musclor commented May 8, 2017

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)

docs update

What is the current behavior? (You can also link to an open issue here)

#15968

What is the new behavior (if this is a feature change)?

Removes a useless spec.

Does this PR introduce a breaking change?

No

Please check if the PR fulfills these requirements

Other information:

@frederikprijck
Copy link
Contributor

frederikprijck commented May 9, 2017

@musclor Just a few more remarks with regards to the comment you left on your previous PR:

Hello @frederikprijck

Finally I did not put any example because :

  • I believe this part of the document is to demonstrate how to test a component controller easily with $componentController.
  • I wanted to provide the same spec as you but it will fail because the heroDetail component needs an external template...
  • I think testing a component and bindings requires a dedicated chapter.
    I suggest to complete this page https://docs.angularjs.org/guide/unit-testing if someone wants to add more examples.

I agree with you that the warning regarding $componentController should not be there. As far as I am concerned removing the useless spec is the most important for now.

Thank you for your review and comments :)

  • I think the components page should explain how to test a component (being $componentController and $compile), or shouldn't explain testing at all but refer to a proper section in the docs regarding testing a component.
  • I'm not sure what you mean. Having a templateUrl doesn't mean we can't use the above test (even tho we might need to modify something to ensure angularjs can find the templateUrl, the test in intself doesn't have to be changed)
  • I'd say a small, dedicating testing section in the components section isn't bad either to start with?

Btw, any reason you closed the previous PR and created a new one? There's no need for that, you can simply update existing PR's by pushing new commits to your branch on which you created the PR!

gkalpak pushed a commit to gkalpak/angular.js that referenced this pull request May 9, 2017
gkalpak pushed a commit that referenced this pull request May 9, 2017
@gkalpak gkalpak closed this in 2f3bd89 May 9, 2017
@musclor
Copy link
Contributor Author

musclor commented May 9, 2017

Hi @frederikprijck

As per the section 'Testing Directives With External Templates' of https://docs.angularjs.org/guide/unit-testing : if the test directory hierarchy differs from the application's, the test with $compile will fail because of an unexpected GET to retrieve the template from the server.

I am sorry I closed the previous PR the reason is I only used the link on 'Improve this Doc' of Angular's website during the whole process. Thank you for your advice I will try to do like that next time.

Kind regards

@frederikprijck
Copy link
Contributor

frederikprijck commented May 9, 2017

@musclor Afaik this does not mean you can't test it. It even explains what you need to do in order to ensure this is not going to be a problem. It doesn't mention using $compile will fail.

If your directive uses templateUrl, consider using karma-ng-html2js-preprocessor to pre-compile HTML templates and thus avoid having to load them over HTTP during test execution. Otherwise you may run into issues if the test directory hierarchy differs from the application's.

Anyhow, doesn't matter now, the PR is merged 😛

@musclor
Copy link
Contributor Author

musclor commented May 9, 2017

You are refering to a sub section of 'Testing Directives' which uses $compile. Furthermore the directive example uses template instead of templateUrl so there is no problem there.

The fact that the PR is merged does not mean we cannot do further improvements to the documentation ! ;p

Have a nice day

@frederikprijck
Copy link
Contributor

frederikprijck commented May 9, 2017

@musclor I'm not sure what you're saying I'm refering to, but this is what I'm talking about: https://plnkr.co/edit/JUbGZuFZG2FPnNK18Kal?p=preview

The tests are green. Even tho I wouldn't use the $templateCache approach but use something like karma-ng-html2js-preprocessor (but that part doesn't need to be in the docs anyway), the test itself doesn't change when switching between templateUrl and template. As you can see I tested two components on the identical way (which is the same test as mentioned in the previous PR), one of those components is using templateUrl whereas the other is not.

They also both fail if the binding is removed from the corresponding component.

@musclor
Copy link
Contributor Author

musclor commented May 9, 2017

@frederikprijck It's because you are using $templateCache (same as karma-ng-html2js-preprocessor).

If you comment it the templateUrl test will fail.

@frederikprijck
Copy link
Contributor

frederikprijck commented May 9, 2017

@musclor I'm totally confused about what you're trying to point out.

I know it will fail when you comment it out but if you want to have tests with templateUrl, you need to use any of those. Commenting that out or not using karma-ng-html2js-preprocessor is not how you need to write tests for components using a templateUrl.

Afaik you're initial problem with the unit test in the docs was:

  • it does not fail when removing the component binding. (and it was a useless test aswell)

Afaict, this is solved with the tests in the plunkr. Hence my proposal to add it as a test instead of the removed test. (as it does what you where saying you wanted the test to be doing).

Anyway ... this is getting to chatty, feel free to ping me over at https://gitter.im/angular/angular.js to discuss this further.

@musclor
Copy link
Contributor Author

musclor commented May 9, 2017

Nothing more to add @frederikprijck

If you want to have tests with templateUrl, you need to use any of those.

Exactly what I tried to say from the begining actually :)

I just think for the sake of simplicity it is better for the documentation to use a simplier component with template rather than reuse the heroDetail component (which requires an additional service as we pointed out) but it is only a personal preference.

Thank you for your time and explainations (I have learnt a few new things thanks to you :)

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

Successfully merging this pull request may close these issues.

3 participants