import getApp from '../../app'
import request from 'supertest';
import getConfig from '../../libs/config';
import axios from "axios";

const config = getConfig();
const app = getApp(config);
config.env = "test";
const appRequest = request(app); 
jest.mock('axios')

describe('postCodeController', () => {
    

    test('postcode controller should show select page when response ins 200', async() => {

        const testData = [{
            "UPRN": "100110668168",
            "AddressLine1": "1 Mitford Gardens - Ineligible",
            "AddressLine2": "",
            "AddressLine3": "",
            "Town": "Stakeford, Choppington",
            "County": "Northumberland",
            "Postcode": "NE62 5YR"
        }];
        (axios.get as jest.Mock).mockResolvedValueOnce({status: 200, data: testData})
            
          const res = await appRequest.post('/postcode').send({postcode: 'ne128rf'});
          expect(res.status).toBe(302);
          expect(res.header['location']).toContain('/address-select');

    })

    test('postcode controller should show no coverage page when no address data', async() => {

        (axios.get as jest.Mock).mockResolvedValueOnce({status: 204, data: []})
            
          const res = await appRequest.post('/postcode').send({postcode: 'ne204rf'});
          expect(res.status).toBe(302);
          expect(res.header['location']).toContain('/address-not-covered');

    })

    test('postcode controller should show error page when response is 500 from address API', async() => {

        (axios.get as jest.Mock).mockRejectedValueOnce({status: 500})
            
        const res = await appRequest.post('/postcode').send({postcode: 'ne204rf'});
        expect(res.status).toBe(302);
        expect(res.header['location']).toContain('/error500');

    })
})