|
| 1 | +import { Commit, RuleConfigCondition, RuleOutcome } from '@commitlint/types'; |
| 2 | +import { beforeEach, describe, expect, it, jest } from '@jest/globals'; |
| 3 | +import functionRule, { FunctionRule } from './function-rule'; |
| 4 | + |
| 5 | +describe('functionRule', (): void => { |
| 6 | + const commit: Commit = { |
| 7 | + type: 'chore', |
| 8 | + scope: 'scope', |
| 9 | + subject: 'test', |
| 10 | + merge: null, |
| 11 | + header: 'chore(scope): test', |
| 12 | + body: null, |
| 13 | + footer: null, |
| 14 | + notes: [], |
| 15 | + references: [], |
| 16 | + mentions: [], |
| 17 | + revert: null, |
| 18 | + raw: 'chore(scope): test\n', |
| 19 | + }; |
| 20 | + const when: RuleConfigCondition = 'always'; |
| 21 | + /** |
| 22 | + * To pass this to an function, that is obviously not expecting a mock, a type |
| 23 | + * assertion is needed. For this, the as-syntax is needed when a mock is |
| 24 | + * passed as function argument. |
| 25 | + */ |
| 26 | + const ruleImplementation = jest.fn(); |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + ruleImplementation.mockReset(); |
| 30 | + }); |
| 31 | + |
| 32 | + it('calls implementation function', () => { |
| 33 | + functionRule(commit, when, ruleImplementation as FunctionRule); |
| 34 | + |
| 35 | + expect(ruleImplementation).toHaveBeenCalledTimes(1); |
| 36 | + }); |
| 37 | + |
| 38 | + it('passes arguments to implementation function', () => { |
| 39 | + functionRule(commit, when, ruleImplementation as FunctionRule); |
| 40 | + |
| 41 | + expect(ruleImplementation).toHaveBeenCalledWith(commit, when); |
| 42 | + }); |
| 43 | + |
| 44 | + it("defaults 'when' argument to 'always'", () => { |
| 45 | + functionRule(commit, undefined, ruleImplementation as FunctionRule); |
| 46 | + |
| 47 | + expect(ruleImplementation).toHaveBeenCalledWith(commit, 'always'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('returns value from sync implementation function', () => { |
| 51 | + const returnValue: RuleOutcome = [ |
| 52 | + true, |
| 53 | + 'Message from sync implementation function.', |
| 54 | + ]; |
| 55 | + ruleImplementation.mockImplementation(() => returnValue); |
| 56 | + const value = functionRule( |
| 57 | + commit, |
| 58 | + when, |
| 59 | + ruleImplementation as FunctionRule, |
| 60 | + ); |
| 61 | + |
| 62 | + expect(ruleImplementation).toHaveBeenCalledTimes(1); |
| 63 | + expect(value).toEqual(returnValue); |
| 64 | + }); |
| 65 | + |
| 66 | + it('returns value from async implementation function', async () => { |
| 67 | + const returnValue: RuleOutcome = [ |
| 68 | + true, |
| 69 | + 'Message from async implementation function.', |
| 70 | + ]; |
| 71 | + ruleImplementation.mockImplementation(() => Promise.resolve(returnValue)); |
| 72 | + const value = await functionRule( |
| 73 | + commit, |
| 74 | + when, |
| 75 | + ruleImplementation as FunctionRule, |
| 76 | + ); |
| 77 | + |
| 78 | + expect(ruleImplementation).toHaveBeenCalledTimes(1); |
| 79 | + expect(value).toEqual(returnValue); |
| 80 | + }); |
| 81 | + |
| 82 | + it("throws an error when 'value' is 'undefined'", () => { |
| 83 | + expect(() => { |
| 84 | + functionRule(commit, when, undefined); |
| 85 | + }).toThrow(); |
| 86 | + }); |
| 87 | + |
| 88 | + it("throws an error when 'value' is 'not an function'", () => { |
| 89 | + expect(() => { |
| 90 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 91 | + // @ts-expect-error: TS2345: Argument of type ... is not assignable to parameter of type ... |
| 92 | + functionRule(commit, when, 'not a function!'); |
| 93 | + }).toThrow(); |
| 94 | + }); |
| 95 | +}); |
0 commit comments