|
| 1 | +import { SyntaxKind } from 'ts-morph' |
| 2 | + |
| 3 | +import { createSourceFile } from '@sourcegraph/codemod-common' |
| 4 | + |
| 5 | +import { addOrUpdateImportIfIdentifierIsUsed } from '../ImportDeclaration' |
| 6 | + |
| 7 | +const classNamesImportStructure = { |
| 8 | + defaultImport: 'classNames', |
| 9 | + moduleSpecifier: 'classnames', |
| 10 | +} |
| 11 | + |
| 12 | +describe('addOrUpdateImportIfIdentifierIsUsed', () => { |
| 13 | + it('adds default import if needed', () => { |
| 14 | + const { sourceFile } = createSourceFile('const className = classNames("d-flex", )') |
| 15 | + |
| 16 | + addOrUpdateImportIfIdentifierIsUsed({ sourceFile, importStructure: classNamesImportStructure }) |
| 17 | + const importDeclaration = sourceFile.getFirstDescendantByKindOrThrow(SyntaxKind.ImportDeclaration) |
| 18 | + |
| 19 | + expect(importDeclaration.getModuleSpecifier().getLiteralValue()).toBe(classNamesImportStructure.moduleSpecifier) |
| 20 | + expect(importDeclaration.getDefaultImport()?.getText()).toBe(classNamesImportStructure.defaultImport) |
| 21 | + }) |
| 22 | + |
| 23 | + it('adds named imports if needed', () => { |
| 24 | + const { sourceFile } = createSourceFile('const markup = <Container><Button /><Container>') |
| 25 | + const importStructure = { |
| 26 | + namedImports: ['Button', 'Container', 'Input'], |
| 27 | + moduleSpecifier: '@sourcegraph/wildcard', |
| 28 | + } |
| 29 | + |
| 30 | + addOrUpdateImportIfIdentifierIsUsed({ sourceFile, importStructure }) |
| 31 | + const importDeclaration = sourceFile.getFirstDescendantByKindOrThrow(SyntaxKind.ImportDeclaration) |
| 32 | + const namedImports = importDeclaration.getNamedImports() |
| 33 | + |
| 34 | + expect(importDeclaration.getModuleSpecifier().getLiteralValue()).toBe(importStructure.moduleSpecifier) |
| 35 | + expect(namedImports.length).toBe(2) |
| 36 | + expect( |
| 37 | + namedImports.map(namedImport => { |
| 38 | + return namedImport.getText() |
| 39 | + }) |
| 40 | + ).toEqual(['Button', 'Container']) |
| 41 | + }) |
| 42 | + |
| 43 | + it('updates existing import declaration if needed', () => { |
| 44 | + const { sourceFile } = createSourceFile(` |
| 45 | + import { Container } from '@sourcegraph/wildcard' |
| 46 | +
|
| 47 | + const markup = <Container><Button /><Container> |
| 48 | + `) |
| 49 | + |
| 50 | + const importStructure = { |
| 51 | + namedImports: ['Button', 'Container', 'Input'], |
| 52 | + moduleSpecifier: '@sourcegraph/wildcard', |
| 53 | + } |
| 54 | + |
| 55 | + addOrUpdateImportIfIdentifierIsUsed({ sourceFile, importStructure }) |
| 56 | + const importDeclaration = sourceFile.getFirstDescendantByKindOrThrow(SyntaxKind.ImportDeclaration) |
| 57 | + const namedImports = importDeclaration.getNamedImports() |
| 58 | + |
| 59 | + expect(importDeclaration.getModuleSpecifier().getLiteralValue()).toBe(importStructure.moduleSpecifier) |
| 60 | + expect(namedImports.length).toBe(2) |
| 61 | + expect( |
| 62 | + namedImports.map(namedImport => { |
| 63 | + return namedImport.getText() |
| 64 | + }) |
| 65 | + ).toEqual(['Container', 'Button']) |
| 66 | + }) |
| 67 | + |
| 68 | + it('does not add import if it already exists', () => { |
| 69 | + const { sourceFile } = createSourceFile(` |
| 70 | + import classNames from 'classnames' |
| 71 | +
|
| 72 | + const className = classNames("d-flex", ) |
| 73 | + `) |
| 74 | + |
| 75 | + addOrUpdateImportIfIdentifierIsUsed({ sourceFile, importStructure: classNamesImportStructure }) |
| 76 | + const importDeclarations = sourceFile.getDescendantsOfKind(SyntaxKind.ImportDeclaration) |
| 77 | + |
| 78 | + expect(importDeclarations.length).toBe(1) |
| 79 | + }) |
| 80 | +}) |
0 commit comments