Sha256: 9ff7ec124cac1185846d7febde91bf777466dd52ee118785630c49cc5d543447

Contents?: true

Size: 1.85 KB

Versions: 31

Compression:

Stored size: 1.85 KB

Contents

/* global describe it expect afterEach beforeEach spyOn */

var $ = window.jQuery

describe('GOVUK.analyticsPlugins.mailtoLinkTracker', function () {
  'use strict'
  var GOVUK = window.GOVUK

  var $links

  beforeEach(function () {
    $links = $(
      '<div class="mailto-links">' +
        '<a href="mailto:name1@email.com"></a>' +
        '<a href="mailto:name2@email.com">The link for a mailto</a>' +
        '<a href="mailto:name3@email.com"><img src="/img" /></a>' +
      '</div>'
    )

    $('html').on('click', function (evt) { evt.preventDefault() })
    $('body').append($links)
    GOVUK.analytics = {trackEvent: function () {}}

    GOVUK.analyticsPlugins.mailtoLinkTracker()
  })

  afterEach(function () {
    $('html').off()
    $('body').off()
    $links.remove()
    delete GOVUK.analytics
  })

  it('listens to click events on mailto links', function () {
    spyOn(GOVUK.analytics, 'trackEvent')

    $('.mailto-links a').each(function () {
      $(this).trigger('click')
      expect(GOVUK.analytics.trackEvent).toHaveBeenCalled()
      GOVUK.analytics.trackEvent.calls.reset()
    })
  })

  it('tracks mailto addresses and link text', function () {
    spyOn(GOVUK.analytics, 'trackEvent')
    $('.mailto-links a').trigger('click')

    expect(GOVUK.analytics.trackEvent).toHaveBeenCalledWith(
      'Mailto Link Clicked', 'mailto:name1@email.com', {transport: 'beacon'})

    expect(GOVUK.analytics.trackEvent).toHaveBeenCalledWith(
      'Mailto Link Clicked', 'mailto:name2@email.com', {transport: 'beacon', label: 'The link for a mailto'})
  })

  it('listens to click events on elements within mailto links', function () {
    spyOn(GOVUK.analytics, 'trackEvent')

    $('.mailto-links a img').trigger('click')
    expect(GOVUK.analytics.trackEvent).toHaveBeenCalledWith(
      'Mailto Link Clicked', 'mailto:name3@email.com', {transport: 'beacon'})
  })
})

Version data entries

31 entries across 31 versions & 2 rubygems

Version Path
govuk_frontend_toolkit-8.2.0 app/assets/spec/unit/analytics/mailto-link-tracker.spec.js
govuk_frontend_toolkit-8.1.0 app/assets/spec/unit/analytics/mailto-link-tracker.spec.js
govuk_frontend_toolkit-8.0.0 app/assets/spec/unit/analytics/mailto-link-tracker.spec.js
govuk_frontend_toolkit-7.6.0 app/assets/spec/unit/analytics/mailto-link-tracker.spec.js
idsk_frontend_toolkit-8.4.4 app/assets/spec/unit/analytics/mailto-link-tracker.spec.js
idsk_frontend_toolkit-7.4.3 app/assets/spec/unit/analytics/mailto-link-tracker.spec.js
govuk_frontend_toolkit-7.5.0 app/assets/spec/unit/analytics/mailto-link-tracker.spec.js
idsk_frontend_toolkit-7.4.2 app/assets/spec/unit/analytics/mailto-link-tracker.spec.js
govuk_frontend_toolkit-7.4.2 app/assets/spec/unit/analytics/mailto-link-tracker.spec.js
govuk_frontend_toolkit-7.4.1 app/assets/spec/unit/analytics/mailto-link-tracker.spec.js
govuk_frontend_toolkit-7.4.0 app/assets/spec/unit/analytics/mailto-link-tracker.spec.js
govuk_frontend_toolkit-7.3.0 app/assets/spec/unit/analytics/mailto-link-tracker.spec.js
govuk_frontend_toolkit-7.2.0 app/assets/spec/unit/analytics/mailto-link-tracker.spec.js
govuk_frontend_toolkit-7.1.0 app/assets/spec/unit/analytics/mailto-link-tracker.spec.js
govuk_frontend_toolkit-7.0.1 app/assets/spec/unit/analytics/mailto-link-tracker.spec.js
govuk_frontend_toolkit-7.0.0 app/assets/spec/unit/analytics/mailto-link-tracker.spec.js
govuk_frontend_toolkit-6.0.4 app/assets/spec/unit/analytics/mailto-link-tracker.spec.js
govuk_frontend_toolkit-6.0.3 app/assets/spec/unit/analytics/mailto-link-tracker.spec.js
govuk_frontend_toolkit-6.0.2 app/assets/spec/unit/analytics/mailto-link-tracker.spec.js
govuk_frontend_toolkit-6.0.1 app/assets/spec/unit/analytics/mailto-link-tracker.spec.js