@@ -2,6 +2,76 @@ const Labels = require('../../../lib/actions/labels')
22const Helper = require ( '../../../__fixtures__/unit/helper' )
33const UnSupportedSettingError = require ( '../../../lib/errors/unSupportedSettingError' )
44
5+ test ( 'check that replace replaces existing labels' , async ( ) => {
6+ const labels = new Labels ( )
7+ const context = createMockContext ( [ 'drop_label' ] )
8+
9+ const settings = {
10+ replace : [ 'work in progress' , 'development' ]
11+ }
12+
13+ await labels . afterValidate ( context , settings )
14+ expect ( context . octokit . issues . setLabels . mock . calls . length ) . toBe ( 1 )
15+ expect ( context . octokit . issues . setLabels . mock . calls [ 0 ] [ 0 ] . labels ) . toStrictEqual ( { labels : settings . replace } )
16+ } )
17+
18+ test ( 'check that add appends to existing labels' , async ( ) => {
19+ const labels = new Labels ( )
20+ const context = createMockContext ( [ 'another label' , 'test label' ] )
21+
22+ const settings = {
23+ add : [ 'production' , 'deploy' ]
24+ }
25+
26+ await labels . afterValidate ( context , settings )
27+ expect ( context . octokit . issues . setLabels . mock . calls . length ) . toBe ( 1 )
28+ expect ( context . octokit . issues . setLabels . mock . calls [ 0 ] [ 0 ] . labels ) . toStrictEqual ( { labels : [ 'another label' , 'test label' , 'production' , 'deploy' ] } )
29+ } )
30+
31+ test ( 'check that delete removes from existing labels' , async ( ) => {
32+ const labels = new Labels ( )
33+ const context = createMockContext ( [ 'another label' , 'test label' , 'delete me' , 'delete this too' ] )
34+
35+ const settings = {
36+ delete : [ 'delete me' , 'delete this too' ]
37+ }
38+
39+ await labels . afterValidate ( context , settings )
40+ expect ( context . octokit . issues . setLabels . mock . calls . length ) . toBe ( 1 )
41+ expect ( context . octokit . issues . setLabels . mock . calls [ 0 ] [ 0 ] . labels ) . toStrictEqual ( { labels : [ 'another label' , 'test label' ] } )
42+ } )
43+
44+ test ( 'check the order of replace, add, delete' , async ( ) => {
45+ const labels = new Labels ( )
46+ const context = createMockContext ( [ 'original label' , 'another label' ] )
47+
48+ // order of operations is replace, then add, then delete
49+ const settings = {
50+ delete : [ 'not present' , 'more adds' ] ,
51+ add : [ 'addition' , 'more adds' ] ,
52+ replace : [ 'test present' , 'not present' ]
53+ }
54+
55+ await labels . afterValidate ( context , settings )
56+ expect ( context . octokit . issues . setLabels . mock . calls . length ) . toBe ( 1 )
57+ expect ( context . octokit . issues . setLabels . mock . calls [ 0 ] [ 0 ] . labels ) . toStrictEqual ( { labels : [ 'test present' , 'addition' ] } )
58+ } )
59+
60+ test ( 'check that settings can be a single value' , async ( ) => {
61+ const labels = new Labels ( )
62+ const context = createMockContext ( [ 'original label' , 'another label' , 'delete me' ] )
63+
64+ const settings = {
65+ delete : 'deletion' ,
66+ add : 'addition' ,
67+ replace : 'replace'
68+ }
69+
70+ await labels . afterValidate ( context , settings )
71+ expect ( context . octokit . issues . setLabels . mock . calls . length ) . toBe ( 1 )
72+ expect ( context . octokit . issues . setLabels . mock . calls [ 0 ] [ 0 ] . labels ) . toStrictEqual ( { labels : [ 'replace' , 'addition' ] } )
73+ } )
74+
575test ( 'check that unknown mode throw errors' , async ( ) => {
676 const labels = new Labels ( )
777 const context = Helper . mockContext ( )
0 commit comments