Files
labeler/__tests__/labeler.test.ts
Josh Dales d0d3628020 Add extra tests and use toEqual matcher.
Turns out that toMatchObject would allow extra properties through
2023-03-20 17:40:28 -04:00

68 lines
2.0 KiB
TypeScript

import {checkMatchConfigs, MatchConfig, toMatchConfig} from '../src/labeler';
import * as core from '@actions/core';
jest.mock('@actions/core');
beforeAll(() => {
jest.spyOn(core, 'getInput').mockImplementation((name, options) => {
return jest.requireActual('@actions/core').getInput(name, options);
});
});
describe('toMatchConfig', () => {
describe('when all expected config options are present', () => {
const config = {
'changed-files': [{any: ['testing-any']}, {all: ['testing-all']}],
'head-branch': ['testing-head'],
'base-branch': ['testing-base']
};
it('returns a MatchConfig object with all options', () => {
const result = toMatchConfig(config);
expect(result).toEqual<MatchConfig>({
changedFiles: {
all: ['testing-all'],
any: ['testing-any']
},
headBranch: ['testing-head'],
baseBranch: ['testing-base']
});
});
describe('and there are also unexpected options present', () => {
config['test-test'] = 'testing';
it('does not include the unexpected items in the returned MatchConfig object', () => {
const result = toMatchConfig(config);
expect(result).toEqual<MatchConfig>({
changedFiles: {
all: ['testing-all'],
any: ['testing-any']
},
headBranch: ['testing-head'],
baseBranch: ['testing-base']
});
});
});
});
});
describe('checkMatchConfigs', () => {
const matchConfig: MatchConfig[] = [{changedFiles: {any: ['*.txt']}}];
it('returns true when our pattern does match changed files', () => {
const changedFiles = ['foo.txt', 'bar.txt'];
const result = checkMatchConfigs(changedFiles, matchConfig);
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();
});
});