import {getParameterString, request, setNullBusinessUnit} from './rest'
import fetchMock from 'fetch-mock'

describe('tocco-util', () => {
  describe('rest', () => {
    describe('getParameterString', () => {
      it('should order params', () => {
        const params = {
          'param1': 'val1',
          'param3': 'val3',
          'param2': 'val2'
        }
        const res = getParameterString(params)

        expect(res).to.eql('?param1=val1&param2=val2&param3=val3')
      })

      it('should remove empty params', () => {
        const params = {
          'param1': '',
          'param2': 'val2'
        }
        const res = getParameterString(params)

        expect(res).to.eql('?param2=val2')
      })

      it('should decode string', () => {
        const params = {
          'param1': '%'
        }
        const res = getParameterString(params)

        expect(res).to.eql('?param1=%25')
      })

      it('should handle arrays string', () => {
        const params = {
          'param1': ['1', '2']
        }
        const res = getParameterString(params)

        expect(res).to.eql('?param1=1&param1=2')
      })

      it('should handle undefined params', () => {
        const res = getParameterString(undefined)

        expect(res).to.eql('')
      })
    })

    describe('Request', () => {
      beforeEach(() => {
        fetchMock.reset()
        fetchMock.restore()
      })

      it('should return with accepted errorCode', done => {
        const statusCode = 400
        const bodyObj = {
          errorCode: 'SAVE_FAILED',
          errors: {
            'firstname': {
              'illegal': ['SPEICHERFEHLER: Vorname tocco1 nicht toleriert.']
            }
          }
        }
        const body = new Blob([JSON.stringify(bodyObj, null, 2)], {type: 'application/json'})
        const mockedResponse = new Response(body, {'status': statusCode})

        fetchMock.get('*', mockedResponse)
        const resource = 'Entities/Contact'
        request(resource, {}, 'GET', {}, ['SAVE_FAILED']).then(response => {
          expect(response.status).to.eql(statusCode)
          expect(response.body).to.eql(bodyObj)
          done()
        })
      })

      it('should trow exception on unaccepted errorCode', done => {
        const statusCode = 400
        const bodyObj = {
          errorCode: 'SAVE_FAILED'
        }

        const body = new Blob([JSON.stringify(bodyObj, null, 2)], {type: 'application/json'})
        const mockedResponse = new Response(body, {'status': statusCode, statusText: 'Some error'})

        fetchMock.get('*', mockedResponse)
        const resource = 'Entities/Contact'
        request(resource, {}, 'GET', {}, []).catch(() => {
          done()
        })
      })

      it('should set content type header', () => {
        fetchMock.get('*', {})

        request('', {}, 'GET', {}, [])

        const headerObject = fetchMock.lastOptions().headers.map
        expect(headerObject).to.have.property('content-type')
      })

      it('should set null business unit header', () => {
        fetchMock.get('*', {})

        request('', {}, 'GET', {}, [])
        const headerObject = fetchMock.lastOptions().headers.map
        expect(headerObject).to.not.property('x-business-unit')

        setNullBusinessUnit(true)
        request('', {}, 'GET', {}, [])
        const headerObject2 = fetchMock.lastOptions().headers.map
        expect(headerObject2).to.have.property('x-business-unit')
      })

      it('should use ordered params', () => {
        fetchMock.get('*', {})
        const resource = 'Entities/Contact'
        const params = {
          _search: 'test',
          xyz: 'abc'
        }
        request(resource, params)

        const lastCall = fetchMock.lastCall()[0]
        expect(lastCall).to.eql('/nice2/rest/Entities/Contact?_search=test&xyz=abc')
      })
    })
  })
})

@@ -2881,38 +2881,33 @@
     const header
-Object
+s
  = fetchMock.las
@@ -2916,36 +2916,32 @@
 ptions().headers
-.map
 %0A        expect(
@@ -2950,46 +2950,108 @@
 ader
-Object).to.have.property('content-type
+s.get('randomxyxc')).to.be.null%0A        expect(headers.get('content-type')).to.eql('application/json
 ')%0A 
@@ -3207,22 +3207,17 @@
 t header
-Object
+s
  = fetch
@@ -3234,36 +3234,32 @@
 ptions().headers
-.map
 %0A        expect(
@@ -3268,31 +3268,13 @@
 ader
-Object).to.not.property
+s.get
 ('x-
@@ -3288,16 +3288,28 @@
 s-unit')
+).to.be.null
 %0A%0A      
@@ -3367,32 +3367,33 @@
  'GET', %7B%7D, %5B%5D)%0A
+%0A
         const he
@@ -3396,22 +3396,17 @@
 t header
-Object
+s
 2 = fetc
@@ -3436,12 +3436,8 @@
 ders
-.map
 %0A   
@@ -3458,50 +3458,54 @@
 ader
-Object2).to.have.property('x-business-unit
+s2.get('x-business-unit')).to.eql('__n-u-l-l__
 ')%0A 

refactor failing saucelabs tests