Skip to content

Commit

Permalink
Add negative test
Browse files Browse the repository at this point in the history
  • Loading branch information
xalvarez committed Dec 29, 2024
1 parent 8ed66c1 commit ff960eb
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions __tests__/pattern-matcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ jest.mock('../src/github-service')

describe('pattern-matcher', () => {
let gitHubService: GitHubService
let closePullRequestSpy: jest.SpyInstance

beforeEach(() => {
gitHubService = new GitHubService(GITHUB_TOKEN)
closePullRequestSpy = jest.spyOn(GitHubService.prototype, 'closePullRequest').mockResolvedValue()
})
afterEach(() => {
jest.restoreAllMocks()
Expand All @@ -24,6 +27,7 @@ describe('pattern-matcher', () => {

expect(core.setFailed).toHaveBeenCalledWith(`There is at least one file matching the pattern ${pattern}`)
expect(core.debug).not.toHaveBeenCalled()
expect(closePullRequestSpy).not.toHaveBeenCalled()
})

it('Should not reject non matching pattern', async () => {
Expand All @@ -34,6 +38,7 @@ describe('pattern-matcher', () => {

expect(core.setFailed).not.toHaveBeenCalled()
expect(core.debug).toHaveBeenCalledWith(`There isn't any file matching the pattern ${pattern}`)
expect(closePullRequestSpy).not.toHaveBeenCalled()
})

it('Should not reject empty commit', async () => {
Expand All @@ -44,6 +49,7 @@ describe('pattern-matcher', () => {

expect(core.setFailed).not.toHaveBeenCalled()
expect(core.debug).toHaveBeenCalledWith(`This commit doesn't contain any files`)
expect(closePullRequestSpy).not.toHaveBeenCalled()
})

it('Should not reject matching added file when allowNewFiles is true', async () => {
Expand All @@ -54,24 +60,16 @@ describe('pattern-matcher', () => {

expect(core.setFailed).not.toHaveBeenCalled()
expect(core.debug).toHaveBeenCalledWith(`There isn't any file matching the pattern ${pattern}`)
expect(closePullRequestSpy).not.toHaveBeenCalled()
})
it('Should close PR', async () => {
const files: IFile[] = givenFiles()
const pattern = '.*.js'
jest.spyOn(core, 'getInput').mockImplementation((inputName: string) => {
switch (inputName) {
case 'githubToken':
return 'exampleToken'
case 'closePR':
return 'true'
default:
return ''
}
})
const closePullRequest = jest.spyOn(GitHubService.prototype, 'closePullRequest').mockResolvedValue()

await checkChangedFilesAgainstPattern(files, pattern, gitHubService, 'exampleRepo', 'exampleOwner', 1, true)

expect(core.setFailed).not.toHaveBeenCalled()
expect(closePullRequest).toHaveBeenCalledWith('exampleOwner', 'exampleRepo', 1)
expect(closePullRequestSpy).toHaveBeenCalledWith('exampleOwner', 'exampleRepo', 1)
})
})

Expand Down

0 comments on commit ff960eb

Please sign in to comment.