Skip to content

Unit Test Coverage on Angular Services and Providers #6

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

Merged
merged 5 commits into from
Jun 10, 2016

Conversation

kanhirun
Copy link
Contributor

@kanhirun kanhirun commented Jun 7, 2016

Summary

  • Provides full unit test coverage for all angular services—namely, zendeskWidgetSettings, and ZendeskWidgetProvider (4d95fcc) (See Figure 1 below)
  • Reorganizes test directory into testing layers, integration/ and unit/ (see Figure 2 below)

Figure 1. Unit Test Coverage Report

Missing 6.25% is due to Self-Executing Anonymous Function [LOC]

----------------------------------|----------|----------|----------|----------|----------------|
File                              |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
----------------------------------|----------|----------|----------|----------|----------------|
 angular-zendesk-widget/          |      100 |      100 |      100 |      100 |                |
  zendeskWidget.module.js         |      100 |      100 |      100 |      100 |                |
 angular-zendesk-widget/services/ |    92.86 |      100 |     87.5 |    92.86 |                |
  zendeskWidgetProvider.js        |    90.91 |      100 |    85.71 |    90.91 |             29 |
  zendeskWidgetSettings.js        |      100 |      100 |      100 |      100 |                |
----------------------------------|----------|----------|----------|----------|----------------|
All files                         |    93.75 |      100 |    88.89 |    93.75 |                |
----------------------------------|----------|----------|----------|----------|----------------|

Figure 2. The test/ is reorganized to expose two distinct layers

test
├── integration/               <— tests angular's bootstrap phase
│   └── zendeskWidgetSpec.js
└── unit/                      <— tests angular's configuration, and run phase
    └── services/
        ├── zendeskWidgetProviderSpec.js
        └── zendeskWidgetSettingsSpec.js

@kanhirun kanhirun assigned kanhirun and bsouthga and unassigned kanhirun Jun 7, 2016
@kanhirun
Copy link
Contributor Author

kanhirun commented Jun 7, 2016

@bsouthga Ship is ready for sail. Ready for review.

@bsouthga
Copy link
Contributor

bsouthga commented Jun 7, 2016

@kanhirun this looks great! thanks for adding code coverage + a few more tests. 👏

There is only one issue I see to take a look at before merging this in -- when I use this branch with our current app, it I get the

angular-zendesk-widget.js:15 Uncaught Error: Missing accountUrl. Please set in app config via ZendeskWidgetProvider

Error, as it looks like the new order of the concatenated files causes the run statement to occur before the config registration.

Maybe take a look at that? It also might make sense to add a real index.html test file with an example of injecting the plugin to a fake angular module.

@kanhirun kanhirun assigned kanhirun and unassigned bsouthga Jun 8, 2016
@kanhirun
Copy link
Contributor Author

kanhirun commented Jun 8, 2016

@bsouthga I'm not sure what the proper naming is for angular files. Could I request for a review on 1bb64e1a?

@bsouthga
Copy link
Contributor

bsouthga commented Jun 8, 2016

@kanhirun the filenames look good to me, don't yet have a nailed down standard.

kanhirun added 5 commits June 8, 2016 22:37
* Re-generates build artifact
* Moves angular components into separate files. This improves the testability of
  angular services such that the configuration, and run phase can be tested independently
This provides better test feedback on readability, and allows using
Karma's --grep option easily.
@kanhirun kanhirun assigned bsouthga and unassigned kanhirun Jun 9, 2016
@kanhirun
Copy link
Contributor Author

kanhirun commented Jun 9, 2016

@bsouthga I made changes based on your feedback and tested it end-to-end with the angular-seed app. Thanks again for bringing this to my attention.

The issue wasn't just that the run statement occurred before config, but that both zendeskWidgetSettings and ZendeskWidgetProvider share settings in common, and it needs to remain shared even when the pieces separate into their own files—and not with their own copy of settings. The task became, how can these two components be separated into their own files while still sharing the same data?

Ultimately, I decided to just merge the two back into the same file. (After all, my testing requirement was just to keep the run statement by itself.)

If you want to take a different approach, I'll lean on your expertise here.

Here are some other solutions:

  1. Define a global variable which both components have access to. 👎
  2. Have the two components pass data in the "angular" way. I looked into this, but it seems to be complicated by several details that I will omit here.
  3. Finally, we can probably configure concatenation to wrap all the files into one self-executing anonymous function. But here, we would break encapsulation across components.

Either way, this is ready for another review (and to be re-tested on your end)

@bsouthga
Copy link
Contributor

bsouthga commented Jun 9, 2016

Awesome! looks good to me -- I think keeping the two in the same file is fine for now. If we did want to move them to separate files, we could try injecting the settings into the provider like this:

  angular.module('zendeskWidget')
    .provider('ZendeskWidget', ['zendeskWidgetSettings', function(settings) {
     //... do stuff
    });

Overall great work 👍 -- appreciate your thoroughness and desire for implementing refactoring best practices!

@kanhirun
Copy link
Contributor Author

kanhirun commented Jun 10, 2016

@bsouthga Great! I'll leave it to you to press the big ✅ button then.

EDIT: To my understanding, zendeskWidgetSettings can't exist as a dependency because angular loads providers first before loading its services (i.e. Value, Factory). Indeed if you tried doing so, you'll get Unknown provider: zendeskWidgetSettingsProvider.

@bsouthga bsouthga merged commit f6d271e into master Jun 10, 2016
@bsouthga
Copy link
Contributor

Your totally right, my mistake -- I think keeping the two in the same file is fine for now. Merged!

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

Successfully merging this pull request may close these issues.

2 participants