From 56347d54a15948763d5141fcbf50e50ec2e0206f Mon Sep 17 00:00:00 2001 From: Josh Dales Date: Tue, 21 Feb 2023 08:30:24 -0500 Subject: [PATCH] Add unit tests for toBranchMatchConfig --- __tests__/branch.test.ts | 70 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/__tests__/branch.test.ts b/__tests__/branch.test.ts index 71386b905..5bd36aa32 100644 --- a/__tests__/branch.test.ts +++ b/__tests__/branch.test.ts @@ -1,4 +1,9 @@ -import {getBranchName, checkBranch} from '../src/branch'; +import { + getBranchName, + checkBranch, + toBranchMatchConfig, + BranchMatchConfig +} from '../src/branch'; import * as github from '@actions/github'; jest.mock('@actions/core'); @@ -85,3 +90,66 @@ describe('checkBranch', () => { }); }); }); + +describe('toBranchMatchConfig', () => { + describe('when there are no branch keys in the config', () => { + const config = {'changed-files': [{any: ['testing']}]}; + it('returns an empty object', () => { + const result = toBranchMatchConfig(config); + expect(result).toMatchObject({}); + }); + }); + + describe('when the config contains a head-branch option', () => { + const config = {'head-branch': ['testing']}; + it('sets headBranch in the matchConfig', () => { + const result = toBranchMatchConfig(config); + expect(result).toMatchObject({ + headBranch: ['testing'] + }); + }); + + describe('and the matching option is a string', () => { + const stringConfig = {'head-branch': 'testing'}; + + it('sets headBranch in the matchConfig', () => { + const result = toBranchMatchConfig(stringConfig); + expect(result).toMatchObject({ + headBranch: ['testing'] + }); + }); + }); + }); + + describe('when the config contains a base-branch option', () => { + const config = {'base-branch': ['testing']}; + it('sets headBranch in the matchConfig', () => { + const result = toBranchMatchConfig(config); + expect(result).toMatchObject({ + baseBranch: ['testing'] + }); + }); + + describe('and the matching option is a string', () => { + const stringConfig = {'base-branch': 'testing'}; + + it('sets headBranch in the matchConfig', () => { + const result = toBranchMatchConfig(stringConfig); + expect(result).toMatchObject({ + baseBranch: ['testing'] + }); + }); + }); + }); + + describe('when the config contains both a base-branch and head-branch option', () => { + const config = {'base-branch': ['testing'], 'head-branch': ['testing']}; + it('sets headBranch in the matchConfig', () => { + const result = toBranchMatchConfig(config); + expect(result).toMatchObject({ + baseBranch: ['testing'], + headBranch: ['testing'] + }); + }); + }); +});