Skip to content

Commit

Permalink
test: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
friedrith committed Mar 10, 2024
1 parent d148d7b commit 90af0ff
Show file tree
Hide file tree
Showing 15 changed files with 167 additions and 8 deletions.
Empty file.
4 changes: 0 additions & 4 deletions src/features/contributing/utils/removeSection.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/features/license/generateLicense.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as packageConfig from '../package'
import * as context from '../../context'
import listLicenseFiles, { getFullPath } from './utils/listLicenseFiles'
import cleanLicenseName from './utils/cleanLicenseName'
import hasProperty from './utils/properties/hasProperty'
import hasProperty from './utils/hasProperty'
import * as readme from '../readme'
import printTerminal from '../../services/terminal/printTerminal'
import setVariable from '../../services/template/setVariable'
Expand Down
7 changes: 7 additions & 0 deletions src/features/license/utils/__tests__/cleanLicenseName.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import cleanLicenseName from '../cleanLicenseName'

describe('cleanLicenseName', () => {
it('should remove .txt and convert to uppercase', () => {
expect(cleanLicenseName('mit.txt')).toEqual('MIT')
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import getLicenseContentInReadme from '../getLicenseContentInReadme'

describe('getLicenseContentInReadme', () => {
it('should return the content of the license section in the readme', () => {
const expectedLicenseContent =
'This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.'

const licenseContent = getLicenseContentInReadme('MIT')

expect(licenseContent).toEqual(expectedLicenseContent)
})
})
12 changes: 12 additions & 0 deletions src/features/license/utils/__tests__/getLicenseFilename.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import getLicenseFilename from '../getLicenseFilename'

describe('getLicenseFilename', () => {
it('should return the path to the license file', () => {
const repositoryPath = '/path/to/repo'
const expectedFilename = '/path/to/repo/LICENSE'

const filename = getLicenseFilename(repositoryPath)

expect(filename).toBe(expectedFilename)
})
})
27 changes: 27 additions & 0 deletions src/features/license/utils/__tests__/hasProperty.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import hasProperty from '../hasProperty'

describe('hasProperty', () => {
it('should return true', () => {
const content = 'foo {{var}} bar'

expect(hasProperty(content, 'var')).toBe(true)
})

it('should return true when template has space', () => {
const content = 'foo {{ var }} bar'

expect(hasProperty(content, 'var')).toBe(true)
})

it('should return false when no variable', () => {
const content = 'foo bar'

expect(hasProperty(content, 'var')).toBe(false)
})

it('should return false when variable with wrong name', () => {
const content = 'foo {{var2}} bar'

expect(hasProperty(content, 'var')).toBe(false)
})
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const hasProperty = (content: string, property: string) =>
content.includes(`{{ ${property} }}`)
Boolean(content.match(new RegExp(`{{[ ]*${property}[ ]*}}`)))

export default hasProperty
27 changes: 27 additions & 0 deletions src/features/package/packageManager/__tests__/npm.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import npm from '../npm'

describe('npm', () => {
describe('getConfigFile', () => {
it('should return package-lock.yaml', () => {
expect(npm.getConfigFile()).toBe('package-lock.yaml')
})
})

describe('getCommand', () => {
it('should return npm install', () => {
expect(npm.getCommand('install')).toBe('npm install')
})

it('should return npm test', () => {
expect(npm.getCommand('test')).toBe('npm test')
})

it('should return npm start', () => {
expect(npm.getCommand('start')).toBe('npm start')
})

it('should return npm run format', () => {
expect(npm.getCommand('format')).toBe('npm run format')
})
})
})
27 changes: 27 additions & 0 deletions src/features/package/packageManager/__tests__/pnpm.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pnpm from '../pnpm'

describe('pnpm', () => {
describe('getConfigFile', () => {
it('should return pnpm-lock.yaml', () => {
expect(pnpm.getConfigFile()).toBe('pnpm-lock.yaml')
})
})

describe('getCommand', () => {
it('should return pnpm install', () => {
expect(pnpm.getCommand('install')).toBe('pnpm install')
})

it('should return pnpm test', () => {
expect(pnpm.getCommand('test')).toBe('pnpm test')
})

it('should return pnpm start', () => {
expect(pnpm.getCommand('start')).toBe('pnpm start')
})

it('should return pnpm format', () => {
expect(pnpm.getCommand('format')).toBe('pnpm format')
})
})
})
27 changes: 27 additions & 0 deletions src/features/package/packageManager/__tests__/yarn.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import yarn from '../yarn'

describe('yarn', () => {
describe('getConfigFile', () => {
it('should return yarn.lock', () => {
expect(yarn.getConfigFile()).toBe('yarn.lock')
})
})

describe('getCommand', () => {
it('should return yarn install', () => {
expect(yarn.getCommand('install')).toBe('yarn install')
})

it('should return yarn test', () => {
expect(yarn.getCommand('test')).toBe('yarn test')
})

it('should return yarn start', () => {
expect(yarn.getCommand('start')).toBe('yarn start')
})

it('should return yarn format', () => {
expect(yarn.getCommand('format')).toBe('yarn format')
})
})
})
4 changes: 3 additions & 1 deletion src/features/package/packageManager/npm.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import PackageManager from './PackageManager'

const commandsWithoutRun = ['start', 'test', 'install']

const npm: PackageManager = {
getConfigFile: () => 'package-lock.yaml',
getCommand: (script: string) =>
['start', 'test'].includes(script) ? `npm ${script}` : `pnpm run ${script}`,
commandsWithoutRun.includes(script) ? `npm ${script}` : `npm run ${script}`,
}

export default npm
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import getPackageConfigFilename from '../getPackageConfigFilename'

describe('getPackageConfigFilename', () => {
it('should return the package.json path', () => {
const repositoryPath = '/path/to/repository'
const expectedFilename = '/path/to/repository/package.json'

expect(getPackageConfigFilename(repositoryPath)).toBe(expectedFilename)
})
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import setProperty from '../utils/setProperty'
import setProperty from '../setProperty'

const formattedContent = (content: object) => JSON.stringify(content, null, 2)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import getPullRequestTemplateFilename from '../getPullRequestTemplateFilename'

describe('getPullRequestTemplateFilename', () => {
it('should return the path to the pull request template file', () => {
const repositoryPath = '/path/to/repo'
const expectedFilename = '/path/to/repo/.github/PULL_REQUEST_TEMPLATE.md'

const filename = getPullRequestTemplateFilename(repositoryPath)

expect(filename).toBe(expectedFilename)
})
})

0 comments on commit 90af0ff

Please sign in to comment.