|
| 1 | +import * as ts from 'typescript'; |
| 2 | +import { describe, expect, test } from 'vitest'; |
| 3 | +import { |
| 4 | + returnStringServiceCode, |
| 5 | + hasResponseBodyServiceCode, |
| 6 | + needResponseBodyServiceCode, |
| 7 | + notPromiseServiceCode, |
| 8 | +} from '../../fixtures/fake-service-code'; |
| 9 | +import { before } from '../compiler-plugin'; |
| 10 | + |
| 11 | +describe('HttpInterfaceVisitor', () => { |
| 12 | + test('should ignore if file name is not match', () => { |
| 13 | + // given |
| 14 | + const options: ts.CompilerOptions = { |
| 15 | + module: ts.ModuleKind.ES2020, |
| 16 | + target: ts.ScriptTarget.ES2020, |
| 17 | + newLine: ts.NewLineKind.LineFeed, |
| 18 | + noEmitHelpers: true, |
| 19 | + experimentalDecorators: true, |
| 20 | + strict: true, |
| 21 | + }; |
| 22 | + const filename = 'not-match.ts'; |
| 23 | + const fakeProgram = ts.createProgram([filename], options); |
| 24 | + |
| 25 | + // when |
| 26 | + const result = ts.transpileModule(needResponseBodyServiceCode, { |
| 27 | + compilerOptions: options, |
| 28 | + fileName: filename, |
| 29 | + transformers: { before: [before({}, fakeProgram)] }, |
| 30 | + }); |
| 31 | + |
| 32 | + // then |
| 33 | + expect(result.outputText).toMatchSnapshot(); |
| 34 | + }); |
| 35 | + |
| 36 | + test('should ignore if return type is not a class', () => { |
| 37 | + // given |
| 38 | + const options: ts.CompilerOptions = { |
| 39 | + module: ts.ModuleKind.ES2020, |
| 40 | + target: ts.ScriptTarget.ES2020, |
| 41 | + newLine: ts.NewLineKind.LineFeed, |
| 42 | + noEmitHelpers: true, |
| 43 | + experimentalDecorators: true, |
| 44 | + strict: true, |
| 45 | + }; |
| 46 | + const filename = 'text.service.ts'; |
| 47 | + const fakeProgram = ts.createProgram([filename], options); |
| 48 | + |
| 49 | + // when |
| 50 | + const result = ts.transpileModule(returnStringServiceCode, { |
| 51 | + compilerOptions: options, |
| 52 | + fileName: filename, |
| 53 | + transformers: { |
| 54 | + before: [before(undefined, fakeProgram)], |
| 55 | + }, |
| 56 | + }); |
| 57 | + |
| 58 | + // then |
| 59 | + expect(result.outputText).toMatchSnapshot(); |
| 60 | + }); |
| 61 | + |
| 62 | + test('should ignore if method has ResponseBody decorator', () => { |
| 63 | + // given |
| 64 | + const options: ts.CompilerOptions = { |
| 65 | + module: ts.ModuleKind.ES2020, |
| 66 | + target: ts.ScriptTarget.ES2020, |
| 67 | + newLine: ts.NewLineKind.LineFeed, |
| 68 | + noEmitHelpers: true, |
| 69 | + experimentalDecorators: true, |
| 70 | + strict: true, |
| 71 | + }; |
| 72 | + const filename = 'text.service.ts'; |
| 73 | + const fakeProgram = ts.createProgram([filename], options); |
| 74 | + |
| 75 | + // when |
| 76 | + const result = ts.transpileModule(hasResponseBodyServiceCode, { |
| 77 | + compilerOptions: options, |
| 78 | + fileName: filename, |
| 79 | + transformers: { before: [before({}, fakeProgram)] }, |
| 80 | + }); |
| 81 | + |
| 82 | + // then |
| 83 | + expect(result.outputText).toMatchSnapshot(); |
| 84 | + }); |
| 85 | + |
| 86 | + test('should ignore if return type if not a promise ', () => { |
| 87 | + // given |
| 88 | + const options: ts.CompilerOptions = { |
| 89 | + module: ts.ModuleKind.ES2020, |
| 90 | + target: ts.ScriptTarget.ES2020, |
| 91 | + newLine: ts.NewLineKind.LineFeed, |
| 92 | + noEmitHelpers: true, |
| 93 | + experimentalDecorators: true, |
| 94 | + strict: true, |
| 95 | + }; |
| 96 | + const filename = 'text.service.ts'; |
| 97 | + const fakeProgram = ts.createProgram([filename], options); |
| 98 | + |
| 99 | + // when |
| 100 | + const result = ts.transpileModule(notPromiseServiceCode, { |
| 101 | + compilerOptions: options, |
| 102 | + fileName: filename, |
| 103 | + transformers: { before: [before({}, fakeProgram)] }, |
| 104 | + }); |
| 105 | + |
| 106 | + // then |
| 107 | + expect(result.outputText).toMatchSnapshot(); |
| 108 | + }); |
| 109 | + |
| 110 | + test('should override plugin suffix option', () => { |
| 111 | + // given |
| 112 | + const options: ts.CompilerOptions = { |
| 113 | + module: ts.ModuleKind.ES2020, |
| 114 | + target: ts.ScriptTarget.ES2020, |
| 115 | + newLine: ts.NewLineKind.LineFeed, |
| 116 | + noEmitHelpers: true, |
| 117 | + experimentalDecorators: true, |
| 118 | + strict: true, |
| 119 | + }; |
| 120 | + const filename = '.custom.ts'; |
| 121 | + const fakeProgram = ts.createProgram([filename], options); |
| 122 | + |
| 123 | + // when |
| 124 | + const result = ts.transpileModule(needResponseBodyServiceCode, { |
| 125 | + compilerOptions: options, |
| 126 | + fileName: filename, |
| 127 | + transformers: { |
| 128 | + before: [ |
| 129 | + before({ interfaceFilenameSuffix: '.custom.ts' }, fakeProgram), |
| 130 | + ], |
| 131 | + }, |
| 132 | + }); |
| 133 | + |
| 134 | + // then |
| 135 | + expect(result.outputText).toMatchSnapshot(); |
| 136 | + }); |
| 137 | +}); |
0 commit comments