Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlecRust committed Apr 15, 2024
1 parent 7dd97c7 commit 0fd68a1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 18 deletions.
59 changes: 46 additions & 13 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,74 @@ import {
createPullRequest
} from '../src/utils'
import { mocked } from 'jest-mock'
import { simpleGit } from 'simple-git'

jest.mock('@actions/core')
jest.mock('../src/utils')
jest.mock('simple-git', () => ({
simpleGit: jest.fn().mockImplementation(() => ({
cwd: jest.fn().mockReturnThis(),
addConfig: jest.fn().mockReturnThis(),
checkoutLocalBranch: jest.fn().mockReturnThis(),
add: jest.fn().mockReturnThis(),
commit: jest.fn().mockReturnThis(),
push: jest.fn().mockReturnThis()
}))
}))
jest.mock('simple-git', () => {
const mockGit = {
addConfig: jest.fn(),
checkoutLocalBranch: jest.fn(),
add: jest.fn(),
commit: jest.fn(),
push: jest.fn(),
cwd: jest.fn()
}
return {
simpleGit: jest.fn(() => mockGit)
}
})

describe('action', () => {
const mockGit = simpleGit()

beforeEach(() => {
jest.resetAllMocks()
})

it('completes after updating files', async () => {
mocked(core.getInput).mockReturnValueOnce('true')
/* eslint-disable @typescript-eslint/unbound-method */
it('completes after updating files and creates PR when create-pr is true', async () => {
mocked(core.getInput).mockReturnValueOnce('true') // create-pr is true
mocked(core.getInput).mockReturnValueOnce('readme.txt\nanother-file.txt')
mocked(getLatestWpVersion).mockResolvedValueOnce('5.9')
mocked(updateFiles).mockResolvedValueOnce(true)
mocked(createPullRequest).mockResolvedValueOnce()

await run()

expect(core.getInput).toHaveBeenCalledWith('file-paths')
expect(getLatestWpVersion).toHaveBeenCalled()
expect(updateFiles).toHaveBeenCalled()
expect(mockGit.checkoutLocalBranch).toHaveBeenCalled()
expect(mockGit.add).toHaveBeenCalledWith('.')
expect(mockGit.commit).toHaveBeenCalled()
expect(mockGit.push).toHaveBeenCalledWith('origin', expect.any(String), [
'--set-upstream'
])
expect(createPullRequest).toHaveBeenCalled()
expect(core.setOutput).toHaveBeenCalledWith('updated', 'true')
})

it('completes after updating files and pushes to main branch when create-pr is false', async () => {
mocked(core.getInput).mockReturnValueOnce('false') // create-pr is false
mocked(core.getInput).mockReturnValueOnce('readme.txt\nanother-file.txt')
mocked(getLatestWpVersion).mockResolvedValueOnce('5.9')
mocked(updateFiles).mockResolvedValueOnce(true)

await run()

expect(core.getInput).toHaveBeenCalledWith('file-paths')
expect(getLatestWpVersion).toHaveBeenCalled()
expect(updateFiles).toHaveBeenCalled()

expect(mockGit.add).toHaveBeenCalledWith('.')
expect(mockGit.commit).toHaveBeenCalled()
expect(mockGit.push).toHaveBeenCalled()
expect(mockGit.push).toHaveBeenCalledWith()
expect(createPullRequest).not.toHaveBeenCalled()
expect(core.setOutput).toHaveBeenCalledWith('updated', 'true')
})
/* eslint-enable @typescript-eslint/unbound-method */

it('completes with no updates needed', async () => {
mocked(core.getInput).mockReturnValueOnce('true')
mocked(core.getInput).mockReturnValueOnce('readme.txt\nanother-file.txt')
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ export async function run(): Promise<void> {
}

console.log(`Updated to WordPress ${wpVersion}, committing changes...`)

const commitMessage = `Update WordPress 'Tested up to' version to ${wpVersion}`
await git.addConfig('user.email', '[email protected]')
await git.addConfig('user.name', 'GitHub Action')

const commitMessage = `Update WordPress 'Tested up to' version to ${wpVersion}`
if (createPR) {
const branchName = `tested-up-to-${wpVersion.replace(/\./g, '-')}`
await git.checkoutLocalBranch(branchName)
Expand Down

0 comments on commit 0fd68a1

Please sign in to comment.