|
| 1 | +// import { TargetCaseType } from '@commitlint/types'; // When it is published |
| 2 | +import { When, TargetCaseType } from 'commitlint-github-utils/@types'; |
| 3 | +import { WhenAndRequiredValue, RuleResolverResult } from '../../../../@types'; |
| 4 | +import { typeCaseRuleResolver } from '../index'; |
| 5 | +import runRule from '../../utils/tests/utils'; |
| 6 | + |
| 7 | +const parse = ( |
| 8 | + whenAndValue: WhenAndRequiredValue<TargetCaseType | TargetCaseType[]>, |
| 9 | + rawCommitMessage: string, |
| 10 | +): RuleResolverResult => runRule(typeCaseRuleResolver, whenAndValue, rawCommitMessage); |
| 11 | + |
| 12 | +describe('typeCaseRuleResolver', () => { |
| 13 | + it('should always return an error response if an empty commit message is provided', () => { |
| 14 | + expect(parse([When.ALWAYS, 'lower-case'], '')[0]).toEqual(false); |
| 15 | + expect(parse([When.NEVER, 'lower-case'], '')[0]).toEqual(false); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should return a success response when passed non-WIP commits with no type at all, as nothing to validate', () => { |
| 19 | + expect(parse([When.ALWAYS, 'lower-case'], 'my commit message')[0]).toEqual(true); |
| 20 | + expect(parse([When.ALWAYS, 'lower-case'], '(#1) my commit message')[0]).toEqual(true); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should return a success response when passed non-WIP commits with a lowercased type matching the requirement', () => { |
| 24 | + expect(parse([When.ALWAYS, 'lower-case'], '(#1) chore: my commit message')[0]).toEqual(true); |
| 25 | + |
| 26 | + expect(parse([When.ALWAYS, 'lower-case'], '(#1, #2) fix: my commit message')[0]).toEqual(true); |
| 27 | + expect(parse([When.ALWAYS, 'lower-case'], '(#1,#2) arbitrary: my commit message')[0]).toEqual(true); |
| 28 | + |
| 29 | + expect(parse([When.ALWAYS, 'lower-case'], '(#123, #23) dummy: my commit message')[0]).toEqual(true); |
| 30 | + expect(parse([When.ALWAYS, 'lower-case'], '(#123,#23) chore: my commit message')[0]).toEqual(true); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should return a success response when passed non-WIP commits with a uppercased type matching the requirement', () => { |
| 34 | + expect(parse([When.ALWAYS, 'upper-case'], '(#1) CHORE: my commit message')[0]).toEqual(true); |
| 35 | + |
| 36 | + expect(parse([When.ALWAYS, 'upper-case'], '(#1, #2) FIX: my commit message')[0]).toEqual(true); |
| 37 | + expect(parse([When.ALWAYS, 'upper-case'], '(#1,#2) ARBITRARY: my commit message')[0]).toEqual(true); |
| 38 | + |
| 39 | + expect(parse([When.ALWAYS, 'upper-case'], '(#123, #23) DUMMY: my commit message')[0]).toEqual(true); |
| 40 | + expect(parse([When.ALWAYS, 'upper-case'], '(#123,#23) CHORE: my commit message')[0]).toEqual(true); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should return an error response when passed non-WIP commits with a lowercased type and never requiring it', () => { |
| 44 | + expect(parse([When.NEVER, 'lower-case'], '(#1) chore: my commit message')[0]).toEqual(false); |
| 45 | + |
| 46 | + expect(parse([When.NEVER, 'lower-case'], '(#1, #2) fix: my commit message')[0]).toEqual(false); |
| 47 | + expect(parse([When.NEVER, 'lower-case'], '(#1,#2) arbitrary: my commit message')[0]).toEqual(false); |
| 48 | + |
| 49 | + expect(parse([When.NEVER, 'lower-case'], '(#123, #23) dummy: my commit message')[0]).toEqual(false); |
| 50 | + expect(parse([When.NEVER, 'lower-case'], '(#123,#23) chore: my commit message')[0]).toEqual(false); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should return an error response when passed non-WIP commits with a uppercased type and never requiring it', () => { |
| 54 | + expect(parse([When.NEVER, 'upper-case'], '(#1) CHORE: my commit message')[0]).toEqual(false); |
| 55 | + |
| 56 | + expect(parse([When.NEVER, 'upper-case'], '(#1, #2) FIX: my commit message')[0]).toEqual(false); |
| 57 | + expect(parse([When.NEVER, 'upper-case'], '(#1,#2) ARBITRARY: my commit message')[0]).toEqual(false); |
| 58 | + |
| 59 | + expect(parse([When.NEVER, 'upper-case'], '(#123, #23) DUMMY: my commit message')[0]).toEqual(false); |
| 60 | + expect(parse([When.NEVER, 'upper-case'], '(#123,#23) CHORE: my commit message')[0]).toEqual(false); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should return a success response when passed WIP commits as that is not treated as a regular type and so is not validated by this rule', () => { |
| 64 | + // ALWAYS |
| 65 | + expect(parse([When.ALWAYS, 'lower-case'], '(#1) WIP')[0]).toEqual(true); |
| 66 | + |
| 67 | + expect(parse([When.ALWAYS, 'lower-case'], '(#1) WIP:my commit message')[0]).toEqual(true); |
| 68 | + expect(parse([When.ALWAYS, 'lower-case'], '(#1) WIP: my commit message')[0]).toEqual(true); |
| 69 | + |
| 70 | + expect(parse([When.ALWAYS, 'lower-case'], '(#1, #2, #3, #4) WIP: my commit message')[0]).toEqual(true); |
| 71 | + expect(parse([When.ALWAYS, 'lower-case'], '(#1, #2, #3, #4) WIP:my commit message')[0]).toEqual(true); |
| 72 | + |
| 73 | + expect(parse([When.ALWAYS, 'lower-case'], '(#1,#2,#3,#4) WIP: my commit message')[0]).toEqual(true); |
| 74 | + expect(parse([When.ALWAYS, 'lower-case'], '(#1,#2,#3,#4) WIP:my commit message')[0]).toEqual(true); |
| 75 | + |
| 76 | + expect(parse([When.ALWAYS, 'lower-case'], 'WIP2: My commit message')[0]).toEqual(true); |
| 77 | + expect(parse([When.ALWAYS, 'lower-case'], 'WIP 2: My commit message')[0]).toEqual(true); |
| 78 | + expect(parse([When.ALWAYS, 'lower-case'], 'WIP2 - My commit message')[0]).toEqual(true); |
| 79 | + expect(parse([When.ALWAYS, 'lower-case'], 'WIP 2 - My commit message')[0]).toEqual(true); |
| 80 | + |
| 81 | + // NEVER |
| 82 | + expect(parse([When.NEVER, 'lower-case'], '(#1) WIP')[0]).toEqual(true); |
| 83 | + |
| 84 | + expect(parse([When.NEVER, 'lower-case'], '(#1) WIP:my commit message')[0]).toEqual(true); |
| 85 | + expect(parse([When.NEVER, 'lower-case'], '(#1) WIP: my commit message')[0]).toEqual(true); |
| 86 | + |
| 87 | + expect(parse([When.NEVER, 'lower-case'], '(#1, #2, #3, #4) WIP: my commit message')[0]).toEqual(true); |
| 88 | + expect(parse([When.NEVER, 'lower-case'], '(#1, #2, #3, #4) WIP:my commit message')[0]).toEqual(true); |
| 89 | + |
| 90 | + expect(parse([When.NEVER, 'lower-case'], '(#1,#2,#3,#4) WIP: my commit message')[0]).toEqual(true); |
| 91 | + expect(parse([When.NEVER, 'lower-case'], '(#1,#2,#3,#4) WIP:my commit message')[0]).toEqual(true); |
| 92 | + |
| 93 | + expect(parse([When.NEVER, 'lower-case'], 'WIP2: My commit message')[0]).toEqual(true); |
| 94 | + expect(parse([When.NEVER, 'lower-case'], 'WIP 2: My commit message')[0]).toEqual(true); |
| 95 | + expect(parse([When.NEVER, 'lower-case'], 'WIP2 - My commit message')[0]).toEqual(true); |
| 96 | + expect(parse([When.NEVER, 'lower-case'], 'WIP 2 - My commit message')[0]).toEqual(true); |
| 97 | + }); |
| 98 | +}); |
0 commit comments