1
- import { forceCastTo } from '../../../../testsutil' ;
1
+ import { expansionCastTo , forceCastTo } from '../../../../testsutil' ;
2
2
import { AssignedWorkItemsViewProvider } from './jiraAssignedWorkItemsViewProvider' ;
3
3
import { Container } from '../../../container' ;
4
4
import { JQLManager } from '../../../jira/jqlManager' ;
@@ -8,6 +8,9 @@ import { JQLEntry } from '../../../config/model';
8
8
import { MinimalIssue } from '@atlassianlabs/jira-pi-common-models' ;
9
9
import { DetailedSiteInfo } from '../../../atlclients/authInfo' ;
10
10
import * as vscode from 'vscode' ;
11
+ import { PromiseRacer } from 'src/util/promises' ;
12
+ import { JiraNotifier } from './jiraNotifier' ;
13
+ import { RefreshTimer } from 'src/views/RefreshTimer' ;
11
14
12
15
const mockedJqlEntry = forceCastTo < JQLEntry > ( {
13
16
id : 'jqlId' ,
@@ -52,6 +55,7 @@ const mockedIssue3 = forceCastTo<MinimalIssue<DetailedSiteInfo>>({
52
55
children : [ ] ,
53
56
} ) ;
54
57
58
+ jest . mock ( './jiraNotifier' ) ;
55
59
jest . mock ( '../searchJiraHelper' ) ;
56
60
jest . mock ( '../../../container' , ( ) => ( {
57
61
Container : {
@@ -78,7 +82,9 @@ jest.mock('../../../container', () => ({
78
82
} ,
79
83
} ) ) ;
80
84
81
- class PromiseRacerMockClass {
85
+ type ExtractPublic < T > = { [ P in keyof T ] : T [ P ] } ;
86
+
87
+ class PromiseRacerMockClass implements ExtractPublic < PromiseRacer < any > > {
82
88
public static LastInstance : PromiseRacerMockClass | undefined = undefined ;
83
89
private count : number ;
84
90
private mockedData : any [ ] = [ ] ;
@@ -108,6 +114,44 @@ jest.mock('../../../util/promises', () => ({
108
114
PromiseRacer : jest . fn ( ) . mockImplementation ( ( promises ) => new PromiseRacerMockClass ( promises ) ) ,
109
115
} ) ) ;
110
116
117
+ class JiraNotifierMockClass implements ExtractPublic < JiraNotifier > {
118
+ public static LastInstance : JiraNotifierMockClass | undefined = undefined ;
119
+ constructor ( ) {
120
+ JiraNotifierMockClass . LastInstance = this ;
121
+ }
122
+ public ignoreAssignedIssues ( issues : MinimalIssue < DetailedSiteInfo > [ ] ) : void { }
123
+ public notifyForNewAssignedIssues ( issues : MinimalIssue < DetailedSiteInfo > [ ] ) : void { }
124
+ }
125
+
126
+ jest . mock ( './jiraNotifier' , ( ) => ( {
127
+ JiraNotifier : jest . fn ( ) . mockImplementation ( ( ) => new JiraNotifierMockClass ( ) ) ,
128
+ } ) ) ;
129
+
130
+ class RefreshTimerMockClass implements ExtractPublic < RefreshTimer > {
131
+ public static LastInstance : RefreshTimerMockClass | undefined = undefined ;
132
+ constructor (
133
+ _enabledConfigPath : string | undefined ,
134
+ _intervalConfigPath : string ,
135
+ public refreshFunc : ( ) => void ,
136
+ ) {
137
+ RefreshTimerMockClass . LastInstance = this ;
138
+ }
139
+ dispose ( ) : void { }
140
+ isEnabled ( ) : boolean {
141
+ return false ;
142
+ }
143
+ setActive ( active : boolean ) : void { }
144
+ }
145
+
146
+ jest . mock ( '../../RefreshTimer' , ( ) => ( {
147
+ RefreshTimer : jest
148
+ . fn ( )
149
+ . mockImplementation (
150
+ ( enabledConfigPath , intervalConfigPath , refreshFunc ) =>
151
+ new RefreshTimerMockClass ( enabledConfigPath , intervalConfigPath , refreshFunc ) ,
152
+ ) ,
153
+ } ) ) ;
154
+
111
155
const mockedTreeView = {
112
156
onDidChangeVisibility : ( ) => { } ,
113
157
} ;
@@ -118,13 +162,44 @@ describe('AssignedWorkItemsViewProvider', () => {
118
162
beforeEach ( ( ) => {
119
163
jest . spyOn ( vscode . window , 'createTreeView' ) . mockReturnValue ( mockedTreeView as any ) ;
120
164
provider = undefined ;
165
+
166
+ PromiseRacerMockClass . LastInstance = undefined ;
167
+ JiraNotifierMockClass . LastInstance = undefined ;
168
+ RefreshTimerMockClass . LastInstance = undefined ;
121
169
} ) ;
122
170
123
171
afterEach ( ( ) => {
124
172
provider ?. dispose ( ) ;
125
173
jest . restoreAllMocks ( ) ;
126
174
} ) ;
127
175
176
+ describe ( 'initialization' , ( ) => {
177
+ it ( 'onDidSitesAvailableChange is registered during construction' , async ( ) => {
178
+ let onDidSitesAvailableChangeCallback = undefined ;
179
+ jest . spyOn ( Container . siteManager , 'onDidSitesAvailableChange' ) . mockImplementation (
180
+ ( func : any , parent : any ) : any => {
181
+ onDidSitesAvailableChangeCallback = ( ...args : any [ ] ) => func . apply ( parent , args ) ;
182
+ } ,
183
+ ) ;
184
+
185
+ provider = new AssignedWorkItemsViewProvider ( ) ;
186
+
187
+ expect ( onDidSitesAvailableChangeCallback ) . toBeDefined ( ) ;
188
+ } ) ;
189
+
190
+ it ( 'RefreshTimer is registered during construction and triggers refresh' , async ( ) => {
191
+ let dataChanged = false ;
192
+
193
+ provider = new AssignedWorkItemsViewProvider ( ) ;
194
+ provider . onDidChangeTreeData ( ( ) => ( dataChanged = true ) , undefined ) ;
195
+
196
+ expect ( RefreshTimerMockClass . LastInstance ) . toBeDefined ( ) ;
197
+
198
+ RefreshTimerMockClass . LastInstance ?. refreshFunc ( ) ;
199
+ expect ( dataChanged ) . toBeTruthy ( ) ;
200
+ } ) ;
201
+ } ) ;
202
+
128
203
describe ( 'getAllDefaultJQLEntries' , ( ) => {
129
204
it ( 'should initialize with configure Jira message if no JQL entries' , async ( ) => {
130
205
jest . spyOn ( Container . jqlManager , 'getAllDefaultJQLEntries' ) . mockReturnValue ( [ ] ) ;
@@ -139,7 +214,7 @@ describe('AssignedWorkItemsViewProvider', () => {
139
214
} ) ;
140
215
141
216
it ( 'should initialize with JQL promises if JQL entries exist, returns empty' , async ( ) => {
142
- const jqlEntries = [ forceCastTo < JQLEntry > ( { siteId : 'site1' , query : 'query1' } ) ] ;
217
+ const jqlEntries = [ expansionCastTo < JQLEntry > ( { siteId : 'site1' , query : 'query1' } ) ] ;
143
218
jest . spyOn ( Container . jqlManager , 'getAllDefaultJQLEntries' ) . mockReturnValue ( jqlEntries ) ;
144
219
provider = new AssignedWorkItemsViewProvider ( ) ;
145
220
@@ -153,7 +228,7 @@ describe('AssignedWorkItemsViewProvider', () => {
153
228
} ) ;
154
229
155
230
it ( 'should initialize with JQL promises if JQL entries exist, returns issues' , async ( ) => {
156
- const jqlEntries = [ forceCastTo < JQLEntry > ( { siteId : 'site1' , query : 'query1' } ) ] ;
231
+ const jqlEntries = [ expansionCastTo < JQLEntry > ( { siteId : 'site1' , query : 'query1' } ) ] ;
157
232
jest . spyOn ( Container . jqlManager , 'getAllDefaultJQLEntries' ) . mockReturnValue ( jqlEntries ) ;
158
233
provider = new AssignedWorkItemsViewProvider ( ) ;
159
234
@@ -180,18 +255,43 @@ describe('AssignedWorkItemsViewProvider', () => {
180
255
} ) ;
181
256
} ) ;
182
257
183
- describe ( 'onDidSitesAvailableChange' , ( ) => {
184
- it ( 'onDidSitesAvailableChange is registered during construction' , async ( ) => {
185
- let onDidSitesAvailableChangeCallback = undefined ;
186
- jest . spyOn ( Container . siteManager , 'onDidSitesAvailableChange' ) . mockImplementation (
187
- ( func : any , parent : any ) : any => {
188
- onDidSitesAvailableChangeCallback = ( ...args : any [ ] ) => func . apply ( parent , args ) ;
189
- } ,
190
- ) ;
258
+ describe ( 'JiraNotifier' , ( ) => {
259
+ it ( "doesn't notify when the provider is fetching for the first time" , async ( ) => {
260
+ const jqlEntries = [ expansionCastTo < JQLEntry > ( { siteId : 'site1' , query : 'query1' } ) ] ;
261
+
262
+ jest . spyOn ( Container . jqlManager , 'getAllDefaultJQLEntries' ) . mockReturnValue ( jqlEntries ) ;
263
+ jest . spyOn ( vscode . window , 'showInformationMessage' ) ;
191
264
192
265
provider = new AssignedWorkItemsViewProvider ( ) ;
193
266
194
- expect ( onDidSitesAvailableChangeCallback ) . toBeDefined ( ) ;
267
+ jest . spyOn ( JiraNotifierMockClass . LastInstance ! , 'ignoreAssignedIssues' ) ;
268
+ jest . spyOn ( JiraNotifierMockClass . LastInstance ! , 'notifyForNewAssignedIssues' ) ;
269
+
270
+ PromiseRacerMockClass . LastInstance ?. mockData ( [ mockedIssue1 , mockedIssue2 , mockedIssue3 ] ) ;
271
+ await provider . getChildren ( ) ;
272
+
273
+ expect ( JiraNotifierMockClass . LastInstance ! . ignoreAssignedIssues ) . toHaveBeenCalled ( ) ;
274
+ expect ( JiraNotifierMockClass . LastInstance ! . notifyForNewAssignedIssues ) . not . toHaveBeenCalled ( ) ;
275
+ } ) ;
276
+
277
+ it ( 'it notifies for newly fetched items' , async ( ) => {
278
+ const jqlEntries = [ expansionCastTo < JQLEntry > ( { siteId : 'site1' , query : 'query1' } ) ] ;
279
+
280
+ jest . spyOn ( Container . jqlManager , 'getAllDefaultJQLEntries' ) . mockReturnValue ( jqlEntries ) ;
281
+ jest . spyOn ( vscode . window , 'showInformationMessage' ) ;
282
+
283
+ provider = new AssignedWorkItemsViewProvider ( ) ;
284
+
285
+ PromiseRacerMockClass . LastInstance ?. mockData ( [ mockedIssue1 , mockedIssue2 , mockedIssue3 ] ) ;
286
+ await provider . getChildren ( ) ;
287
+
288
+ jest . spyOn ( JiraNotifierMockClass . LastInstance ! , 'ignoreAssignedIssues' ) ;
289
+ jest . spyOn ( JiraNotifierMockClass . LastInstance ! , 'notifyForNewAssignedIssues' ) ;
290
+
291
+ await provider . getChildren ( ) ;
292
+
293
+ expect ( JiraNotifierMockClass . LastInstance ! . ignoreAssignedIssues ) . not . toHaveBeenCalled ( ) ;
294
+ expect ( JiraNotifierMockClass . LastInstance ! . notifyForNewAssignedIssues ) . toHaveBeenCalled ( ) ;
195
295
} ) ;
196
296
} ) ;
197
297
} ) ;
0 commit comments