Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion __tests__/currencyRoutes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,4 @@ describe('Currency Routes', () => {
expect(mockedCurrencyService.convertCurrency).not.toHaveBeenCalled();
});
});
});
});
165 changes: 165 additions & 0 deletions __tests__/transactionRoutes.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import { jest } from '@jest/globals';
import request from 'supertest';
import express from 'express';
import transactionRoutes from '../src/routes/transactionRoutes.js';
import * as transactionServiceModule from '../src/services/transactionService.js';

const mockedTransactionService = transactionServiceModule.default;

// Mock service methods
mockedTransactionService.createTransaction = jest.fn();
mockedTransactionService.getAllTransactions = jest.fn();
mockedTransactionService.getTransactionById = jest.fn();
mockedTransactionService.updateTransactionStatus = jest.fn();

let app;
let server;

beforeAll(() => {
app = express();
app.use(express.json());
app.use('/api/transactions', transactionRoutes);
server = app.listen(0);
});

afterAll(async () => {
await new Promise((resolve) => server.close(resolve));
});

describe('Transaction Routes', () => {
beforeEach(() => {
jest.clearAllMocks();
});

describe('POST /api/transactions', () => {
it('should create a new transaction successfully', async () => {
const validTransactionData = {
amount: 100,
currency: 'USD',
sender_id: '123e4567-e89b-12d3-a456-426614174000',
receiver_id: '123e4567-e89b-12d3-a456-426614174001',
txn_type: 'transfer'
};

const mockResponse = {
success: true,
data: {
...validTransactionData,
id: 1,
txn_status: 'pending',
createdAt: '2023-01-01T00:00:00.000Z'
}
};

mockedTransactionService.createTransaction.mockResolvedValue(mockResponse);

const response = await request(app)
.post('/api/transactions')
.send(validTransactionData)
.expect('Content-Type', /json/)
.expect(201);

expect(response.body).toEqual(mockResponse);
expect(mockedTransactionService.createTransaction).toHaveBeenCalledWith(validTransactionData);
});

it('should return 400 for invalid transaction data', async () => {
const invalidTransactionData = {
amount: 'not-a-number',
currency: 'US',
sender_id: 'invalid',
txn_type: 'invalid-type'
};

const response = await request(app)
.post('/api/transactions')
.send(invalidTransactionData)
.expect('Content-Type', /json/)
.expect(400);

expect(response.body).toHaveProperty('error');
expect(response.body).toHaveProperty('details');
expect(mockedTransactionService.createTransaction).not.toHaveBeenCalled();
});
});

describe('GET /api/transactions', () => {
it('should fetch all transactions successfully', async () => {
const mockResponse = {
success: true,
data: [
{ id: 1, amount: 100, currency: 'USD' },
{ id: 2, amount: 200, currency: 'EUR' }
]
};

mockedTransactionService.getAllTransactions.mockResolvedValue(mockResponse);

const response = await request(app)
.get('/api/transactions')
.expect('Content-Type', /json/)
.expect(200);

expect(response.body).toEqual(mockResponse);
expect(mockedTransactionService.getAllTransactions).toHaveBeenCalled();
});

it('should return empty array when no transactions exist', async () => {
const mockResponse = {
success: true,
data: []
};

mockedTransactionService.getAllTransactions.mockResolvedValue(mockResponse);

const response = await request(app)
.get('/api/transactions')
.expect('Content-Type', /json/)
.expect(200);

expect(response.body.data).toEqual([]);
});
});

describe('GET /api/transactions/:id', () => {
it('should fetch a single transaction by ID', async () => {
const mockResponse = {
success: true,
data: {
id: 1,
amount: 100,
currency: 'USD',
txn_status: 'completed'
}
};

mockedTransactionService.getTransactionById.mockResolvedValue(mockResponse);

const response = await request(app)
.get('/api/transactions/1')
.expect('Content-Type', /json/)
.expect(200);

expect(response.body).toEqual(mockResponse);
expect(mockedTransactionService.getTransactionById).toHaveBeenCalledWith('1');
});

it('should return 404 for non-existent transaction', async () => {
const mockResponse = {
success: false,
error: 'Transaction not found',
status: 404
};

mockedTransactionService.getTransactionById.mockResolvedValue(mockResponse);

const response = await request(app)
.get('/api/transactions/999')
.expect('Content-Type', /json/)
.expect(404);

expect(response.body.error).toBe('Transaction not found');
});
});

});
187 changes: 187 additions & 0 deletions __tests__/transactionService.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
import { jest } from '@jest/globals';

// Mock the entire service module
jest.mock(process.cwd() + '/src/services/transactionService.js', () => {
const mockService = {
createTransaction: jest.fn(),
getAllTransactions: jest.fn(),
getTransactionById: jest.fn(),
updateTransactionStatus: jest.fn()
};
return {
__esModule: true,
default: mockService
};
});

import transactionService from '../src/services/transactionService.js';

// Ensure the methods are Jest mocks
transactionService.createTransaction = jest.fn();
transactionService.getAllTransactions = jest.fn();
transactionService.getTransactionById = jest.fn();
transactionService.updateTransactionStatus = jest.fn();

describe('TransactionService', () => {
beforeEach(() => {
jest.clearAllMocks();
});

describe('createTransaction', () => {
it('should successfully create a transaction', async () => {
const mockTransactionData = {
amount: 100,
currency: 'USD',
sender_id: 'sender123',
receiver_id: 'receiver456',
txn_type: 'transfer'
};

const mockResponse = {
success: true,
data: {
...mockTransactionData,
id: 1,
txn_status: 'pending',
createdAt: '2023-01-01T00:00:00.000Z'
}
};

transactionService.createTransaction.mockResolvedValue(mockResponse);

const result = await transactionService.createTransaction(mockTransactionData);

expect(result.success).toBe(true);
expect(result.data).toEqual(mockResponse.data);
expect(transactionService.createTransaction).toHaveBeenCalledWith(mockTransactionData);
});

it('should handle creation errors', async () => {
const mockError = {
success: false,
error: 'Failed to create transaction'
};

transactionService.createTransaction.mockResolvedValue(mockError);

const result = await transactionService.createTransaction({});

expect(result.success).toBe(false);
expect(result.error).toBe('Failed to create transaction');
});
});

describe('getAllTransactions', () => {
it('should fetch all transactions successfully', async () => {
const mockResponse = {
success: true,
data: [
{ id: 1, amount: 100 },
{ id: 2, amount: 200 }
]
};

transactionService.getAllTransactions.mockResolvedValue(mockResponse);

const result = await transactionService.getAllTransactions();

expect(result.success).toBe(true);
expect(result.data.length).toBe(2);
expect(transactionService.getAllTransactions).toHaveBeenCalled();
});

it('should handle fetch errors', async () => {
const mockError = {
success: false,
error: 'Failed to fetch transactions'
};

transactionService.getAllTransactions.mockResolvedValue(mockError);

const result = await transactionService.getAllTransactions();

expect(result.success).toBe(false);
expect(result.error).toBe('Failed to fetch transactions');
});
});

describe('getTransactionById', () => {
it('should fetch a transaction by ID successfully', async () => {
const mockResponse = {
success: true,
data: {
id: 1,
amount: 100,
currency: 'USD'
}
};

transactionService.getTransactionById.mockResolvedValue(mockResponse);

const result = await transactionService.getTransactionById(1);

expect(result.success).toBe(true);
expect(result.data.id).toBe(1);
expect(transactionService.getTransactionById).toHaveBeenCalledWith(1);
});

it('should handle transaction not found', async () => {
const mockError = {
success: false,
error: 'Transaction not found',
status: 404
};

transactionService.getTransactionById.mockResolvedValue(mockError);

const result = await transactionService.getTransactionById(999);

expect(result.success).toBe(false);
expect(result.error).toBe('Transaction not found');
expect(result.status).toBe(404);
});
});

describe('updateTransactionStatus', () => {
it('should update transaction status successfully', async () => {
const mockResponse = {
success: true,
data: {
id: 1,
txn_status: 'completed',
confirmed_at: '2023-01-01T00:00:00.000Z'
}
};

transactionService.updateTransactionStatus.mockResolvedValue(mockResponse);

const result = await transactionService.updateTransactionStatus(1, {
txn_status: 'completed',
confirmed_at: '2023-01-01T00:00:00.000Z'
});

expect(result.success).toBe(true);
expect(result.data.txn_status).toBe('completed');
expect(transactionService.updateTransactionStatus).toHaveBeenCalledWith(1, {
txn_status: 'completed',
confirmed_at: '2023-01-01T00:00:00.000Z'
});
});

it('should handle transaction not found during update', async () => {
const mockError = {
success: false,
error: 'Transaction not found',
status: 404
};

transactionService.updateTransactionStatus.mockResolvedValue(mockError);

const result = await transactionService.updateTransactionStatus(999, {});

expect(result.success).toBe(false);
expect(result.error).toBe('Transaction not found');
expect(result.status).toBe(404);
});
});
});
Loading