/* eslint-env jest */

import React from 'react'
import {
  shallow
} from 'enzyme'

jest.mock('utils/client-location')
jest.mock('ads/consentManagement')
jest.mock('analytics/withPageviewTracking', () => child => child)

beforeEach(() => {
  jest.clearAllMocks()
})

describe('App', () => {
  it('renders without error', () => {
    const App = require('../App').default
    shallow(
      <App />
    )
  })

  it('registers callback with the CMP for data consent update (when in the EU)', async () => {
    // Mock that the client is in the EU
    const isInEuropeanUnion = require('utils/client-location').isInEuropeanUnion
    isInEuropeanUnion.mockResolvedValue(true)

    const App = require('../App').default
    const wrapper = shallow(
      <App />
    )
    await wrapper.instance().componentDidMount()
    wrapper.update()
    const registerConsentCallback = require('ads/consentManagement').registerConsentCallback
    expect(registerConsentCallback).toHaveBeenCalled()
  })

  it('does not register callback with the CMP for data consent update (when not in the EU)', async () => {
    // Mock that the client is not in the EU
    const isInEuropeanUnion = require('utils/client-location').isInEuropeanUnion
    isInEuropeanUnion.mockResolvedValue(false)

    const App = require('../App').default
    const wrapper = shallow(
      <App />
    )
    await wrapper.instance().componentDidMount()
    wrapper.update()
    const registerConsentCallback = require('ads/consentManagement').registerConsentCallback
    expect(registerConsentCallback).not.toHaveBeenCalled()
  })

  it('saves to localStorage that there is updated consent data when the CMP says it\'s been updated', async () => {
    // Mock the callback registration so we can trigger it ourselves
    var cmpCallback
    const registerConsentCallback = require('ads/consentManagement').registerConsentCallback
    registerConsentCallback.mockImplementationOnce(cb => {
      cmpCallback = cb
    })

    const saveConsentUpdateEventToLocalStorage = require('ads/consentManagement')
      .saveConsentUpdateEventToLocalStorage

    // Mock that the client is in the EU
    const isInEuropeanUnion = require('utils/client-location').isInEuropeanUnion
    isInEuropeanUnion.mockResolvedValue(true)
    const App = require('../App').default
    const wrapper = shallow(
      <App />
    )
    await wrapper.instance().componentDidMount()
    wrapper.update()

    // We should not have done anything yet
    expect(saveConsentUpdateEventToLocalStorage).not.toHaveBeenCalled()

    // Mock the CMP's callback for when consent data has changed
    cmpCallback('some-consent-string', true)
    expect(saveConsentUpdateEventToLocalStorage).toHaveBeenCalledTimes(1)
  })
})

@@ -2733,12 +2733,674 @@
 (1)%0A  %7D)
+%0A%0A  it('unregisters its callback when unmounting', async () =%3E %7B%0A    expect.assertions(2)%0A%0A    // Mock that the client is in the EU%0A    const isInEuropeanUnion = require('utils/client-location').isInEuropeanUnion%0A    isInEuropeanUnion.mockResolvedValue(true)%0A    const App = require('../App').default%0A    const wrapper = shallow(%0A      %3CApp /%3E%0A    )%0A    await wrapper.instance().componentDidMount()%0A    wrapper.update()%0A%0A    const unregisterConsentCallback = require('ads/consentManagement').unregisterConsentCallback%0A    expect(unregisterConsentCallback).not.toHaveBeenCalled()%0A    wrapper.unmount()%0A    expect(unregisterConsentCallback).toHaveBeenCalled()%0A  %7D)
 %0A%7D)%0A

Add a test