Skip to content

Commit

Permalink
Clarify what getPageViewObject() is doing
Browse files Browse the repository at this point in the history
There's a bunch of magic numbers and assumptions in this method about
how many ga() functions have been called, in what order, and for what
reason.  This is fine, but it's hard to work out so we leave some more
explicit comments, and introduce an expectation that will fail if our
assumptions are false.
  • Loading branch information
h-lame committed Feb 8, 2018
1 parent e5168fe commit 68d9921
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions spec/javascripts/analytics/static-analytics-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1067,13 +1067,34 @@ describe("GOVUK.StaticAnalytics", function() {
// needs to be called manually. We do this below and reload the arguments sent
// to window.ga in order to obtain the trackPageView args as well.

// 1. get all arguments to all calls to the ga() function
universalSetupArguments = window.ga.calls.allArgs()

// 2. get the ga(function(tracker) { ...}) call - it's the 4th one:
// 1st is the call to create "universal-id"
// 2nd is the call to set "anonymizeIp"
// 3rd is the call to set "displayFeaturesTask"
// 4th is the call that sets the tracker function callback
bound = universalSetupArguments[3][0];

// 3. trigger the callback with a canned tracker object that has a stubbed
// get method that always retuns the same client id. This is the only
// method we expect to call on the tracker object - calling anything
// else should cause these specs to fail.
bound({get: function () { return '12345.67890' }});

// 4. get all the calls to ga() again as executing the callback will add a
// 5th call. This time it's the one to to send "pageview" which is the
// initial page view tracking we're interested in.
universalSetupArguments = window.ga.calls.allArgs();
lastArgumentSet = universalSetupArguments.pop();

// 5. make sure this final set of arguments is the one we're looking for
// and fail the test otherwise.
expect(lastArgumentSet[0]).toEqual('send');
expect(lastArgumentSet[1]).toEqual('pageview');

// 6. extract the arguments to that last call and return them
pageViewObject = lastArgumentSet[2];

return pageViewObject;
Expand Down

0 comments on commit 68d9921

Please sign in to comment.