1
1
import { assert , expect } from 'chai' ;
2
+ import * as gulp from 'gulp' ;
2
3
import 'mocha' ;
3
- import TaskManagerFactory , { ERRORS } from './index' ;
4
+ import TaskManager , { ERRORS } from './index' ;
4
5
5
6
6
7
// Helper: create custom hook task
7
- function makeTask ( hook : string ) {
8
+ function makeTask ( hook : string , register : boolean = true ) {
8
9
return {
9
10
install ( subscriptions ) {
10
- return subscriptions [ hook ] ( `${ hook } -task` ) ;
11
+ // Register Gulp test task
12
+ register && gulp . task ( `${ hook } -task` , done => done ( ) ) ;
13
+
14
+ // Subscribe task to hook
15
+ return subscriptions [ hook ] && subscriptions [ hook ] ( `${ hook } -task` ) ;
11
16
} ,
12
17
} ;
13
18
}
@@ -19,98 +24,120 @@ describe('TaskManager', () => {
19
24
} ;
20
25
21
26
22
- describe ( 'Factory' , ( ) => {
23
- it ( 'creates TaskManager instance' , ( ) => {
24
- const taskManager = TaskManagerFactory ( ) ;
25
- expect ( taskManager )
26
- . to . be . an ( 'object' )
27
- . to . have . all . keys ( 'add' , 'getHooks' , 'getTasks' ) ;
28
- } ) ;
27
+ it ( 'creates TaskManager instance' , ( ) => {
28
+ const taskManager = TaskManager ( ) ;
29
+ expect ( taskManager )
30
+ . to . be . an ( 'object' )
31
+ . to . have . all . keys ( 'add' , 'run' , 'hasTasks' , 'getHooks' , 'getTasks' ) ;
32
+ } ) ;
33
+
34
+
35
+ it ( 'adds custom hooks through the options object argument' , ( ) => {
36
+ const taskManager = TaskManager ( options ) ;
37
+ expect ( taskManager . getHooks ( ) )
38
+ . to . be . an ( 'array' )
39
+ . to . have . members ( options . hooks ) ;
40
+ } ) ;
41
+
42
+
43
+ it ( 'returns task arrays for all hooks' , ( ) => {
44
+ const taskManager = TaskManager ( options ) ;
45
+ const task = {
46
+ install ( subscribers ) {
47
+ expect ( subscribers )
48
+ . to . be . an ( 'object' )
49
+ . to . have . all . keys ( options . hooks )
50
+ . that . satisfies ( subject =>
51
+ Object . keys ( subject ) . every ( key => typeof subject [ key ] === 'function' ) ,
52
+ ) ;
53
+ } ,
54
+ } ;
55
+
56
+ taskManager . add ( task ) ;
57
+ } ) ;
58
+
59
+
60
+ it ( 'subscribes task to hook' , ( ) => {
61
+ const taskManager = TaskManager ( options ) ;
62
+ const task = makeTask ( 'before' ) ;
63
+
64
+ taskManager . add ( task ) ;
65
+
66
+ expect ( taskManager . getTasks ( ) . before )
67
+ . to . be . an ( 'array' )
68
+ . to . have . members ( [ 'before-task' ] ) ;
69
+ } ) ;
70
+
71
+
72
+ it ( 'returns true on task subscription' , ( ) => {
73
+ const taskManager = TaskManager ( options ) ;
74
+ const task = {
75
+ install ( subscriptions ) {
76
+ expect ( subscriptions . before ( 'before-task' ) ) . to . be . true ;
77
+ } ,
78
+ } ;
79
+ taskManager . add ( task ) ;
80
+ } ) ;
81
+
82
+
83
+ it ( 'returns true when task is run' , async ( ) => {
84
+ const taskManager = TaskManager ( options ) ;
85
+ taskManager . add ( makeTask ( 'before' ) ) ;
86
+ expect ( await taskManager . run ( 'before' ) ) . to . be . true ;
87
+ } ) ;
88
+
89
+
90
+ it ( 'returns false when task is not run' , async ( ) => {
91
+ const taskManager = TaskManager ( options ) ;
92
+ expect ( await taskManager . run ( 'before' ) ) . to . be . false ;
93
+ } ) ;
94
+
95
+
96
+ it ( 'returns true if specific hook has tasks' , ( ) => {
97
+ const taskManager = TaskManager ( options ) ;
98
+ taskManager . add ( makeTask ( 'before' ) ) ;
99
+ expect ( taskManager . hasTasks ( 'before' ) ) . to . be . true ;
100
+ } ) ;
101
+
102
+
103
+ it ( 'returns false if no tasks are found for specific hook' , ( ) => {
104
+ const taskManager = TaskManager ( options ) ;
105
+ expect ( taskManager . hasTasks ( 'before' ) ) . to . be . false ;
106
+ } ) ;
107
+
108
+
109
+ it ( 'throws when .add() doesn\'t receive appropriate task' , ( ) => {
110
+ const taskManager = TaskManager ( options ) ;
111
+ assert . throws ( ( ) => taskManager . add ( undefined ) , ERRORS . BAD_TASK ) ;
112
+ } ) ;
113
+
114
+
115
+ it ( 'returns false when trying to subscribe an already subscribed task name' , ( ) => {
116
+ const task1 = {
117
+ install ( subscriptions ) {
118
+ expect ( subscriptions . before ( 'same-task-name' ) ) . to . be . true ;
119
+ } ,
120
+ } ;
121
+ const task2 = {
122
+ install ( subscriptions ) {
123
+ expect ( subscriptions . before ( 'same-task-name' ) ) . to . be . false ;
124
+ } ,
125
+ } ;
126
+
127
+ const taskManager = TaskManager ( options ) ;
128
+ taskManager . add ( task1 ) ;
129
+ taskManager . add ( task2 ) ;
29
130
} ) ;
30
131
31
132
32
- describe ( 'Instance' , ( ) => {
33
- it ( 'adds custom hooks through the options object argument' , ( ) => {
34
- const taskManager = TaskManagerFactory ( options ) ;
35
- expect ( taskManager . getHooks ( ) )
36
- . to . be . an ( 'array' )
37
- . to . have . members ( options . hooks ) ;
38
- } ) ;
39
-
40
-
41
- it ( 'returns task arrays for all hooks' , ( ) => {
42
- const taskManager = TaskManagerFactory ( options ) ;
43
- const task = {
44
- install ( subscribers ) {
45
- expect ( subscribers )
46
- . to . be . an ( 'object' )
47
- . to . have . all . keys ( options . hooks )
48
- . that . satisfies ( subject =>
49
- Object . keys ( subject ) . every ( key => typeof subject [ key ] === 'function' ) ,
50
- ) ;
51
- } ,
52
- } ;
53
-
54
- taskManager . add ( task ) ;
55
- } ) ;
56
-
57
-
58
- it ( 'subscribes task to hook' , ( ) => {
59
- const taskManager = TaskManagerFactory ( options ) ;
60
- const task = makeTask ( 'before' ) ;
61
-
62
- taskManager . add ( task ) ;
63
-
64
- expect ( taskManager . getTasks ( ) . before )
65
- . to . be . an ( 'array' )
66
- . to . have . members ( [ 'before-task' ] ) ;
67
- } ) ;
68
-
69
-
70
- it ( 'returns true on task subscription' , ( ) => {
71
- const taskManager = TaskManagerFactory ( options ) ;
72
- const task = {
73
- install ( subscriptions ) {
74
- expect ( subscriptions . before ( 'before-task' ) ) . to . be . true ;
75
- } ,
76
- } ;
77
- taskManager . add ( task ) ;
78
- } ) ;
79
-
80
-
81
- it ( 'throws when .add() doesn\'t receive appropriate task' , ( ) => {
82
- const taskManager = TaskManagerFactory ( options ) ;
83
- assert . throws ( ( ) => taskManager . add ( undefined ) , ERRORS . BAD_TASK ) ;
84
- } ) ;
85
-
86
-
87
- it ( 'returns false when trying to subscribe an already subscribed task name' , ( ) => {
88
- const task1 = {
89
- install ( subscriptions ) {
90
- expect ( subscriptions . before ( 'same-task-name' ) ) . to . be . true ;
91
- } ,
92
- } ;
93
- const task2 = {
94
- install ( subscriptions ) {
95
- expect ( subscriptions . before ( 'same-task-name' ) ) . to . be . false ;
96
- } ,
97
- } ;
98
-
99
- const taskManager = TaskManagerFactory ( options ) ;
100
- taskManager . add ( task1 ) ;
101
- taskManager . add ( task2 ) ;
102
- } ) ;
103
-
104
-
105
- it ( 'should not allow more than 1 subscription per hook' , ( ) => {
106
- const abusingTask = {
107
- install ( subscriptions ) {
108
- expect ( subscriptions . before ( 'task-name' ) ) . to . be . true ;
109
- expect ( subscriptions . before ( 'devious-new-name' ) ) . to . be . undefined ;
110
- } ,
111
- } ;
112
- const taskManager = TaskManagerFactory ( options ) ;
113
- taskManager . add ( abusingTask ) ;
114
- } ) ;
133
+ it ( 'should not allow more than 1 subscription per hook' , ( ) => {
134
+ const abusingTask = {
135
+ install ( subscriptions ) {
136
+ expect ( subscriptions . before ( 'task-name' ) ) . to . be . true ;
137
+ expect ( subscriptions . before ( 'devious-new-name' ) ) . to . be . undefined ;
138
+ } ,
139
+ } ;
140
+ const taskManager = TaskManager ( options ) ;
141
+ taskManager . add ( abusingTask ) ;
115
142
} ) ;
116
143
} ) ;
0 commit comments