Skip to content
Snippets Groups Projects
findUserPdl.spec.js 1.28 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')
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    var mockParseUserPdl
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    // Function to mock in order to test findUserPdl()
    
    jest.mock('../src/parsing', () => ({
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
      parseUserPdl: (mockParseUserPdl = jest.fn()),
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    // Dependencies
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    jest.spyOn(xml2js, 'parseStringPromise').mockResolvedValue('response')
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    jest.mock('easy-soap-request', () =>
      jest.fn().mockResolvedValue({
        response: {
          body: 'test',
        },
      })
    )
    
    
    describe('recherchePoint', () => {
      it('should throw LOGIN_FAILED for too many responses', async () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        mockParseUserPdl.mockImplementationOnce(() => {
    
          throw new Error('fail')
        })
    
        try {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
          await findUserPdl(
    
            'azertyuiop',
            'apiKey',
            'login@user.com',
            'John',
            '1 street',
            '69069',
            '69069'
          )
          expect(true).toBe(false)
        } catch (error) {
          expect(error).toBe(errors.LOGIN_FAILED)
        }
      })
    
      it('should return a correct pdl number', async () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        mockParseUserPdl.mockResolvedValue('12345')
    
        expect(
          await findUserPdl(
            'azertyuiop',
            'apiKey',
            'login@user.com',
            'John',
            '1 street',
            '69069',
            '69069'
          )
        ).toBe('12345')
      })
    })