Skip to content

Commit

Permalink
fix: add some more test
Browse files Browse the repository at this point in the history
  • Loading branch information
bonyuta0204 committed Dec 2, 2023
1 parent fd5accb commit e9e87e7
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 24 deletions.
120 changes: 96 additions & 24 deletions __tests__/run.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { setFailed, info } from '@actions/core'
import { run } from '../src/run'
import { fetchRemoteBranches } from '../src/git-util'
import { fetchExistingPullRequest } from '../src/github-utils'
import { fetchRemoteBranches, hasCommitsBetween } from '../src/git-util'
import {
addLabelsToPullRequest,
createPullRequest,
fetchExistingPullRequest
} from '../src/github-utils'
import { vi, expect, describe, it, beforeEach } from 'vitest'

vi.mock('@actions/core', () => ({
getInput: vi.fn(),
setFailed: vi.fn(),
info: vi.fn()
setFailed: vi.fn((msg: string) => console.error(msg)),
info: vi.fn((msg: string) => console.log(msg))
}))

vi.mock('../src/git-util', () => ({
Expand All @@ -16,7 +20,9 @@ vi.mock('../src/git-util', () => ({
}))

vi.mock('../src/github-utils', () => ({
fetchExistingPullRequest: vi.fn()
fetchExistingPullRequest: vi.fn(),
createPullRequest: vi.fn(),
addLabelsToPullRequest: vi.fn()
}))

describe('main function tests', () => {
Expand All @@ -30,7 +36,8 @@ describe('main function tests', () => {
targetBranch: 'target-branch',
repoToken: 'dummy-token',
repo: 'test-repo',
owner: 'test-owner'
owner: 'test-owner',
labels: []
})

expect(setFailed).toHaveBeenCalledWith(
Expand All @@ -46,34 +53,99 @@ describe('main function tests', () => {
targetBranch: 'valid-branch',
repoToken: 'dummy-token',
repo: 'test-repo',
owner: 'test-owner'
owner: 'test-owner',
labels: []
})

expect(setFailed).toHaveBeenCalledWith(
'Source branch nonexistent-branch does not exist'
)
})

it('should fail when pull request already exist', async () => {
const dummyPullRequest = 'https://dummy-pr.com'
;(fetchRemoteBranches as any).mockResolvedValue([
'valid-branch',
'valid-branch-2'
])
;(fetchExistingPullRequest as any).mockResolvedValue({
html_url: dummyPullRequest
describe('when there is valid diffrence', () => {
beforeEach(() => {
;(fetchRemoteBranches as any).mockResolvedValue([
'valid-branch',
'valid-branch-2'
])
;(hasCommitsBetween as any).mockResolvedValue(true)
})
describe('pull request is not created when pull request already exist', async () => {
const dummyPullRequest = 'https://dummy-pr.com'
beforeEach(() => {
;(fetchExistingPullRequest as any).mockResolvedValue({
html_url: dummyPullRequest
})
})

await run({
srcBranch: 'valid-branch',
targetBranch: 'valid-branch-2',
repoToken: 'dummy-token',
repo: 'test-repo',
owner: 'test-owner'
it('should not create pull request when there is existing pull request', async () => {
await run({
srcBranch: 'valid-branch',
targetBranch: 'valid-branch-2',
repoToken: 'dummy-token',
repo: 'test-repo',
owner: 'test-owner',
labels: []
})

expect(info).toHaveBeenCalledWith(
`Pull request already exists: ${dummyPullRequest}`
)
expect(createPullRequest).not.toHaveBeenCalled()

expect(addLabelsToPullRequest).not.toHaveBeenCalled()
})

it('should called addLabel request', async () => {
await run({
srcBranch: 'valid-branch',
targetBranch: 'valid-branch-2',
repoToken: 'dummy-token',
repo: 'test-repo',
owner: 'test-owner',
labels: ['test-label', 'test-label2']
})

expect(info).toHaveBeenCalledWith(
`Pull request already exists: ${dummyPullRequest}`
)
expect(createPullRequest).not.toHaveBeenCalled()

expect(addLabelsToPullRequest).toHaveBeenCalled()
})
})

expect(info).toHaveBeenCalledWith(
`Pull request already exists: ${dummyPullRequest}`
)
it('should create pull request when there is no existing pull request', async () => {
;(fetchExistingPullRequest as any).mockResolvedValue(undefined)

await run({
srcBranch: 'valid-branch',
targetBranch: 'valid-branch-2',
repoToken: 'dummy-token',
repo: 'test-repo',
owner: 'test-owner',
labels: []
})

expect(createPullRequest).toHaveBeenCalled()
expect(addLabelsToPullRequest).not.toHaveBeenCalled()
})

it('should create pull request and label when there is no existing pull request', async () => {
;(fetchExistingPullRequest as any).mockResolvedValue(undefined)
;(createPullRequest as any).mockResolvedValue({ pullNumber: 1 })

await run({
srcBranch: 'valid-branch',
targetBranch: 'valid-branch-2',
repoToken: 'dummy-token',
repo: 'test-repo',
owner: 'test-owner',
labels: ['test-label', 'test-label2']
})

expect(createPullRequest).toHaveBeenCalled()
expect(addLabelsToPullRequest).toHaveBeenCalled()
})
})
})
1 change: 1 addition & 0 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export async function run(options: OptionParams) {
`origin/${targetBranch}`,
`origin/${srcBranch}`
)

if (!hasCommits) {
info(`No commits between ${srcBranch} and ${targetBranch}`)
return
Expand Down

0 comments on commit e9e87e7

Please sign in to comment.