// (C) 2007-2019 GoodData Corporation import { isMockRequestMatching, mock, getMatchingResponse } from "../mockRequest"; import * as HttpStatusCodes from "http-status-codes"; const response = "response"; describe("mock", () => { function mockRes() { const res: any = { status: jest.fn().mockImplementation(() => res), send: jest.fn().mockImplementation(() => res), json: jest.fn().mockImplementation(() => res), }; return res; } it("should setup express method", () => { const app: any = { get: jest.fn(), }; mock(app, "get", "uri", []); expect(app.get).toHaveBeenCalledTimes(1); }); it("should respond with 400 if mock is not specified", () => { const req: any = {}; const res = mockRes(); const app: any = { get: (_url: string, callback: any) => { callback(req, res); }, }; mock(app, "get", "uri", []); expect(res.status).toHaveBeenCalledWith(HttpStatusCodes.BAD_REQUEST); }); it("should respond with 400 if mock is not found", () => { const req: any = {}; const res = mockRes(); const app: any = { get: (_url: string, callback: any) => { callback(req, res); }, }; mock(app, "get", "uri", [{ query: { a: 1 }, response }]); expect(res.status).toHaveBeenCalledWith(HttpStatusCodes.BAD_REQUEST); }); it("should respond with 200 and the default response if mock is not found but the default response has been defined", () => { const req: any = {}; const res = mockRes(); const app: any = { get: (_url: string, callback: any) => { callback(req, res); }, }; mock(app, "get", "uri", [{ query: { a: 1 }, response }], { response: "defaultResponse" }); expect(res.status).toHaveBeenCalledWith(HttpStatusCodes.OK); expect(res.json).toHaveBeenCalledWith("defaultResponse"); }); it("should respond with 200 and mocked response if mock is found", () => { const req: any = { query: { a: 1, }, }; const res = mockRes(); const app: any = { get: (_url: string, callback: any) => { callback(req, res); }, }; mock(app, "get", "uri", [{ query: { a: 1 }, response }]); expect(res.status).toHaveBeenCalledWith(HttpStatusCodes.OK); expect(res.json).toHaveBeenCalledWith(response); }); }); describe("mockResponse", () => { it("should return undefined if mock is not specified", () => { const req: any = {}; const mockedResponse = getMatchingResponse(req, []); expect(mockedResponse).toBeUndefined(); }); it("should return undefined if mock is not found", () => { const req: any = {}; const mockedResponse = getMatchingResponse(req, [{ query: { a: 1 }, response }]); expect(mockedResponse).toBeUndefined(); }); it("should return mocked response if mock is found", () => { const req: any = { query: { a: 1, }, }; const mockedResponse = getMatchingResponse(req, [{ query: { a: 1 }, response }]); expect(mockedResponse).toEqual({ query: { a: 1, }, response, }); }); }); describe("isMockRequestMatching", () => { it("should return false if request has no query and there is expected query", () => { const req: any = {}; const result = isMockRequestMatching(req, { query: { a: 1 }, response }); expect(result).toEqual(false); }); it("should return true if request is matched by query", () => { const req: any = { query: { a: 1 } }; const result = isMockRequestMatching(req, { query: { a: 1 }, response }); expect(result).toEqual(true); }); it("should return true if request is matched by params", () => { const req: any = { headers: { a: 1 } }; const result = isMockRequestMatching(req, { headers: { a: 1 }, response }); expect(result).toEqual(true); }); it("should return true if request is matched by headers", () => { const req: any = { params: { a: 1 } }; const result = isMockRequestMatching(req, { params: { a: 1 }, response }); expect(result).toEqual(true); }); it("should return true if request is matched by body", () => { const req: any = { body: { a: 1 } }; const result = isMockRequestMatching(req, { body: { a: 1 }, response }); expect(result).toEqual(true); }); it("should return false if request is not matched by query", () => { const req: any = { query: { a: 1 } }; const result = isMockRequestMatching(req, { query: { a: 2 }, response }); expect(result).toEqual(false); }); it("should return false if request is not matched by params", () => { const req: any = { params: { a: 1 } }; const result = isMockRequestMatching(req, { params: { a: 2 }, response }); expect(result).toEqual(false); }); it("should return false if request is not matched by headers", () => { const req: any = { headers: { a: 1 } }; const result = isMockRequestMatching(req, { headers: { a: 2 }, response }); expect(result).toEqual(false); }); it("should return false if request is not matched by body", () => { const req: any = { body: { a: 1 } }; const result = isMockRequestMatching(req, { body: { a: 2 }, response }); expect(result).toEqual(false); }); it("should return true if request is matched by query and headers", () => { const req: any = { query: { a: 1 }, headers: { a: 2 } }; const result = isMockRequestMatching(req, { query: { a: 1 }, headers: { a: 2 }, response, }); expect(result).toEqual(true); }); it("should return false if request is matched by query but not headers", () => { const req: any = { query: { a: 1 }, headers: { a: 2 } }; const result = isMockRequestMatching(req, { query: { a: 1 }, headers: { a: 3 }, response, }); expect(result).toEqual(false); }); it("should return false if request is matched by query and body is not expected but still is in req", () => { const req: any = { query: { a: 1 }, body: { a: 2 } }; const result = isMockRequestMatching(req, { query: { a: 1 }, response }); expect(result).toEqual(false); }); it("should ignore headers in request if not expected in mock", () => { const req: any = { query: { a: 1 }, headers: { a: 2 } }; const result = isMockRequestMatching(req, { query: { a: 1 }, response }); expect(result).toEqual(true); }); });