Skip to content
Snippets Groups Projects
findUserPdl.spec.ts 1.48 KiB
Newer Older
  • Learn to ignore specific revisions
  • const { errors } = require('cozy-konnector-libs')
    const { findUserPdl } = require('../src/findUserPdl')
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    const xml2js = require('xml2js')
    
    
    var mockParsePdl: jest.Mock
    
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    // Function to mock in order to test findUserPdl()
    
    jest.mock('../src/parsing', () => ({
      parseUserPdl: mockParsePdl = jest.fn(),
    }))
    
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    // Dependencies
    
    jest.mock('easy-soap-request', () =>
      jest.fn().mockResolvedValue({
        response: {
          body: 'test',
        },
      })
    )
    
    jest.mock('../src/requests/sge', () => ({
      rechercherPoint: jest.fn().mockResolvedValue('response'),
    }))
    
    //OK
    jest.spyOn(xml2js, 'parseStringPromise').mockResolvedValue('response')
    
    
    describe('recherchePoint', () => {
      it('should throw LOGIN_FAILED for too many responses', async () => {
        mockParsePdl.mockImplementationOnce(() => {
          throw new Error('fail')
        })
    
        try {
          const result = await findUserPdl(
            'azertyuiop',
            'apiKey',
            'login@user.com',
            'John',
            '1 street',
            '69069',
            '69069'
          )
          console.log('result: ' + result)
          expect(true).toBe(false)
        } catch (error) {
          console.log('error in test file ' + error)
          expect(error).toBe(errors.LOGIN_FAILED)
        }
      })
    
      it('should return a correct pdl number', async () => {
        mockParsePdl.mockResolvedValue('12345')
        expect(
          await findUserPdl(
            'azertyuiop',
            'apiKey',
            'login@user.com',
            'John',
            '1 street',
            '69069',
            '69069'
          )
        ).toBe('12345')
      })
    })