import injectValidateFormRequest from "../../middleware/injectValidateFormRequest"
import { Request, Response,NextFunction } from "express";
import validate from '../../middleware/validators/validate'

jest.mock('../../middleware/validators/validate');

describe('inject validator for each post req should', () => {
    let req: Request;
    let res: Partial<Response>;
    let next: NextFunction;

    beforeEach(() => {
        req = {
            method: 'POST',
            originalUrl: '/test-url',
            body: {},
        } as Request;
        res = {
            redirect: jest.fn(),
        } as Partial<Response>;
        next = jest.fn();
    });

    test('move to next function when validation function returns true', () => {

        (validate as jest.Mock).mockReturnValue(true);

        const returnFn = injectValidateFormRequest([]);

        returnFn(req, res as Response, next);

        expect(res.redirect).not.toHaveBeenCalled();
        expect(validate).toHaveBeenCalledWith(req, expect.any(Array));
        expect(next).toHaveBeenCalled();
        
    })

    test('move to redirect with error  when validation function returns false', () => {

        (validate as jest.Mock).mockReturnValue(false);

        const returnFn = injectValidateFormRequest([]);

        returnFn(req, res as Response, next);

        expect(res.redirect).toHaveBeenCalled();
        expect(next).not.toHaveBeenCalled();
        expect(validate).toHaveBeenCalledWith(req, expect.any(Array));
    })
})