@nestjsvn/swagger-sse
Version:
OpenAPI documentation and interactive Swagger UI for NestJS Server-Sent Events endpoints
170 lines (169 loc) • 7.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const setup_1 = require("../../ui/setup");
const swagger_1 = require("@nestjs/swagger");
// Mock dependencies
jest.mock('@nestjs/swagger');
jest.mock('express', () => ({
static: jest.fn(() => 'mock-static-middleware'),
}));
jest.mock('path', () => ({
join: jest.fn((...args) => args.join('/')),
}));
// eslint-disable-next-line @typescript-eslint/unbound-method
const mockSwaggerModuleSetup = swagger_1.SwaggerModule.setup;
describe('setupSsePlugin', () => {
let mockApp;
let mockDocument;
beforeEach(() => {
mockApp = {
use: jest.fn(),
};
mockDocument = {
openapi: '3.0.0',
info: { title: 'Test API', version: '1.0.0' },
paths: {
'/events/stream': {
get: {
'x-sse-endpoint': true,
},
},
},
};
mockSwaggerModuleSetup.mockClear();
});
it('should setup SSE plugin with default options', () => {
(0, setup_1.setupSsePlugin)(mockApp, mockDocument, 'api-docs');
// eslint-disable-next-line @typescript-eslint/unbound-method
expect(mockApp.use).toHaveBeenCalledWith('/swagger-sse-assets', 'mock-static-middleware');
expect(mockSwaggerModuleSetup).toHaveBeenCalledWith('api-docs', mockApp, mockDocument, expect.objectContaining({
swaggerOptions: expect.objectContaining({
plugins: expect.arrayContaining([expect.any(Function)]),
}),
customJs: expect.arrayContaining(['/swagger-sse-assets/sse-plugin.js']),
customCssUrl: expect.arrayContaining(['/swagger-sse-assets/sse-plugin.css']),
}));
});
it('should merge with existing options', () => {
const existingOptions = {
swaggerOptions: {
plugins: ['existing-plugin'],
},
customJs: ['existing.js'],
customCssUrl: ['existing.css'],
};
(0, setup_1.setupSsePlugin)(mockApp, mockDocument, 'api-docs', existingOptions);
expect(mockSwaggerModuleSetup).toHaveBeenCalledWith('api-docs', mockApp, mockDocument, expect.objectContaining({
swaggerOptions: expect.objectContaining({
plugins: expect.arrayContaining(['existing-plugin', expect.any(Function)]),
}),
customJs: expect.arrayContaining(['existing.js', '/swagger-sse-assets/sse-plugin.js']),
customCssUrl: expect.arrayContaining([
'existing.css',
'/swagger-sse-assets/sse-plugin.css',
]),
}));
});
it('should preserve other swagger options', () => {
const existingOptions = {
swaggerOptions: {
docExpansion: 'none',
filter: true,
},
customSiteTitle: 'My API',
};
(0, setup_1.setupSsePlugin)(mockApp, mockDocument, 'api-docs', existingOptions);
expect(mockSwaggerModuleSetup).toHaveBeenCalledWith('api-docs', mockApp, mockDocument, expect.objectContaining({
swaggerOptions: expect.objectContaining({
docExpansion: 'none',
filter: true,
plugins: expect.any(Array),
}),
customSiteTitle: 'My API',
}));
});
});
describe('setupSsePluginAdvanced', () => {
let mockApp;
let mockDocument;
beforeEach(() => {
mockApp = {
use: jest.fn(),
};
mockDocument = {
openapi: '3.0.0',
info: { title: 'Test API', version: '1.0.0' },
paths: {
'/events/stream': {
get: {
'x-sse-endpoint': true,
},
},
},
};
mockSwaggerModuleSetup.mockClear();
});
it('should setup with default SSE configuration', () => {
(0, setup_1.setupSsePluginAdvanced)(mockApp, mockDocument, 'api-docs');
expect(mockSwaggerModuleSetup).toHaveBeenCalledWith('api-docs', mockApp, mockDocument, expect.objectContaining({
customSiteTitle: 'API Documentation with SSE Support',
explorer: true,
customJs: expect.arrayContaining([expect.stringContaining('window.SSE_PLUGIN_CONFIG')]),
}));
});
it('should use custom SSE configuration', () => {
const customOptions = {
sseConfig: {
enableEventFiltering: false,
enableEventSearch: false,
maxEventLogSize: 500,
enableExport: false,
enableReconnection: false,
reconnectionDelay: 5000,
},
};
(0, setup_1.setupSsePluginAdvanced)(mockApp, mockDocument, 'api-docs', customOptions);
const setupCall = mockSwaggerModuleSetup.mock.calls[0];
const options = setupCall[3];
const configScript = options?.customJs?.[options.customJs.length - 1];
expect(configScript).toContain('"enableEventFiltering":false');
expect(configScript).toContain('"enableEventSearch":false');
expect(configScript).toContain('"maxEventLogSize":500');
expect(configScript).toContain('"enableExport":false');
expect(configScript).toContain('"enableReconnection":false');
expect(configScript).toContain('"reconnectionDelay":5000');
});
it('should merge SSE config with defaults', () => {
const customOptions = {
sseConfig: {
maxEventLogSize: 2000,
reconnectionDelay: 1000,
},
};
(0, setup_1.setupSsePluginAdvanced)(mockApp, mockDocument, 'api-docs', customOptions);
const setupCall = mockSwaggerModuleSetup.mock.calls[0];
const options = setupCall[3];
const configScript = options?.customJs?.[options.customJs.length - 1];
// Should have custom values
expect(configScript).toContain('"maxEventLogSize":2000');
expect(configScript).toContain('"reconnectionDelay":1000');
// Should have default values for unspecified options
expect(configScript).toContain('"enableEventFiltering":true');
expect(configScript).toContain('"enableEventSearch":true');
expect(configScript).toContain('"enableExport":true');
expect(configScript).toContain('"enableReconnection":true');
});
it('should handle custom site title and other options', () => {
const customOptions = {
customSiteTitle: 'Custom API Docs',
customfavIcon: '/favicon.ico',
explorer: false,
};
(0, setup_1.setupSsePluginAdvanced)(mockApp, mockDocument, 'api-docs', customOptions);
expect(mockSwaggerModuleSetup).toHaveBeenCalledWith('api-docs', mockApp, mockDocument, expect.objectContaining({
customSiteTitle: 'Custom API Docs',
customfavIcon: '/favicon.ico',
explorer: false,
}));
});
});