1
1
const commitlintPluginSelectiveScope = require ( '../index' )
2
+ const commitlintPluginSelectiveScopeResolver =
3
+ commitlintPluginSelectiveScope . rules [ 'selective-scope' ]
4
+ // Wrap the resolver so we don't have to pass in "always"
5
+ const commitlintPluginSelectiveScopeResolverWrapped = ( ctx , rules ) =>
6
+ commitlintPluginSelectiveScopeResolver ( ctx , 'always' , rules )
7
+
8
+ const TEST_RULES = {
9
+ feat : [ / ^ f r o n t e n d \/ [ ^ / ] + $ / , 'backend' ] ,
10
+ perf : [ ] ,
11
+ ci : [ null , 'codebuild' , 'jenkins' ]
12
+ }
2
13
3
14
describe ( 'commitlintPluginSelectiveScope' , ( ) => {
4
15
it ( 'should return a valid config' , ( ) => {
@@ -7,4 +18,111 @@ describe('commitlintPluginSelectiveScope', () => {
7
18
Object . keys ( commitlintPluginSelectiveScope . rules ) . length
8
19
) . toBeGreaterThan ( 0 )
9
20
} )
21
+
22
+ it ( 'should not enforce scope if the type does not appear in the rules' , ( ) => {
23
+ expect (
24
+ commitlintPluginSelectiveScopeResolverWrapped (
25
+ { scope : 'frontend/web' , type : 'fix' } ,
26
+ TEST_RULES
27
+ ) [ 0 ]
28
+ ) . toBe ( true )
29
+
30
+ expect (
31
+ commitlintPluginSelectiveScopeResolverWrapped (
32
+ { scope : 'anything' , type : 'fix' } ,
33
+ TEST_RULES
34
+ ) [ 0 ]
35
+ ) . toBe ( true )
36
+
37
+ expect (
38
+ commitlintPluginSelectiveScopeResolverWrapped (
39
+ { scope : null , type : 'fix' } ,
40
+ TEST_RULES
41
+ ) [ 0 ]
42
+ ) . toBe ( true )
43
+ } )
44
+
45
+ it ( 'should not allow scope if the type appears in the rules with an empty array' , ( ) => {
46
+ expect (
47
+ commitlintPluginSelectiveScopeResolverWrapped (
48
+ { scope : null , type : 'perf' } ,
49
+ TEST_RULES
50
+ ) [ 0 ]
51
+ ) . toBe ( true )
52
+
53
+ expect (
54
+ commitlintPluginSelectiveScopeResolverWrapped (
55
+ { scope : 'something' , type : 'perf' } ,
56
+ TEST_RULES
57
+ ) [ 0 ]
58
+ ) . toBe ( false )
59
+ } )
60
+
61
+ describe ( 'should only allow scopes defined if the type appears in the rule with a non-empty array' , ( ) => {
62
+ it ( 'should properly match a string literal' , ( ) => {
63
+ expect (
64
+ commitlintPluginSelectiveScopeResolverWrapped (
65
+ { scope : 'backend' , type : 'feat' } ,
66
+ TEST_RULES
67
+ ) [ 0 ]
68
+ ) . toBe ( true )
69
+
70
+ expect (
71
+ commitlintPluginSelectiveScopeResolverWrapped (
72
+ { scope : 'test' , type : 'feat' } ,
73
+ TEST_RULES
74
+ ) [ 0 ]
75
+ ) . toBe ( false )
76
+ } )
77
+
78
+ it ( 'should properly match a RegExp' , ( ) => {
79
+ expect (
80
+ commitlintPluginSelectiveScopeResolverWrapped (
81
+ { scope : 'frontend/web' , type : 'feat' } ,
82
+ TEST_RULES
83
+ ) [ 0 ]
84
+ ) . toBe ( true )
85
+
86
+ expect (
87
+ commitlintPluginSelectiveScopeResolverWrapped (
88
+ { scope : 'frontend' , type : 'feat' } ,
89
+ TEST_RULES
90
+ ) [ 0 ]
91
+ ) . toBe ( false )
92
+ } )
93
+ } )
94
+
95
+ describe ( 'optional scope' , ( ) => {
96
+ it ( 'should allow scope to be optional if the type appears in the rules with a null in the array' , ( ) => {
97
+ expect (
98
+ commitlintPluginSelectiveScopeResolverWrapped (
99
+ { scope : null , type : 'ci' } ,
100
+ TEST_RULES
101
+ ) [ 0 ]
102
+ ) . toBe ( true )
103
+ } )
104
+
105
+ it ( 'should match scope if provided' , ( ) => {
106
+ expect (
107
+ commitlintPluginSelectiveScopeResolverWrapped (
108
+ { scope : 'codebuild' , type : 'ci' } ,
109
+ TEST_RULES
110
+ ) [ 0 ]
111
+ ) . toBe ( true )
112
+
113
+ expect (
114
+ commitlintPluginSelectiveScopeResolverWrapped (
115
+ { scope : 'jenkins' , type : 'ci' } ,
116
+ TEST_RULES
117
+ ) [ 0 ]
118
+ ) . toBe ( true )
119
+
120
+ expect (
121
+ commitlintPluginSelectiveScopeResolverWrapped (
122
+ { scope : 'github' , type : 'ci' } ,
123
+ TEST_RULES
124
+ ) [ 0 ]
125
+ ) . toBe ( false )
126
+ } )
127
+ } )
10
128
} )
0 commit comments