Files
Online-Topup-V1/application/tests/cash-in-controller.test.js
T
2026-11-24 23:14:30 +07:00

43 lines
1.7 KiB
JavaScript

jest.mock('../helpers/helper', () => ({
getUniqueCode: jest.fn(),
startScheduler: jest.fn(),
finishScheduler: jest.fn(),
}));
jest.mock('../helpers/winston.logger', () => ({
logger: { info: jest.fn(), warn: jest.fn(), error: jest.fn() },
}));
jest.mock('../modules/cashin/cash-in-generation.service', () => ({
_generateCashIn: jest.fn(),
}));
jest.mock('../modules/cashin/cash-in-posting.service', () => jest.fn());
const helper = require('../helpers/helper');
const { _generateCashIn } = require('../modules/cashin/cash-in-generation.service');
const { apiGenerateCashIn } = require('../modules/cashin/cashin.controller');
test('permintaan Cash-in paralel mempertahankan kode dan hasil masing-masing', async () => {
let finishFirst;
helper.getUniqueCode.mockReturnValueOnce('job-a').mockReturnValueOnce('job-b');
helper.startScheduler.mockResolvedValueOnce({ c_execution_token: 'a' })
.mockResolvedValueOnce({ c_execution_token: 'b' });
helper.finishScheduler.mockImplementation((token) => {
if (token === 'a') return new Promise((resolve) => { finishFirst = resolve; });
});
_generateCashIn.mockImplementation(async (id) => [id]);
const firstResponse = { status: jest.fn().mockReturnThis(), json: jest.fn() };
const secondResponse = { status: jest.fn().mockReturnThis(), json: jest.fn() };
const first = apiGenerateCashIn({}, firstResponse);
await apiGenerateCashIn({}, secondResponse);
expect(finishFirst).toBeDefined();
finishFirst();
await first;
expect(firstResponse.json).toHaveBeenCalledWith({
code: '200', message: 'Succes posting', data: ['job-a'],
});
expect(secondResponse.json).toHaveBeenCalledWith({
code: '200', message: 'Succes posting', data: ['job-b'],
});
});