<commit_before>describe("GOVUK.PII", function() {
  var pii

  beforeEach(function() {
    resetHead()
    pii = new GOVUK.pii()
  })

  afterEach(function() {
    resetHead()
  });

  describe('by default', function() {
    it("strips email addresses, but not postcodes and dates from strings", function() {
      var string = pii.stripPII("this is <EMAIL> address, this is a sw1a 1aa postcode, this is a 2019-01-21 date")
      expect(string).toEqual("this is [email] address, this is a sw1a 1aa postcode, this is a 2019-01-21 date")
    })

    it("strips email addresses but not dates and postcodes from objects", function() {
      var obj = {
        'email': 'this is <EMAIL> address',
        'postcode': 'this is a sw1a 1aa postcode',
        'date': 'this is a 2019-01-21 date'
      }

      var strippedObj = {
        'email': 'this is [email] address',
        'postcode': 'this is a sw1a 1aa postcode',
        'date': 'this is a 2019-01-21 date'
      }

      obj = pii.stripPII(obj)
      expect(obj).toEqual(strippedObj)
    })

    it("strips email addresses but not dates and postcodes from arrays", function() {
      var arr = [
        'this is <EMAIL> address',
        'this is a sw1a 1aa postcode',
        'this is a 2019-01-21 date'
      ]

      var strippedArr = [
        'this is [email] address',
        'this is a sw1a 1aa postcode',
        'this is a 2019-01-21 date'
      ]

      arr = pii.stripPII(arr)
      expect(arr).toEqual(strippedArr)
    })
  })

  describe('when configured to remove all PII', function() {
    beforeEach(function() {
      pageWantsDatesStripped()
      pageWantsPostcodesStripped()
      pii = new GOVUK.pii()
    })

    it("strips email addresses, postcodes and dates from strings", function() {
      var string = pii.stripPII("this is <EMAIL> address, this is a sw1a 1aa postcode, this is a 2019-01-21 date")
      expect(string).toEqual("this is [email] address, this is a [postcode] postcode, this is a [date] date")
    })

    it("strips all PII from objects", function() {
      var obj = {
        'email': 'this is <EMAIL> address',
        'postcode': 'this is a sw1a 1aa postcode',
        'date': 'this is a 2019-01-21 date'
      }

      var strippedObj = {
        'email': 'this is [email] address',
        'postcode': 'this is a [postcode] postcode',
        'date': 'this is a [date] date'
      }

      obj = pii.stripPII(obj)
      expect(obj).toEqual(strippedObj)
    })

    it("strips all PII from arrays", function() {
      var arr = [
        'this is <EMAIL>',
        'this is a sw1a 1aa postcode',
        'this is a 2019-01-21 date'
      ]

      var strippedArr = [
        'this is [email] address',
        'this is a [postcode] postcode',
        'this is a [date] date'
      ]

      arr = pii.stripPII(arr)
      expect(arr).toEqual(strippedArr)
    })
  })

  describe('when there is a a govuk:static-analytics:strip-postcodes meta tag present', function() {
    beforeEach(function() {
      pageWantsPostcodesStripped()
      pii = new GOVUK.pii()
    });

    it('observes the meta tag and strips out postcodes', function() {
      expect(pii.stripPostcodePII).toEqual(true);
      var string = pii.stripPII("this is <EMAIL> address, this is a sw1a 1aa postcode, this is a 2019-01-21 date")
      expect(string).toEqual("this is [email] address, this is a [postcode] postcode, this is a 2019-01-21 date")
    });
  });

  describe('when there is a a govuk:static-analytics:strip-dates meta tag present', function() {
    beforeEach(function() {
      pageWantsDatesStripped()
      pii = new GOVUK.pii()
    });

    it('observes the meta tag and strips out postcodes', function() {
      expect(pii.stripDatePII).toEqual(true);
      var string = pii.stripPII("this is <EMAIL> address, this is a sw1a 1aa postcode, this is a 2019-01-21 date")
      expect(string).toEqual("this is [email] address, this is a sw1a 1aa postcode, this is a [date] date")
    });
  });

  function resetHead() {
    $('head').find('meta[name="govuk:static-analytics:strip-postcodes"]').remove();
    $('head').find('meta[name="govuk:static-analytics:strip-dates"]').remove();
  }

  function pageWantsDatesStripped() {
    $('head').append('<meta name="govuk:static-analytics:strip-dates" value="does not matter" />');
  }

  function pageWantsPostcodesStripped() {
    $('head').append('<meta name="govuk:static-analytics:strip-postcodes" value="does not matter" />');
  }
});<commit_msg>convert pii.spec.js to use es6 and jest<commit_after>/* eslint new-cap: ["error", { "newIsCap": false }] */

describe('GOVUK.PII', () => {
  let pii

  beforeAll(() => {
    require('../../node_modules/digitalmarketplace-frontend-toolkit/toolkit/javascripts/analytics/_govukAnalytics.js')
    require('../../node_modules/digitalmarketplace-frontend-toolkit/toolkit/javascripts/analytics/_pii.js')
  })

  beforeEach(() => {
    pii = new GOVUK.pii()
  })

  afterEach(() => {
    resetHead()
  })

  describe('by default', () => {
    it('strips email addresses, but not postcodes and dates from strings', () => {
      const results = pii.stripPII('this is <EMAIL> address, this is a sw1a 1aa postcode, this is a 2019-01-21 date')
      expect(results).toEqual('this is [email] address, this is a sw1a 1aa postcode, this is a 2019-01-21 date')
    })

    it('strips email addresses but not dates and postcodes from objects', () => {
      const obj = {
        email: 'this is <EMAIL> address',
        postcode: 'this is a sw1a 1aa postcode',
        date: 'this is a 2019-01-21 date'
      }

      const strippedObj = {
        email: 'this is [email] address',
        postcode: 'this is a sw1a 1aa postcode',
        date: 'this is a 2019-01-21 date'
      }

      const results = pii.stripPII(obj)
      expect(results).toEqual(strippedObj)
    })

    it('strips email addresses but not dates and postcodes from arrays', () => {
      const arr = [
        'this is <EMAIL> address',
        'this is a sw1a 1aa postcode',
        'this is a 2019-01-21 date'
      ]

      const strippedArr = [
        'this is [email] address',
        'this is a sw1a 1aa postcode',
        'this is a 2019-01-21 date'
      ]

      const results = pii.stripPII(arr)
      expect(results).toEqual(strippedArr)
    })
  })

  describe('when configured to remove all PII', () => {
    beforeEach(() => {
      pageWantsDatesStripped()
      pageWantsPostcodesStripped()
      pii = new GOVUK.pii()
    })

    it('strips email addresses, postcodes and dates from strings', () => {
      const results = pii.stripPII('this is <EMAIL> address, this is a sw1a 1aa postcode, this is a 2019-01-21 date')
      expect(results).toEqual('this is [email] address, this is a [postcode] postcode, this is a [date] date')
    })

    it('strips all PII from objects', () => {
      const obj = {
        email: 'this is <EMAIL> address',
        postcode: 'this is a sw1a 1aa postcode',
        date: 'this is a 2019-01-21 date'
      }

      const strippedObj = {
        email: 'this is [email] address',
        postcode: 'this is a [postcode] postcode',
        date: 'this is a [date] date'
      }

      const results = pii.stripPII(obj)
      expect(results).toEqual(strippedObj)
    })

    it('strips all PII from arrays', () => {
      const arr = [
        'this is <EMAIL> address',
        'this is a sw1a 1aa postcode',
        'this is a 2019-01-21 date'
      ]

      const strippedArr = [
        'this is [email] address',
        'this is a [postcode] postcode',
        'this is a [date] date'
      ]

      const results = pii.stripPII(arr)
      expect(results).toEqual(strippedArr)
    })
  })

  describe('when there is a a govuk:static-analytics:strip-postcodes meta tag present', () => {
    beforeEach(() => {
      pageWantsPostcodesStripped()
      pii = new GOVUK.pii()
    })

    it('observes the meta tag and strips out postcodes', () => {
      expect(pii.stripPostcodePII).toBeTruthy()
      const result = pii.stripPII('this is <EMAIL> address, this is a sw1a 1aa postcode, this is a 2019-01-21 date')
      expect(result).toEqual('this is [email] address, this is a [postcode] postcode, this is a 2019-01-21 date')
    })
  })

  describe('when there is a a govuk:static-analytics:strip-dates meta tag present', () => {
    beforeEach(() => {
      pageWantsDatesStripped()
      pii = new GOVUK.pii()
    })

    it('observes the meta tag and strips out postcodes', () => {
      expect(pii.stripDatePII).toBeTruthy()
      const results = pii.stripPII('this is <EMAIL> address, this is a sw1a 1aa postcode, this is a 2019-01-21 date')
      expect(results).toEqual('this is [email] address, this is a sw1a 1aa postcode, this is a [date] date')
    })
  })

  const resetHead = () => {
    // Rather long winded way to remove the meta tags in the head
    // this is to resolve "'remove()' is not a function" error when the
    // element does not exist
    const stripPostcodesMetaTAG = (document.querySelector('meta[name="govuk:static-analytics:strip-postcodes"]') || { remove: () => 0 })
    const stripDatesMetaTAG = (document.querySelector('meta[name="govuk:static-analytics:strip-dates"]') || { remove: () => 0 })

    // Removes the tags if not and should not raise an error if they do not exist
    stripPostcodesMetaTAG.remove()
    stripDatesMetaTAG.remove()
  }

  const pageWantsDatesStripped = () => {
    const metaTAG = document.createElement('meta')
    metaTAG.value = 'does not matter'
    metaTAG.name = 'govuk:static-analytics:strip-dates'

    document.querySelector('head').appendChild(metaTAG)
  }

  const pageWantsPostcodesStripped = () => {
    const metaTAG = document.createElement('meta')
    metaTAG.value = 'does not matter'
    metaTAG.name = 'govuk:static-analytics:strip-postcodes'

    document.querySelector('head').appendChild(metaTAG)
  }
})

{'fraction_non_alphanumeric': 0.13495754144763444, 'fraction_numerical': 0.024565305297209867, 'mean_word_length': 2.7501895375284304, 'pattern_counts': {'":': 1, '<': 21, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1436407', 'n_tokens_mistral': 3404, 'n_tokens_neox': 3196, 'n_words': 934}