@apistudio/apim-cli
Version:
CLI for API Management Products
76 lines (59 loc) • 2.59 kB
text/typescript
// src/CommonRuntimeSDK.test.ts
// Mock the module before importing
jest.mock("@apic/inventory/src/runtime/RuntimeInventory.js", () => {
return {
RuntimeInventory: jest.fn().mockImplementation(() => ({
getPolicySequenceType: jest.fn().mockReturnValue({ id: 'test-runtime' })
}))
};
}, { virtual: true }); // Add virtual: true to create a virtual mock
// Now import the module that uses the mocked dependency
import { RuntimeSDK } from './CommonRuntimeSDK.js';
// Import the mocked constructor to access it in tests
const { RuntimeInventory } = jest.requireMock("@apic/inventory/src/runtime/RuntimeInventory.js");
describe('RuntimeSDK', () => {
let runtimeSDK: RuntimeSDK;
beforeEach(() => {
// Clear all mocks before each test
jest.clearAllMocks();
// Create a new instance of RuntimeSDK for each test
runtimeSDK = new RuntimeSDK();
});
describe('initialization', () => {
it('should create an instance of RuntimeSDK', () => {
expect(runtimeSDK).toBeInstanceOf(RuntimeSDK);
});
it('should initialize with a RuntimeInventory instance', () => {
expect(runtimeSDK.inventory).toBeDefined();
expect(RuntimeInventory).toHaveBeenCalledTimes(1);
});
});
describe('inventory property', () => {
it('should expose the inventory property', () => {
expect(runtimeSDK.inventory).toBeDefined();
});
it('should allow access to inventory methods', () => {
const mockRuntime = { id: 'test-runtime' };
// Call a method on the inventory
const runtime = runtimeSDK.inventory.getPolicySequenceType();
// Verify the method was called and returned the expected value
expect(runtimeSDK.inventory.getPolicySequenceType).toHaveBeenCalled();
expect(runtime).toEqual(mockRuntime);
});
it('should allow listing runtimes through inventory', () => {
const mockRuntimes = {"id": "test-runtime"};
// Call the listRuntimes method
const runtimes = runtimeSDK.inventory.getPolicySequenceType();
// Verify the method was called and returned the expected value
expect(runtimeSDK.inventory.getPolicySequenceType).toHaveBeenCalled();
expect(runtimes).toEqual(mockRuntimes);
});
});
describe('interface implementation', () => {
it('should implement the IRuntimeSDK interface', () => {
// This is more of a compile-time check, but we can verify
// that the required properties exist at runtime
expect(runtimeSDK).toHaveProperty('inventory');
});
});
});