|
| 1 | +// __tests__/OpenAPIHelper.test.js |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const { execSync } = require('child_process'); |
| 5 | +const OpenAPIHelper = require('../src/OpenAPIHelper'); |
| 6 | + |
| 7 | +jest.mock('fs'); |
| 8 | +jest.mock('child_process'); |
| 9 | + |
| 10 | +describe('OpenAPIHelper', () => { |
| 11 | + beforeEach(() => { |
| 12 | + jest.clearAllMocks(); |
| 13 | + }); |
| 14 | + |
| 15 | + describe('convertOpenAPIToPostman', () => { |
| 16 | + it('should call execSync with the correct command', () => { |
| 17 | + const openAPIPath = 'openapi.json'; |
| 18 | + const postmanOutputPath = '/tmp/openapi_schema_postman.json'; |
| 19 | + |
| 20 | + OpenAPIHelper.convertOpenAPIToPostman(openAPIPath, postmanOutputPath); |
| 21 | + |
| 22 | + expect(execSync).toHaveBeenCalledWith( |
| 23 | + `openapi2postmanv2 -s ${openAPIPath} -o ${postmanOutputPath}`, |
| 24 | + { stdio: 'inherit' } |
| 25 | + ); |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + describe('generateSampleCode', () => { |
| 30 | + it('should generate code for all languages and variants if no specific language and variant are provided', () => { |
| 31 | + const postmanCollectionPath = '/tmp/postman_collection.json'; |
| 32 | + const collection = { |
| 33 | + item: [ |
| 34 | + { |
| 35 | + request: { |
| 36 | + name: 'Get Users', |
| 37 | + method: 'GET', |
| 38 | + url: { path: ['api', 'v1', 'users'] } |
| 39 | + } |
| 40 | + } |
| 41 | + ] |
| 42 | + }; |
| 43 | + |
| 44 | + fs.readFileSync.mockReturnValue(JSON.stringify(collection)); |
| 45 | + fs.existsSync.mockReturnValue(false); |
| 46 | + fs.mkdirSync.mockReturnValue(); |
| 47 | + |
| 48 | + OpenAPIHelper.generateSampleCode(postmanCollectionPath); |
| 49 | + |
| 50 | + expect(fs.readFileSync).toHaveBeenCalledWith(postmanCollectionPath); |
| 51 | + expect(fs.mkdirSync).toHaveBeenCalledWith('/tmp/sample_code'); |
| 52 | + expect(fs.writeFileSync).toHaveBeenCalled(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should generate code for specific language and variant if provided', () => { |
| 56 | + const postmanCollectionPath = '/tmp/postman_collection.json'; |
| 57 | + const collection = { |
| 58 | + item: [ |
| 59 | + { |
| 60 | + request: { |
| 61 | + name: 'Get Users', |
| 62 | + method: 'GET', |
| 63 | + url: { path: ['api', 'v1', 'users'] } |
| 64 | + } |
| 65 | + } |
| 66 | + ] |
| 67 | + }; |
| 68 | + |
| 69 | + fs.readFileSync.mockReturnValue(JSON.stringify(collection)); |
| 70 | + fs.existsSync.mockReturnValue(true); |
| 71 | + |
| 72 | + OpenAPIHelper.generateSampleCode(postmanCollectionPath, 'nodejs', 'axios'); |
| 73 | + |
| 74 | + expect(fs.readFileSync).toHaveBeenCalledWith(postmanCollectionPath); |
| 75 | + expect(fs.writeFileSync).toHaveBeenCalled(); |
| 76 | + }); |
| 77 | + }); |
| 78 | +}); |
0 commit comments