Skip to content

Commit

Permalink
Remove div wrapper around form in tests
Browse files Browse the repository at this point in the history
The TrackRadioGroup module is only initialised with a form element in the
production code. However, the tests wrap the form in a div.

We should remove the div wrapper to simplify the tests and the code.
  • Loading branch information
gclssvglx committed Nov 10, 2021
1 parent 601f4d7 commit e283679
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
2 changes: 1 addition & 1 deletion app/assets/javascripts/modules/track-radio-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ window.GOVUK.TrackRadioGroupVerify = function (data) {

(function (Modules) {
function TrackRadioGroup (element) {
this.element = element.querySelector('form') || element
this.element = element
}

TrackRadioGroup.prototype.init = function () {
Expand Down
31 changes: 13 additions & 18 deletions spec/javascripts/track-radio-group.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@ describe('A radio group tracker', function () {
spyOn(GOVUK.analytics, 'trackEvent')

element = $(
'<div>' +
'<form onsubmit="event.preventDefault()">' +
'<input type="radio" name="sign-in-option" value="government-gateway">' +
'<input type="radio" name="sign-in-option" value="govuk-verify">' +
'<input type="radio" name="sign-in-option" value="create-an-account">' +
'<button>Submit</button>' +
'</form>' +
'</div>'
'<form onsubmit="event.preventDefault()">' +
'<input type="radio" name="sign-in-option" value="government-gateway">' +
'<input type="radio" name="sign-in-option" value="govuk-verify">' +
'<input type="radio" name="sign-in-option" value="create-an-account">' +
'<button>Submit</button>' +
'</form>'
)

$(document.body).append(element)
$('body').append(element)

tracker = new GOVUK.Modules.TrackRadioGroup(element[0])
tracker.init()
Expand Down Expand Up @@ -154,19 +152,16 @@ describe('A radio group tracker', function () {
})

describe('cross domain tracking enabled', function () {
var $form

beforeEach(function () {
tracker.trackVerifyUser(element, { status: 'OK', value: true })

spyOn(GOVUK.analytics, 'addLinkedTrackerDomain')

$form = element.find('form')
$form.attr('data-tracking-code', 'UA-xxxxxx')
$form.attr('data-tracking-domain', 'test.service.gov.uk')
$form.attr('data-tracking-name', 'testTracker')
element.attr('data-tracking-code', 'UA-xxxxxx')
element.attr('data-tracking-domain', 'test.service.gov.uk')
element.attr('data-tracking-name', 'testTracker')

tracker = new GOVUK.Modules.TrackRadioGroup($form[0])
tracker = new GOVUK.Modules.TrackRadioGroup(element[0])
tracker.init()
})

Expand All @@ -177,8 +172,8 @@ describe('A radio group tracker', function () {
})

it('sends an event to the linked tracker when the form is submitted', function () {
$form.find('input[value="govuk-verify"]').trigger('click')
$form.find('button').trigger('click')
element.find('input[value="govuk-verify"]').trigger('click')
element.find('button').trigger('click')

expect(GOVUK.analytics.trackEvent).toHaveBeenCalledWith(
'Radio button chosen', 'govuk-verify-with-hint', { trackerName: 'testTracker', transport: 'beacon' }
Expand Down

0 comments on commit e283679

Please sign in to comment.