trade360-nodejs-sdk
Version:
LSports Trade360 SDK for Node.js
71 lines • 3.71 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const distribution_util_1 = require("../../src/utilities/distribution-util");
// Mocks for dependencies
const mockGetDistributionStatus = jest.fn();
const mockStartDistribution = jest.fn();
const mockStopDistribution = jest.fn();
const mockLogger = { debug: jest.fn(), warn: jest.fn() };
// Mock IPackageDistributionHttpClient
const mockPackageDistributionApi = {
getDistributionStatus: mockGetDistributionStatus,
startDistribution: mockStartDistribution,
stopDistribution: mockStopDistribution,
};
describe('DistributionUtil', () => {
beforeEach(() => {
jest.clearAllMocks();
// @ts-expect-error: Setting static property for test mock injection
distribution_util_1.DistributionUtil.packageDistributionApi = mockPackageDistributionApi;
// @ts-expect-error: Setting static property for test mock injection
distribution_util_1.DistributionUtil.logger = mockLogger;
});
describe('checkStatus', () => {
it('should call getDistributionStatus and return the result', async () => {
const status = { status: 'ok' };
mockGetDistributionStatus.mockResolvedValueOnce(status);
const result = await distribution_util_1.DistributionUtil.checkStatus();
expect(mockGetDistributionStatus).toHaveBeenCalled();
expect(result).toBe(status);
});
it('should throw if not initialized', async () => {
// @ts-expect-error: Simulate uninitialized static property for error test
distribution_util_1.DistributionUtil.packageDistributionApi = undefined;
await expect(distribution_util_1.DistributionUtil.checkStatus()).rejects.toThrow('initialize distribution api first!');
});
});
describe('start', () => {
it('should call startDistribution and log message', async () => {
const startResponse = { message: 'started' };
mockStartDistribution.mockResolvedValueOnce(startResponse);
const setTimeoutSpy = jest.spyOn(global, 'setTimeout').mockImplementation((fn) => {
fn();
return 0;
});
await distribution_util_1.DistributionUtil.start();
expect(mockStartDistribution).toHaveBeenCalled();
expect(mockLogger.debug).toHaveBeenCalledWith('started');
setTimeoutSpy.mockRestore();
});
it('should throw if not initialized', async () => {
// @ts-expect-error: Simulate uninitialized static property for error test
distribution_util_1.DistributionUtil.packageDistributionApi = undefined;
await expect(distribution_util_1.DistributionUtil.start()).rejects.toThrow('initialize distribution api first!');
});
});
describe('stop', () => {
it('should call stopDistribution and log message', async () => {
const stopResponse = { message: 'stopped' };
mockStopDistribution.mockResolvedValueOnce(stopResponse);
await distribution_util_1.DistributionUtil.stop();
expect(mockStopDistribution).toHaveBeenCalled();
expect(mockLogger.debug).toHaveBeenCalledWith('stopped');
});
it('should throw if not initialized', async () => {
// @ts-expect-error: Simulate uninitialized static property for error test
distribution_util_1.DistributionUtil.packageDistributionApi = undefined;
await expect(distribution_util_1.DistributionUtil.stop()).rejects.toThrow('initialize distribution api first!');
});
});
});
//# sourceMappingURL=distribution-util.spec.js.map