Untitled
unknown
typescript
2 years ago
3.7 kB
17
Indexable
import { Context, ZodeError } from '@zode/zode'; // Make sure to mock '@zode/zode' appropriately import * as moduleToTest from './moduleToTest'; // Replace 'moduleToTest' with the actual module name containing the getUrl function import * as commonHandlers from '../../../handlers'; // Make sure to mock '../../../handlers' appropriately import * as helpers from '../helpers'; // Make sure to mock '../helpers' appropriately // Helper function to create a mocked Context const createMockContext = (reqBody: any, state?: any): Context => ({ state, request: { body: reqBody, }, }); describe('getUrl', () => { afterEach(() => { jest.clearAllMocks(); }); it('should return a successful response when all URLs are valid', async () => { // Arrange const reqBody = { url1: { url_name: 'valid_url1', url_data: { key1: 'value1' }, }, url2: { url_name: 'valid_url2', url_data: { key2: 'value2' }, }, }; const ctx = createMockContext(reqBody); const generateUrlSpy = jest.spyOn(helpers, 'generateUrl').mockResolvedValue('http://example.com'); // Mock other required functions as needed // Act const result = await moduleToTest.getUrl(ctx); // Assert expect(result).toEqual({ data: { url_details: [ { url_name: 'valid_url1', status: constants.STATUS.SUCCESS, url: 'http://example.com' }, { url_name: 'valid_url2', status: constants.STATUS.SUCCESS, url: 'http://example.com' }, ], response_status: constants.STATUS.SUCCESS, }, status: HTTPStatus.OK, }); expect(generateUrlSpy).toHaveBeenCalledTimes(2); }); it('should return a partial success response when some URLs are invalid', async () => { // Arrange const reqBody = { url1: { url_name: 'valid_url1', url_data: { key1: 'value1' }, }, url2: { url_name: 'invalid_url', url_data: { key2: 'value2' }, }, }; const ctx = createMockContext(reqBody); const generateUrlSpy = jest.spyOn(helpers, 'generateUrl').mockResolvedValue('http://example.com'); // Mock other required functions as needed // Act const result = await moduleToTest.getUrl(ctx); // Assert expect(result).toEqual({ data: { url_details: [ { url_name: 'valid_url1', status: constants.STATUS.SUCCESS, url: 'http://example.com' }, { url_name: 'invalid_url', status: constants.STATUS.FAILED, error: 'Unsupported url_name :invalid_url', }, ], response_status: constants.STATUS_PARTIAL.PARTIAL_SUCCESS, }, status: HTTPStatus.OK, }); expect(generateUrlSpy).toHaveBeenCalledTimes(1); }); it('should throw an error when all URLs are invalid', async () => { // Arrange const reqBody = { url1: { url_name: 'invalid_url1', url_data: { key1: 'value1' }, }, url2: { url_name: 'invalid_url2', url_data: { key2: 'value2' }, }, }; const ctx = createMockContext(reqBody); const generateUrlSpy = jest.spyOn(helpers, 'generateUrl').mockResolvedValue(undefined); // Mock other required functions as needed // Act and Assert await expect(moduleToTest.getUrl(ctx)).rejects.toThrowError(ZodeError); expect(generateUrlSpy).toHaveBeenCalledTimes(2); }); it('should throw an error when reqBody is empty', async () => { // Arrange const reqBody = {}; const ctx = createMockContext(reqBody); // Act and Assert await expect(moduleToTest.getUrl(ctx)).rejects.toThrowError(ZodeError); }); });
Editor is loading...