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

Ahsan/removal underscore string from commerce js #12401

Merged

Conversation

ahsan-ul-haq
Copy link
Contributor

@ahsan-ul-haq ahsan-ul-haq commented May 9, 2016

ECOM-4281
@awais786 @zubair-arbi @tasawernawaz @waheedahmed

  • Add js test for analytics event Completed Order on LMS checkout receipt page.
  • Remove underscore.string dependency and use global string_utils methods.

Initial PR (Merged): https://github.com/edx/edx-platform/pull/12238
Reverted PR (falkytest): https://github.com/edx/edx-platform/pull/12273
Related PR (Underscore string changes): https://github.com/edx/edx-platform/pull/11631

@ahsan-ul-haq
Copy link
Contributor Author

jenkins run a11y

@ahsan-ul-haq
Copy link
Contributor Author

jenkins run python
jenkins run bokchoy

@ahsan-ul-haq
Copy link
Contributor Author

jenkins run bokchoy

@ahsan-ul-haq
Copy link
Contributor Author

@andy-armstrong @bjacobel @mattdrayer I have fixed the JS issue regarding _sprintf which we missed previously for non verified course receipts. @mattdrayer can you please check this on your sandbox.

@andy-armstrong
Copy link
Contributor

@ahsan-ul-haq Thanks for tackling this again, and for adding a regression test. There seems to be some issue where the test doesn't get full code coverage:

https://build.testeng.edx.org/job/edx-platform-js-pr/18172/Diff_Coverage_Report/

Can you investigate whether this is because the test isn't actually running, or if there's an issue with the code coverage itself. I'd expect to see the spec file get 100% coverage, and then the covered file should also get substantially more than 16.7%

@ahsan-ul-haq
Copy link
Contributor Author

@andy-armstrong I have made the changes and now spec file got 100% of coverage and covered file got 33.3% coverage.

@rlucioni
Copy link
Contributor

@ahsan-ul-haq you may want to hold off on merging this until https://github.com/edx/edx-platform/pull/12413 lands. The release branch contains a revert of the original commit. If you were to merge this before the revert lands in master, it might revert changes in this PR. @andy-armstrong and I learned about this the hard way a few releases back.

<section class="wrapper carousel">
<div id="receipt-container" class="pay-and-verify hidden" data-is-payment-complete='True'
data-platform-name='edx-platform' data-verified='True' data-username='user-1'>
<h2>${_("Loading Order Data...")}</h2>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: the fixture is intended to be rendered HTML, so it shouldn't include Python in it. It won't hurt as long as it is valid HTML, but it is a little confusing.

@ahsan-ul-haq
Copy link
Contributor Author

@rlucioni thanks for the heads up on that I would keep a close look on release to get merged into master before moving forward with this.

"currency": "USD",
"total_excl_tax": "10.00"
};
view = new ReceiptView({el: $('#receipt-container')});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a subtle issue where events sometimes don't behave as you'd expect when you render views in the beforeEach method. This has been the cause of a number of flaky Jasmine tests. I'd convert this into a createTestView function, and then call it from your test to create a view.

I also find that it makes the test easier to read. I like tests to be of the form:

  • do a thing
  • verify the result
  • do a thing
  • verify the result

IMO having too much logic in the beforeEach makes it harder to read the test by itself without having to fully understand what the setup code is doing.

@andy-armstrong
Copy link
Contributor

@ahsan-ul-haq Thanks for solving the issue with the spec coverage. Can you also get your changes to 100% coverage (this is just for lines you changed, not for the whole file):

https://build.testeng.edx.org/job/edx-platform-js-pr/18244/Diff_Coverage_Report/

You just need tests for getReceiptData and getProviderData which is a good idea anyway just to be sure that the interpolation is working as expected.

@andy-armstrong
Copy link
Contributor

@ahsan-ul-haq This is looking great. I have a few more comments about the tests, so let me know when those are addressed.

@ahsan-ul-haq
Copy link
Contributor Author

jenkins run js

@ahsan-ul-haq ahsan-ul-haq force-pushed the ahsan/removal-underscore-string-from-commerce-js branch from 36a15eb to 7a0e249 Compare May 11, 2016 15:52
return new ReceiptView({el: $('#receipt-container')});
};

var loadReceiptFixture = function(){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add space between () and {, like var loadReceiptFixture = function() {
Also add this var at top of this method.

@ahsan-ul-haq
Copy link
Contributor Author

@andy-armstrong I have added some new tests and although tests passes I am getting following error afterward

Firefox 28.0.0 (Ubuntu 0.0.0) ERROR
  TypeError: text is null
  at /edx/app/edxapp/edx-platform/lms/static/common/js/vendor/underscore.js:1429
Firefox 28.0.0 (Ubuntu 0.0.0) ERROR
  TypeError: text is null

Do you have any idea about this?

@zubair-arbi
Copy link
Contributor

once tests fixed and build passes 👍

@ahsan-ul-haq
Copy link
Contributor Author

jenkins run bokchoy


});

it("test render receipt", function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: the test name should read like a sentence starting with it, so something like:

it('renders a receipt correctly', function() {

@andy-armstrong
Copy link
Contributor

@ahsan-ul-haq That line of Underscore.js appears to be in the function _.template, so it seems that somewhere it is being rendered with a null template. I recommend putting a breakpoint on that line and then running the tests inside the browser (if you haven't done that before, this is described here: https://github.com/edx/edx-platform/blob/master/docs/en_us/internal/testing.rst#running-javascript-unit-tests).

@ahsan-ul-haq ahsan-ul-haq force-pushed the ahsan/removal-underscore-string-from-commerce-js branch from bfba4a6 to 6d41a0d Compare May 12, 2016 15:56
@ahsan-ul-haq
Copy link
Contributor Author

@andy-armstrong please review

@ahsan-ul-haq
Copy link
Contributor Author

@AlasdairSwan please review

AjaxHelpers.respondWithJson(requests, responseData);
};

var mockRender = function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should only be 1 var declaration at the top of the function, comma separating the variable names.

@ahsan-ul-haq ahsan-ul-haq force-pushed the ahsan/removal-underscore-string-from-commerce-js branch from 48d8661 to 026c739 Compare May 14, 2016 22:44
@ahsan-ul-haq
Copy link
Contributor Author

@AlasdairSwan @andy-armstrong made the suggested changes please review.

@jbzdak
Copy link
Contributor

jbzdak commented May 16, 2016

@ahsan-ul-haq First of all thanks for this work! I'm working on a small change to the receipt_view.js, and would like to base tests for my PR on your work.

I noticed following behavior when running tests for this view on my devstack: when I run whole lms test suite (paver test_js_dev -s lms and then just ran all the tests on http://192.168.33.10:9876/debug.html) everything works, however when I run only this suite (by going to: http://192.168.33.10:9876/debug.html?spec=edx.commerce.ReceiptView) I get test failures.

Could you please verify that is the case? If so I think that this would be worth fixing, as it may suggest some inter-dependencies between tests that could lead to sudden test failures in the future.

Example stack-trace looks like that:

Error: Expected 1 to be greater than 1.
    at stack (http://192.168.33.10:9876/absolute/edx/app/edxapp/edx-platform/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1577:17)
    at buildExpectationResult (http://192.168.33.10:9876/absolute/edx/app/edxapp/edx-platform/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1547:14)
    at Spec.Env.expectationResultFactory (http://192.168.33.10:9876/absolute/edx/app/edxapp/edx-platform/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:638:18)
    at Spec.addExpectationResult (http://192.168.33.10:9876/absolute/edx/app/edxapp/edx-platform/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:330:34)
    at Expectation.addExpectationResult (http://192.168.33.10:9876/absolute/edx/app/edxapp/edx-platform/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:588:21)
    at Expectation.toBeGreaterThan (http://192.168.33.10:9876/absolute/edx/app/edxapp/edx-platform/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1501:12)
    at currentRequest (http://192.168.33.10:9876/base/common/js/spec_helpers/ajax_helpers.js:71:33)
    at Object.expectRequest (http://192.168.33.10:9876/base/common/js/spec_helpers/ajax_helpers.js:76:23)
    at mockRequests (http://192.168.33.10:9876/base/js/spec/commerce/receipt_spec.js:36:30)
    at mockRender (http://192.168.33.10:9876/base/js/spec/commerce/receipt_spec.js:49:17)
TypeError: Cannot read property 'readyState' of undefined
    at Object.expectRequest (http://192.168.33.10:9876/base/common/js/spec_helpers/ajax_helpers.js:77:23)
    at mockRequests (http://192.168.33.10:9876/base/js/spec/commerce/receipt_spec.js:36:30)
    at mockRender (http://192.168.33.10:9876/base/js/spec/commerce/receipt_spec.js:49:17)
    at Object.<anonymous> (http://192.168.33.10:9876/base/js/spec/commerce/receipt_spec.js:149:17)
    at attemptSync (http://192.168.33.10:9876/absolute/edx/app/edxapp/edx-platform/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1886:24)
    at QueueRunner.run (http://192.168.33.10:9876/absolute/edx/app/edxapp/edx-platform/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1874:9)
    at QueueRunner.execute (http://192.168.33.10:9876/absolute/edx/app/edxapp/edx-platform/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1859:10)
    at Spec.Env.queueRunnerFactory (http://192.168.33.10:9876/absolute/edx/app/edxapp/edx-platform/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:697:35)
    at Spec.execute (http://192.168.33.10:9876/absolute/edx/app/edxapp/edx-platform/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:359:10)
    at Object.fn (http://192.168.33.10:9876/absolute/edx/app/edxapp/edx-platform/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:2479:37)

@ahsan-ul-haq
Copy link
Contributor Author

@jbzdak I have tried to investigate/fix this but could not find any inter-dependencies between the tests. If you could find any please let me know.

'jquery', 'jquery.ajax-retry',
'js/commerce/views/receipt_view', 'common/js/spec_helpers/ajax_helpers'
],
function ($, AjaxRetry, ReceiptView, AjaxHelpers){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is AjaxRetry being used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it's not being used in this file, it's being used in lms/static/js/commerce/views/receipt_view.js? If so it should be in that file's define array.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no define array in lms/static/js/commerce/views/receipt_view.js.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhh, I see now. Sorry, the function statement at the top made me think there was.

@andy-armstrong
Copy link
Contributor

👍

1 similar comment
@AlasdairSwan
Copy link
Contributor

👍

@andy-armstrong
Copy link
Contributor

@jbzdak @ahsan-ul-haq I've pushed up a fix for running the tests individually. I stepped through the code and found that it was silently throwing an error trying to record events for analytics. I added the standard code for adding spy methods for the expected analytics functions. I also renamed the spec file to mirror the file it was testing, and cleaned up the order of spec files in the Karma main.js file.

@andy-armstrong
Copy link
Contributor

andy-armstrong commented May 16, 2016

@jbzdak @ahsan-ul-haq I should have mentioned that there are many tests that are not clearing the analytics spy once they are done, which is why running all the tests together allowed these tests to run. Ideally we would clean up this logic so that the spies are removed after each test is run, and also we would share the initialization. FYI @jzoldak @benpatterson.

@jzoldak
Copy link
Contributor

jzoldak commented May 16, 2016

@andy-armstrong ugh. definitely thanks for noticing that, tho. Can you please make sure that there is a JIRA ticket for addressing the potential issues with the non-cleaned-up analytics spy?

@jbzdak
Copy link
Contributor

jbzdak commented May 16, 2016

@ahsan-ul-haq @andy-armstrong Thanks, that really helped a lot! When I run the tests one by one they run well.

However, on my devstack, when I run the whole fixture together (for example by this: http://192.168.33.10:9876/debug.html?spec=edx.commerce.ReceiptView) I still get a failure in: "sends analytic event when non verified receipt is rendered", this failure is related to view not making request to fetch the order (stacktrace is at the end of comment). I was able to debug it somewhat and I found the following:

  1. RecepitView doesn't send request as this condition is false: this.$el.data('is-payment-complete') === 'True' (in render() function)
  2. This condition is false as this.$el is empty, which is due to #receipt-container missing from the DOM.
  3. This is missing from the DOM as readFixtures('js/fixtures/commerce/non_verified_receipt.html'); returns an empty string.
  4. When I debugged readFixtures it turned out that for some reason during the call window.XMLHttpRequest is still set to FakeXMLHttpRequest from the the sinon library. It looks like in this case AjaxUtils didn't properly detach the request.
Error stacktrace is here: 
Error: Expected 0 to be greater than 0.
    at stack (http://192.168.33.10:9876/absolute/edx/app/edxapp/edx-platform/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1577:17)
    at buildExpectationResult (http://192.168.33.10:9876/absolute/edx/app/edxapp/edx-platform/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1547:14)
    at Spec.Env.expectationResultFactory (http://192.168.33.10:9876/absolute/edx/app/edxapp/edx-platform/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:638:18)
    at Spec.addExpectationResult (http://192.168.33.10:9876/absolute/edx/app/edxapp/edx-platform/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:330:34)
    at Expectation.addExpectationResult (http://192.168.33.10:9876/absolute/edx/app/edxapp/edx-platform/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:588:21)
    at Expectation.toBeGreaterThan (http://192.168.33.10:9876/absolute/edx/app/edxapp/edx-platform/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1501:12)
    at currentRequest (http://192.168.33.10:9876/base/common/js/spec_helpers/ajax_helpers.js:71:33)
    at Object.expectRequest (http://192.168.33.10:9876/base/common/js/spec_helpers/ajax_helpers.js:76:23)
    at mockRequests (http://192.168.33.10:9876/base/js/spec/commerce/receipt_view_spec.js:36:30)
    at mockRender (http://192.168.33.10:9876/base/js/spec/commerce/receipt_view_spec.js:54:17)
TypeError: Cannot read property 'readyState' of undefined
    at Object.expectRequest (http://192.168.33.10:9876/base/common/js/spec_helpers/ajax_helpers.js:77:23)
    at mockRequests (http://192.168.33.10:9876/base/js/spec/commerce/receipt_view_spec.js:36:30)
    at mockRender (http://192.168.33.10:9876/base/js/spec/commerce/receipt_view_spec.js:54:17)
    at Object.<anonymous> (http://192.168.33.10:9876/base/js/spec/commerce/receipt_view_spec.js:174:17)
    at attemptSync (http://192.168.33.10:9876/absolute/edx/app/edxapp/edx-platform/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1886:24)
    at QueueRunner.run (http://192.168.33.10:9876/absolute/edx/app/edxapp/edx-platform/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1874:9)
    at QueueRunner.execute (http://192.168.33.10:9876/absolute/edx/app/edxapp/edx-platform/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1859:10)
    at Spec.Env.queueRunnerFactory (http://192.168.33.10:9876/absolute/edx/app/edxapp/edx-platform/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:697:35)
    at Spec.execute (http://192.168.33.10:9876/absolute/edx/app/edxapp/edx-platform/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:359:10)
    at Object.fn (http://192.168.33.10:9876/absolute/edx/app/edxapp/edx-platform/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:2479:37)

@andy-armstrong
Copy link
Contributor

@jzoldak I've created a story to track the Jasmine spy issue:

https://openedx.atlassian.net/browse/TNL-4582

@ahsan-ul-haq
Copy link
Contributor Author

@andy-armstrong Thanks for catching this

@ahsan-ul-haq ahsan-ul-haq force-pushed the ahsan/removal-underscore-string-from-commerce-js branch from 24daa42 to af7eb1f Compare May 16, 2016 20:06
@ahsan-ul-haq
Copy link
Contributor Author

ahsan-ul-haq commented May 17, 2016

@jbzdak I have made the changes for individual tests to run in debug mode and now its working fine on my local devstack. Please confirm so I can move forward with merging this.

@ahsan-ul-haq
Copy link
Contributor Author

jenkins run bokchoy

@ahsan-ul-haq ahsan-ul-haq force-pushed the ahsan/removal-underscore-string-from-commerce-js branch from be7b2d7 to 300002b Compare May 17, 2016 13:56
@ahsan-ul-haq ahsan-ul-haq merged commit 7a43b57 into master May 17, 2016
@ahsan-ul-haq ahsan-ul-haq deleted the ahsan/removal-underscore-string-from-commerce-js branch May 17, 2016 14:49
@andy-armstrong
Copy link
Contributor

@mattdrayer This is the PR that is causing you trouble with the merge back.

@andy-armstrong
Copy link
Contributor

Here is the PR that is merging the release back to master:

https://github.com/edx/edx-platform/pull/12495

We will have to be careful not to lose any of this newer PR with the merge.

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.

None yet

7 participants