diff --git a/__tests__/branch.test.ts b/__tests__/branch.test.ts index d9c904cf..4bd20305 100644 --- a/__tests__/branch.test.ts +++ b/__tests__/branch.test.ts @@ -1,6 +1,7 @@ import { getBranchName, checkAnyBranch, + checkAllBranch, toBranchMatchConfig, BranchMatchConfig } from '../src/branch'; @@ -25,6 +26,65 @@ describe('getBranchName', () => { }); }); +describe('checkAllBranch', () => { + beforeEach(() => { + github.context.payload.pull_request!.head = { + ref: 'test/feature/123' + }; + github.context.payload.pull_request!.base = { + ref: 'main' + }; + }); + + describe('when a single pattern is provided', () => { + describe('and the pattern matches the head branch', () => { + it('returns true', () => { + const result = checkAllBranch(['^test'], 'head'); + expect(result).toBe(true); + }); + }); + + describe('and the pattern does not match the head branch', () => { + it('returns false', () => { + const result = checkAllBranch(['^feature/'], 'head'); + expect(result).toBe(false); + }); + }); + }); + + describe('when multiple patterns are provided', () => { + describe('and not all patterns matched', () => { + it('returns false', () => { + const result = checkAllBranch(['^test/', '^feature/'], 'head'); + expect(result).toBe(false); + }); + }); + + describe('and all patterns match', () => { + it('returns true', () => { + const result = checkAllBranch(['^test/', '/feature/'], 'head'); + expect(result).toBe(true); + }); + }); + + describe('and no patterns match', () => { + it('returns false', () => { + const result = checkAllBranch(['^feature/', '/test$'], 'head'); + expect(result).toBe(false); + }); + }); + }); + + describe('when the branch to check is specified as the base branch', () => { + describe('and the pattern matches the base branch', () => { + it('returns true', () => { + const result = checkAllBranch(['^main$'], 'base'); + expect(result).toBe(true); + }); + }); + }); +}); + describe('checkAnyBranch', () => { beforeEach(() => { github.context.payload.pull_request!.head = { diff --git a/__tests__/labeler.test.ts b/__tests__/labeler.test.ts index d5ac569b..0b9fb307 100644 --- a/__tests__/labeler.test.ts +++ b/__tests__/labeler.test.ts @@ -88,19 +88,53 @@ describe('toMatchConfig', () => { }); describe('checkMatchConfigs', () => { - const matchConfig: MatchConfig[] = [{any: [{changedFiles: ['*.txt']}]}]; + describe('when a single match config is provided', () => { + const matchConfig: MatchConfig[] = [{any: [{changedFiles: ['*.txt']}]}]; - it('returns true when our pattern does match changed files', () => { - const changedFiles = ['foo.txt', 'bar.txt']; - const result = checkMatchConfigs(changedFiles, matchConfig); + it('returns true when our pattern does match changed files', () => { + const changedFiles = ['foo.txt', 'bar.txt']; + const result = checkMatchConfigs(changedFiles, matchConfig); - expect(result).toBeTruthy(); + expect(result).toBeTruthy(); + }); + + it('returns false when our pattern does not match changed files', () => { + const changedFiles = ['foo.docx']; + const result = checkMatchConfigs(changedFiles, matchConfig); + + expect(result).toBeFalsy(); + }); + + it('returns true when either the branch or changed files patter matches', () => { + const matchConfig: MatchConfig[] = [ + {any: [{changedFiles: ['*.txt']}, {headBranch: ['some-branch']}]} + ]; + const changedFiles = ['foo.txt', 'bar.txt']; + + const result = checkMatchConfigs(changedFiles, matchConfig); + expect(result).toBe(true); + }); }); - it('returns false when our pattern does not match changed files', () => { - const changedFiles = ['foo.docx']; - const result = checkMatchConfigs(changedFiles, matchConfig); + describe('when multiple MatchConfigs are supplied', () => { + const matchConfig: MatchConfig[] = [ + {any: [{changedFiles: ['*.txt']}]}, + {any: [{headBranch: ['some-branch']}]} + ]; + const changedFiles = ['foo.txt', 'bar.md']; - expect(result).toBeFalsy(); + it('returns false when only one config matches', () => { + const result = checkMatchConfigs(changedFiles, matchConfig); + expect(result).toBe(false); + }); + + it('returns true when only both config matches', () => { + const matchConfig: MatchConfig[] = [ + {any: [{changedFiles: ['*.txt']}]}, + {any: [{headBranch: ['head-branch']}]} + ]; + const result = checkMatchConfigs(changedFiles, matchConfig); + expect(result).toBe(true); + }); }); });