Mock Class
import DataWarehouseService from '../../services/data-warehouse.service';
import { fetchIndividualIssueFromDwh } from './use-individual-issues.query';
jest.mock('../../services/data-warehouse.service');
describe('fetchIndividualIssueFromDwh', () => {
const token = 'token';
const tableMetadata = {
page: 1,
limit: 10,
order: {},
filters: {},
};
beforeEach(() => {
jest.clearAllMocks();
});
it('should fetch individual issues from data warehouse', async () => {
const spyFetch = jest.spyOn(
DataWarehouseService.prototype as any,
'getIndividualIssues',
);
const count = 5;
const data = [
{
issue_id: 1,
asset_id: 1,
asset_name: 'example.com',
port: 80,
service_name: 'HTTP',
protocol: 'TCP',
status: 'Open',
engine_source: 'Nessus',
scan_type: 'External',
origin: 'Scan',
title: 'Vulnerability',
severity: 'High',
last_detected_at: '2022-01-01',
hostnames: ['example.com'],
banner: 'Apache HTTP Server',
script_output_id: '123',
assignees: 'John,Doe',
tags: 'Tag1,Tag2',
network_name: 'Network',
network_id: 1,
cve: 'CVE-2022-1234',
},
];
spyFetch.mockResolvedValueOnce({
count,
data,
});
const result = await fetchIndividualIssueFromDwh(token, tableMetadata);
expect(spyFetch).toHaveBeenCalledWith(token, {
page: tableMetadata.page,
limit: tableMetadata.limit,
count: true,
sort: { issue_id: 'asc' },
filter: {},
});
expect(result).toEqual({
totalIssues: count,
issues: [
{
id: 1,
issue_id: 1,
asset_id: 1,
source_id: '',
ip: 'example.com',
port: 80,
service: 'HTTP',
protocol: 'TCP',
status: 'Open',
origin_source: 'Nessus',
origin_type: 'External',
origin: 'Scan',
title: 'Vulnerability',
severity: 'High',
last_detected: '2022-01-01',
dns: ['example.com'],
banner: 'Apache HTTP Server',
script_output: '123',
script_output_details: null,
assignees: ['John', 'Doe'],
tags: ['Tag1', 'Tag2'],
network_name: 'Network',
network_id: 1,
cve: 'CVE-2022-1234',
},
],
});
});
});
Mock Service Class
import OrgManagementService from "./org-management-service";
describe('OrgManagementService', () => {
let service: any;
const mockToken = 'token';
const mockOrgId = 'orgId';
beforeEach(() => {
service = new OrgManagementService();
});
it('getOrganisationById should return mock data', async () => {
const mockData = { id: mockOrgId, name: 'Test Org' };
jest
.spyOn((service as any).authorized('token'), 'get')
.mockResolvedValue({ data: mockData });
const result = await (service as OrgManagementService).getOrganisationById(mockToken, mockOrgId);
expect(result).toEqual(mockData);
});
});