text: 'use strict'
  
  const TestHelper = require('../../utils/test-helper')
  
  describe('/eligibility-checker/selling-to-museum route', () => {
    let server
    const url = '/eligibility-checker/selling-to-museum'
    const nextUrlAreYouAMuseum = '/eligibility-checker/are-you-a-museum'
    const nextUrlIsItAMusicalInstrument =
      '/eligibility-checker/is-it-a-musical-instrument'
  
    const elementIds = {
      pageTitle: 'pageTitle',
      helpText: 'helpText',
      helpTextList: 'helpTextList',
      moreInfoLink: 'moreInfoLink',
      sellingToMuseum: 'sellingToMuseum',
      sellingToMuseum2: 'sellingToMuseum-2',
      sellingToMuseum3: 'sellingToMuseum-3',
      continue: 'continue'
    }
  
    let document
  
    beforeAll(async () => {
      server = await TestHelper.createServer()
    })
  
    afterAll(async () => {
      await server.stop()
    })
  
    beforeEach(() => {
      _createMocks()
    })
  
    afterEach(() => {
      jest.clearAllMocks()
    })
  
    describe('GET', () => {
      const getOptions = {
        method: 'GET',
        url
      }
  
      beforeEach(async () => {
        document = await TestHelper.submitGetRequest(server, getOptions)
      })
  
      it('should have the Beta banner', () => {
        TestHelper.checkBetaBanner(document)
      })
  
      it('should have the Back link', () => {
        TestHelper.checkBackLink(document)
      })
  
      it('should have the correct page heading', () => {
        const element = document.querySelector(
          `#${elementIds.pageTitle} > legend > h1`
        )
        expect(element).toBeTruthy()
        expect(TestHelper.getTextContent(element)).toEqual(
          'Are you intending to sell or hire out your item out to a museum?'
        )
      })
  
      it('should have the correct help text', () => {
        const element = document.querySelector(`#${elementIds.helpText}`)
        expect(element).toBeTruthy()
        expect(TestHelper.getTextContent(element)).toEqual(
          'The museum must be a member of the International Council of Museums, or accredited by or on behalf of one of the following:'
        )
      })
  
      it('should have a help text list', () => {
        const element = document.querySelector(`#${elementIds.helpTextList}`)
        expect(element).toBeTruthy()
      })
  
      it('should have the more information link', () => {
        const element = document.querySelector(`#${elementIds.moreInfoLink}`)
        expect(element).toBeTruthy()
      })
  
      it('should have the correct radio buttons', () => {
        TestHelper.checkRadioOption(
          document,
          elementIds.sellingToMuseum,
          'Yes',
          'Yes'
        )
  
        TestHelper.checkRadioOption(
          document,
          elementIds.sellingToMuseum2,
          'No',
          'No'
        )
  
        TestHelper.checkRadioOption(
          document,
          elementIds.sellingToMuseum3,
          'I don’t know',
          'I don’t know'
        )
      })
  
      it('should have the correct Call to Action button', () => {
        const element = document.querySelector(`#${elementIds.continue}`)
        expect(element).toBeTruthy()
        expect(TestHelper.getTextContent(element)).toEqual('Continue')
      })
    })
  
    describe('POST', () => {
      let postOptions
  
      beforeEach(() => {
        postOptions = {
          method: 'POST',
          url,
          payload: {}
        }
      })
  
      describe('Success', () => {
        it('should progress to the next route when the first option has been selected', async () => {
          await _checkSelectedRadioAction(
            postOptions,
            server,
            'Yes',
            nextUrlAreYouAMuseum
          )
        })
  
        it('should progress to the next route when the second option has been selected', async () => {
          await _checkSelectedRadioAction(
            postOptions,
            server,
            'No',
            nextUrlIsItAMusicalInstrument
          )
        })
  
        it('should progress to the next route when the third option has been selected', async () => {
          await _checkSelectedRadioAction(
            postOptions,
            server,
            'I don’t know',
            nextUrlIsItAMusicalInstrument
          )
        })
  
        describe('Failure', () => {
          it('should display a validation error message if the user does not select an item', async () => {
            postOptions.payload.sellingToMuseum = ''
            const response = await TestHelper.submitPostRequest(
              server,
              postOptions,
              400
            )
            await TestHelper.checkValidationError(
              response,
              'sellingToMuseum',
              'sellingToMuseum-error',
              'Tell us whether you are selling or hiring out your item to a museum'
            )
          })
        })
      })
    })
  })
  
  const _createMocks = () => {
    TestHelper.createMocks()
  }
  
  const _checkSelectedRadioAction = async (
    postOptions,
    server,
    selectedOption,
    nextUrl
  ) => {
    postOptions.payload.sellingToMuseum = selectedOption
  
    const response = await TestHelper.submitPostRequest(server, postOptions)
  
    expect(response.headers.location).toEqual(nextUrl)
  }
  