|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { transform } from '../src/ts-transformer'; |
| 3 | + |
| 4 | +describe('Integration', () => { |
| 5 | + it('should transform createPicker to pick only specified properties', () => { |
| 6 | + // noinspection NpmUsedModulesInstalled,JSUnresolvedReference,UnnecessaryLabelJS,BadExpressionStatementJS,JSValidateTypes,JSUnusedLocalSymbols |
| 7 | + const code = ` |
| 8 | + // noinspection JSAnnotator |
| 9 | + |
| 10 | + import { createPicker } from "ts-runtime-picker"; |
| 11 | +
|
| 12 | + interface User { |
| 13 | + firstName: string; |
| 14 | + lastName: string; |
| 15 | + email: string; |
| 16 | + password: string; |
| 17 | + } |
| 18 | +
|
| 19 | + const picker = createPicker<User>(); |
| 20 | +
|
| 21 | + const user = { |
| 22 | + firstName: "John", |
| 23 | + lastName: "Doe", |
| 24 | + email: "john.doe@example.com", |
| 25 | + password: "secret", |
| 26 | + extraData: "will be removed" |
| 27 | + }; |
| 28 | +
|
| 29 | + const result = picker(user); |
| 30 | + `; |
| 31 | + |
| 32 | + const filePath = 'test-file.ts'; |
| 33 | + const transformedCode = transform(code, filePath); |
| 34 | + |
| 35 | + // Verify the transformation |
| 36 | + expect(transformedCode).toContain('const _keys: string[] = ["firstName","lastName","email","password"]'); |
| 37 | + expect(transformedCode).not.toContain('createPicker<User>()'); |
| 38 | + |
| 39 | + // Instead of executing the code (which would fail due to import statements), |
| 40 | + // we'll verify the structure of the transformed code |
| 41 | + |
| 42 | + // Check that the picker function is correctly transformed |
| 43 | + expect(transformedCode).toContain('const picker = (_obj: any) => {'); |
| 44 | + expect(transformedCode).toContain('const _keys: string[] = ["firstName","lastName","email","password"]'); |
| 45 | + expect(transformedCode).toContain('return _keys.reduce((_acc: {[k: string]: any}, _key: string) => {'); |
| 46 | + expect(transformedCode).toContain('if (_key in _obj) _acc[_key] = _obj[_key];'); |
| 47 | + expect(transformedCode).toContain('return _acc;'); |
| 48 | + expect(transformedCode).toContain('}, {});'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should transform createFullPicker to pick all properties', () => { |
| 52 | + // noinspection NpmUsedModulesInstalled,JSUnresolvedReference,UnnecessaryLabelJS,BadExpressionStatementJS,JSValidateTypes,JSUnusedLocalSymbols |
| 53 | + const code = ` |
| 54 | + // noinspection JSAnnotator |
| 55 | + |
| 56 | + import { createFullPicker } from "ts-runtime-picker"; |
| 57 | +
|
| 58 | + interface User { |
| 59 | + firstName: string; |
| 60 | + lastName: string; |
| 61 | + } |
| 62 | +
|
| 63 | + const picker = createFullPicker<User>(); |
| 64 | +
|
| 65 | + const user = { |
| 66 | + firstName: "John", |
| 67 | + lastName: "Doe", |
| 68 | + extraData: "will be removed" |
| 69 | + }; |
| 70 | +
|
| 71 | + const result = picker(user); |
| 72 | + `; |
| 73 | + |
| 74 | + const filePath = 'test-file.ts'; |
| 75 | + const transformedCode = transform(code, filePath); |
| 76 | + |
| 77 | + // Verify the transformation |
| 78 | + expect(transformedCode).toContain('const _keys: string[] = ["firstName","lastName"]'); |
| 79 | + expect(transformedCode).not.toContain('createFullPicker<User>()'); |
| 80 | + |
| 81 | + // Instead of executing the code (which would fail due to import statements), |
| 82 | + // we'll verify the structure of the transformed code |
| 83 | + |
| 84 | + // Check that the picker function is correctly transformed |
| 85 | + expect(transformedCode).toContain('const picker = (_obj: any) => {'); |
| 86 | + expect(transformedCode).toContain('const _keys: string[] = ["firstName","lastName"]'); |
| 87 | + expect(transformedCode).toContain('return _keys.reduce((_acc: {[k: string]: any}, _key: string) => {'); |
| 88 | + expect(transformedCode).toContain('if (_key in _obj) _acc[_key] = _obj[_key];'); |
| 89 | + expect(transformedCode).toContain('return _acc;'); |
| 90 | + expect(transformedCode).toContain('}, {});'); |
| 91 | + }); |
| 92 | +}); |
0 commit comments