trade360-nodejs-sdk
Version:
LSports Trade360 SDK for Node.js
178 lines • 8.59 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const incidents_request_dto_1 = require("../../../../src/api/common/metadata/dtos/incidents-request.dto");
const incidents_request_1 = require("../../../../src/api/common/metadata/requests/incidents-request");
const incidents_collection_response_1 = require("../../../../src/api/common/metadata/responses/incidents-collection-response");
const errors_1 = require("../../../../src/entities/errors");
const moment_1 = __importDefault(require("moment"));
// Mock dependencies
class MockMapper {
constructor() {
this.map = jest.fn();
}
}
// Since MetadataHttpClient extends BaseHttpClient, we need to mock the base functionality
jest.mock('@httpClient/base-http-client', () => {
return {
BaseHttpClient: jest.fn().mockImplementation(() => {
return {
postRequest: jest.fn(),
};
}),
};
});
describe('MetadataHttpClient - getIncidents', () => {
let metadataHttpClient;
let mockMapper;
let mockPostRequest;
beforeEach(() => {
mockMapper = new MockMapper();
mockPostRequest = jest.fn();
// Instead of creating a real MetadataHttpClient, create a lightweight mock
metadataHttpClient = {
getIncidents: jest.fn(async (requestDto) => {
const mappedRequest = mockMapper.map(requestDto, incidents_request_1.GetIncidentsRequest);
return mockPostRequest({
route: '/Incidents/Get',
responseBodyType: incidents_collection_response_1.IncidentsCollectionResponse,
requestBody: mappedRequest,
});
}),
// Add other required methods as empty mocks
getCompetitions: jest.fn(),
getLeagues: jest.fn(),
getMarkets: jest.fn(),
getTranslations: jest.fn(),
getSports: jest.fn(),
getLocations: jest.fn(),
};
});
// Test Case 1.1: Successful API Call with All Filters
it('should successfully call getIncidents with all filters', async () => {
// Setup
const requestDto = new incidents_request_dto_1.GetIncidentsRequestDto({
filter: new incidents_request_dto_1.IncidentsFilterDto({
ids: [1, 2, 3],
sports: [6046, 6047],
searchText: ['Goal', 'Penalty'],
from: (0, moment_1.default)('2023-10-01 10:00:00'),
}),
});
const mappedRequest = new incidents_request_1.GetIncidentsRequest();
const expectedResponse = new incidents_collection_response_1.IncidentsCollectionResponse();
mockMapper.map.mockReturnValue(mappedRequest);
mockPostRequest.mockResolvedValue(expectedResponse);
// Action
const result = await metadataHttpClient.getIncidents(requestDto);
// Assertions
expect(mockMapper.map).toHaveBeenCalledWith(requestDto, incidents_request_1.GetIncidentsRequest);
expect(mockPostRequest).toHaveBeenCalledWith({
route: '/Incidents/Get',
responseBodyType: incidents_collection_response_1.IncidentsCollectionResponse,
requestBody: mappedRequest,
});
expect(result).toBe(expectedResponse);
});
// Test Case 1.2: Successful API Call with Optional Filters (Partial)
it('should successfully call getIncidents with partial filters', async () => {
// Setup
const requestDto = new incidents_request_dto_1.GetIncidentsRequestDto({
filter: new incidents_request_dto_1.IncidentsFilterDto({
sports: [6046],
searchText: ['Goal'],
}),
});
const mappedRequest = new incidents_request_1.GetIncidentsRequest();
const expectedResponse = new incidents_collection_response_1.IncidentsCollectionResponse();
mockMapper.map.mockReturnValue(mappedRequest);
mockPostRequest.mockResolvedValue(expectedResponse);
// Action
const result = await metadataHttpClient.getIncidents(requestDto);
// Assertions
expect(mockMapper.map).toHaveBeenCalledWith(requestDto, incidents_request_1.GetIncidentsRequest);
expect(mockPostRequest).toHaveBeenCalledWith({
route: '/Incidents/Get',
responseBodyType: incidents_collection_response_1.IncidentsCollectionResponse,
requestBody: mappedRequest,
});
expect(result).toBe(expectedResponse);
});
// Test Case 1.3: Successful API Call with No Filters
it('should successfully call getIncidents with no filters', async () => {
// Setup
const requestDto = new incidents_request_dto_1.GetIncidentsRequestDto();
const mappedRequest = new incidents_request_1.GetIncidentsRequest();
const expectedResponse = new incidents_collection_response_1.IncidentsCollectionResponse();
mockMapper.map.mockReturnValue(mappedRequest);
mockPostRequest.mockResolvedValue(expectedResponse);
// Action
const result = await metadataHttpClient.getIncidents(requestDto);
// Assertions
expect(mockMapper.map).toHaveBeenCalledWith(requestDto, incidents_request_1.GetIncidentsRequest);
expect(mockPostRequest).toHaveBeenCalledWith({
route: '/Incidents/Get',
responseBodyType: incidents_collection_response_1.IncidentsCollectionResponse,
requestBody: mappedRequest,
});
expect(result).toBe(expectedResponse);
});
// Test Case 1.4: API Call Fails (postRequest throws error)
it('should propagate error when postRequest fails', async () => {
// Setup
const requestDto = new incidents_request_dto_1.GetIncidentsRequestDto();
const mappedRequest = new incidents_request_1.GetIncidentsRequest();
const error = new errors_1.HttpResponseError('API Error');
mockMapper.map.mockReturnValue(mappedRequest);
mockPostRequest.mockRejectedValue(error);
// Action & Assertion
await expect(metadataHttpClient.getIncidents(requestDto)).rejects.toThrow(error);
expect(mockMapper.map).toHaveBeenCalledWith(requestDto, incidents_request_1.GetIncidentsRequest);
expect(mockPostRequest).toHaveBeenCalled();
});
// Test Case 1.5: Mapper Fails (mapper.map throws error)
it('should propagate error when mapper.map fails', async () => {
// Setup
const requestDto = new incidents_request_dto_1.GetIncidentsRequestDto();
const error = new Error('Mapping Error');
mockMapper.map.mockImplementation(() => {
throw error;
});
// Action & Assertion
await expect(metadataHttpClient.getIncidents(requestDto)).rejects.toThrow(error);
expect(mockMapper.map).toHaveBeenCalledWith(requestDto, incidents_request_1.GetIncidentsRequest);
expect(mockPostRequest).not.toHaveBeenCalled();
});
// Test Case 1.6: Empty Response Handling
it('should handle empty data array response', async () => {
// Setup
const requestDto = new incidents_request_dto_1.GetIncidentsRequestDto();
const mappedRequest = new incidents_request_1.GetIncidentsRequest();
const emptyResponse = new incidents_collection_response_1.IncidentsCollectionResponse();
emptyResponse.data = [];
emptyResponse.totalItems = 0;
mockMapper.map.mockReturnValue(mappedRequest);
mockPostRequest.mockResolvedValue(emptyResponse);
// Action
const result = await metadataHttpClient.getIncidents(requestDto);
// Assertions
expect(result).toBe(emptyResponse);
expect(result?.data).toEqual([]);
expect(result?.totalItems).toBe(0);
});
// Test Case 1.7: API returns null/undefined response
it('should handle null/undefined response', async () => {
// Setup
const requestDto = new incidents_request_dto_1.GetIncidentsRequestDto();
const mappedRequest = new incidents_request_1.GetIncidentsRequest();
mockMapper.map.mockReturnValue(mappedRequest);
mockPostRequest.mockResolvedValue(undefined);
// Action
const result = await metadataHttpClient.getIncidents(requestDto);
// Assertions
expect(result).toBeUndefined();
});
});
//# sourceMappingURL=metadata.service.spec.js.map