|
| 1 | +import { beforeEach, expect, spyOn, test } from 'bun:test' |
| 2 | +import { join } from 'node:path' |
| 3 | +import type { DevupApiOptions } from '@devup-api/core' |
| 4 | +import * as generator from '@devup-api/generator' |
| 5 | +import * as utils from '@devup-api/utils' |
| 6 | +import { devupApi } from '../plugin' |
| 7 | + |
| 8 | +let mockCreateTmpDirAsync: ReturnType<typeof spyOn> |
| 9 | +let mockReadOpenapiAsync: ReturnType<typeof spyOn> |
| 10 | +let mockWriteInterfaceAsync: ReturnType<typeof spyOn> |
| 11 | +let mockCreateUrlMap: ReturnType<typeof spyOn> |
| 12 | +let mockGenerateInterface: ReturnType<typeof spyOn> |
| 13 | + |
| 14 | +const mockSchema = { |
| 15 | + openapi: '3.1.0', |
| 16 | + paths: { |
| 17 | + '/users': { |
| 18 | + get: { |
| 19 | + operationId: 'getUsers', |
| 20 | + responses: { |
| 21 | + '200': { |
| 22 | + content: { |
| 23 | + 'application/json': { |
| 24 | + schema: { |
| 25 | + type: 'array', |
| 26 | + items: { type: 'string' }, |
| 27 | + }, |
| 28 | + }, |
| 29 | + }, |
| 30 | + }, |
| 31 | + }, |
| 32 | + }, |
| 33 | + }, |
| 34 | + }, |
| 35 | +} as const |
| 36 | + |
| 37 | +const mockUrlMap = { |
| 38 | + getUsers: { |
| 39 | + method: 'GET' as const, |
| 40 | + url: '/users', |
| 41 | + }, |
| 42 | + '/users': { |
| 43 | + method: 'GET' as const, |
| 44 | + url: '/users', |
| 45 | + }, |
| 46 | +} |
| 47 | + |
| 48 | +const mockInterfaceContent = 'export interface Test {}' |
| 49 | + |
| 50 | +beforeEach(() => { |
| 51 | + mockCreateTmpDirAsync = spyOn(utils, 'createTmpDirAsync').mockResolvedValue( |
| 52 | + 'df', |
| 53 | + ) |
| 54 | + mockReadOpenapiAsync = spyOn(utils, 'readOpenapiAsync').mockResolvedValue( |
| 55 | + mockSchema as never, |
| 56 | + ) |
| 57 | + mockWriteInterfaceAsync = spyOn( |
| 58 | + utils, |
| 59 | + 'writeInterfaceAsync', |
| 60 | + ).mockResolvedValue(undefined) |
| 61 | + mockCreateUrlMap = spyOn(generator, 'createUrlMap').mockReturnValue( |
| 62 | + mockUrlMap as never, |
| 63 | + ) |
| 64 | + mockGenerateInterface = spyOn(generator, 'generateInterface').mockReturnValue( |
| 65 | + mockInterfaceContent, |
| 66 | + ) |
| 67 | +}) |
| 68 | + |
| 69 | +test('devupApi returns plugin with correct name', () => { |
| 70 | + const plugin = devupApi() |
| 71 | + expect(plugin.name).toBe('devup-api') |
| 72 | +}) |
| 73 | + |
| 74 | +test.each([ |
| 75 | + [undefined], |
| 76 | + [{ tempDir: 'custom-dir' }], |
| 77 | + [{ openapiFile: 'custom-openapi.json' }], |
| 78 | + [ |
| 79 | + { |
| 80 | + tempDir: 'custom-dir', |
| 81 | + openapiFile: 'custom-openapi.json', |
| 82 | + convertCase: 'snake' as const, |
| 83 | + }, |
| 84 | + ], |
| 85 | +] as const)('devupApi returns plugin with config hook: %s', async (options: |
| 86 | + | DevupApiOptions |
| 87 | + | undefined) => { |
| 88 | + const plugin = devupApi(options) |
| 89 | + expect(plugin.config).toBeDefined() |
| 90 | + expect(typeof plugin.config).toBe('function') |
| 91 | + |
| 92 | + const result = await plugin.config?.() |
| 93 | + expect(mockReadOpenapiAsync).toHaveBeenCalledWith(options?.openapiFile) |
| 94 | + expect(mockCreateUrlMap).toHaveBeenCalledWith(mockSchema, options) |
| 95 | + expect(result).toEqual({ |
| 96 | + define: { |
| 97 | + 'process.env.DEVUP_API_URL_MAP': JSON.stringify( |
| 98 | + JSON.stringify(mockUrlMap), |
| 99 | + ), |
| 100 | + }, |
| 101 | + }) |
| 102 | +}) |
| 103 | + |
| 104 | +test('devupApi config hook returns empty define when urlMap is null', async () => { |
| 105 | + mockCreateUrlMap.mockReturnValue(null as never) |
| 106 | + const plugin = devupApi() |
| 107 | + const result = await plugin.config?.() |
| 108 | + expect(result).toEqual({ |
| 109 | + define: {}, |
| 110 | + }) |
| 111 | +}) |
| 112 | + |
| 113 | +test('devupApi config hook returns empty define when urlMap is undefined', async () => { |
| 114 | + mockCreateUrlMap.mockReturnValue(undefined as never) |
| 115 | + const plugin = devupApi() |
| 116 | + const result = await plugin.config?.() |
| 117 | + expect(result).toEqual({ |
| 118 | + define: {}, |
| 119 | + }) |
| 120 | +}) |
| 121 | + |
| 122 | +test.each([ |
| 123 | + [undefined], |
| 124 | + [{ tempDir: 'custom-dir' }], |
| 125 | + [{ openapiFile: 'custom-openapi.json' }], |
| 126 | + [ |
| 127 | + { |
| 128 | + tempDir: 'custom-dir', |
| 129 | + openapiFile: 'custom-openapi.json', |
| 130 | + convertCase: 'pascal' as const, |
| 131 | + }, |
| 132 | + ], |
| 133 | +] as const)('devupApi returns plugin with configResolved hook: %s', async (options: |
| 134 | + | DevupApiOptions |
| 135 | + | undefined) => { |
| 136 | + const plugin = devupApi(options) |
| 137 | + expect(plugin.configResolved).toBeDefined() |
| 138 | + expect(typeof plugin.configResolved).toBe('function') |
| 139 | + |
| 140 | + await plugin.configResolved?.() |
| 141 | + expect(mockCreateTmpDirAsync).toHaveBeenCalledWith(options?.tempDir) |
| 142 | + expect(mockReadOpenapiAsync).toHaveBeenCalledWith(options?.openapiFile) |
| 143 | + expect(mockGenerateInterface).toHaveBeenCalledWith(mockSchema, options) |
| 144 | + expect(mockWriteInterfaceAsync).toHaveBeenCalledWith( |
| 145 | + join('df', 'api.d.ts'), |
| 146 | + mockInterfaceContent, |
| 147 | + ) |
| 148 | +}) |
| 149 | + |
| 150 | +test('devupApi plugin has both config and configResolved hooks', () => { |
| 151 | + const plugin = devupApi() |
| 152 | + expect(plugin.config).toBeDefined() |
| 153 | + expect(plugin.configResolved).toBeDefined() |
| 154 | + expect(typeof plugin.config).toBe('function') |
| 155 | + expect(typeof plugin.configResolved).toBe('function') |
| 156 | +}) |
0 commit comments