|
| 1 | +import {makeGenericsText} from '../../transforms/code-transforms'; |
| 2 | + |
| 3 | +describe('makeGenericsText', () => { |
| 4 | + it('should return an empty string if no generics are provided', () => { |
| 5 | + expect(makeGenericsText(undefined)).toBe(''); |
| 6 | + expect(makeGenericsText([])).toBe(''); |
| 7 | + }); |
| 8 | + |
| 9 | + it('should return a single generic type without constraints or default', () => { |
| 10 | + const generics = [{name: 'T', constraint: undefined, default: undefined}]; |
| 11 | + expect(makeGenericsText(generics)).toBe('<T>'); |
| 12 | + }); |
| 13 | + |
| 14 | + it('should handle a single generic type with a constraint', () => { |
| 15 | + const generics = [{name: 'T', constraint: 'string', default: undefined}]; |
| 16 | + expect(makeGenericsText(generics)).toBe('<T extends string>'); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should handle a single generic type with a default value', () => { |
| 20 | + const generics = [{name: 'T', default: 'number', constraint: undefined}]; |
| 21 | + expect(makeGenericsText(generics)).toBe('<T = number>'); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should handle a single generic type with both constraint and default value', () => { |
| 25 | + const generics = [{name: 'T', constraint: 'string', default: 'number'}]; |
| 26 | + expect(makeGenericsText(generics)).toBe('<T extends string = number>'); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should handle multiple generic types without constraints or defaults', () => { |
| 30 | + const generics = [ |
| 31 | + {name: 'T', constraint: undefined, default: undefined}, |
| 32 | + {name: 'U', constraint: undefined, default: undefined}, |
| 33 | + ]; |
| 34 | + expect(makeGenericsText(generics)).toBe('<T, U>'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should handle multiple generic types with constraints and defaults', () => { |
| 38 | + const generics = [ |
| 39 | + {name: 'T', constraint: 'string', default: 'number'}, |
| 40 | + {name: 'U', constraint: 'boolean', default: undefined}, |
| 41 | + {name: 'V', default: 'any', constraint: undefined}, |
| 42 | + ]; |
| 43 | + expect(makeGenericsText(generics)).toBe( |
| 44 | + '<T extends string = number, U extends boolean, V = any>', |
| 45 | + ); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should handle complex generics with mixed constraints and defaults', () => { |
| 49 | + const generics = [ |
| 50 | + {name: 'A', constraint: 'string', default: undefined}, |
| 51 | + {name: 'B', constraint: undefined, default: undefined}, |
| 52 | + {name: 'C', default: 'number', constraint: undefined}, |
| 53 | + {name: 'D', constraint: 'boolean', default: 'true'}, |
| 54 | + ]; |
| 55 | + expect(makeGenericsText(generics)).toBe( |
| 56 | + '<A extends string, B, C = number, D extends boolean = true>', |
| 57 | + ); |
| 58 | + }); |
| 59 | +}); |
0 commit comments