1
- import { WorkspaceEdit , Uri , workspace } from 'vscode' ;
1
+ import { WorkspaceEdit , Uri , workspace , ExtensionContext } from 'vscode' ;
2
2
import path from 'path' ;
3
3
4
+ import * as fsWatcher from './fsWatcher' ;
5
+ import * as utils from '../utils' ;
4
6
import {
5
7
createFile ,
6
8
removeFile ,
@@ -10,7 +12,6 @@ import {
10
12
closeEditorsAndCleanWorkspace ,
11
13
cacheWorkspace ,
12
14
getWorkspaceCache ,
13
- delay ,
14
15
waitForExpect ,
15
16
} from '../test/testUtils' ;
16
17
@@ -19,6 +20,20 @@ describe('fsWatcher feature', () => {
19
20
20
21
afterEach ( closeEditorsAndCleanWorkspace ) ;
21
22
23
+ let mockContext : ExtensionContext ;
24
+
25
+ beforeAll ( ( ) => {
26
+ mockContext = ( {
27
+ subscriptions : [ ] ,
28
+ } as unknown ) as ExtensionContext ;
29
+
30
+ fsWatcher . activate ( mockContext ) ;
31
+ } ) ;
32
+
33
+ afterAll ( ( ) => {
34
+ mockContext . subscriptions . forEach ( ( sub ) => sub . dispose ( ) ) ;
35
+ } ) ;
36
+
22
37
describe ( 'automatic refs update on file rename' , ( ) => {
23
38
it ( 'should update short ref with short ref on file rename' , async ( ) => {
24
39
const noteName0 = rndName ( ) ;
@@ -175,7 +190,7 @@ describe('fsWatcher feature', () => {
175
190
} ) ;
176
191
} ) ;
177
192
178
- it . skip ( 'should sync workspace cache on file create' , async ( ) => {
193
+ it ( 'should sync workspace cache on file create' , async ( ) => {
179
194
const noteName = rndName ( ) ;
180
195
const imageName = rndName ( ) ;
181
196
@@ -186,25 +201,24 @@ describe('fsWatcher feature', () => {
186
201
await createFile ( `${ noteName } .md` , '' , false ) ;
187
202
await createFile ( `${ imageName } .md` , '' , false ) ;
188
203
189
- // onDidCreate handler is not fired immediately
190
- await delay ( 100 ) ;
191
-
192
- const workspaceCache = await getWorkspaceCache ( ) ;
204
+ await waitForExpect ( async ( ) => {
205
+ const workspaceCache = await utils . getWorkspaceCache ( ) ;
193
206
194
- expect ( [ ...workspaceCache . markdownUris , ...workspaceCache . imageUris ] ) . toHaveLength ( 2 ) ;
195
- expect (
196
- [ ...workspaceCache . markdownUris , ...workspaceCache . imageUris ] . map ( ( { fsPath } ) =>
197
- path . basename ( fsPath ) ,
198
- ) ,
199
- ) . toEqual ( expect . arrayContaining ( [ `${ noteName } .md` , `${ imageName } .md` ] ) ) ;
207
+ expect ( [ ...workspaceCache . markdownUris , ...workspaceCache . imageUris ] ) . toHaveLength ( 2 ) ;
208
+ expect (
209
+ [ ...workspaceCache . markdownUris , ...workspaceCache . imageUris ] . map ( ( { fsPath } ) =>
210
+ path . basename ( fsPath ) ,
211
+ ) ,
212
+ ) . toEqual ( expect . arrayContaining ( [ `${ noteName } .md` , `${ imageName } .md` ] ) ) ;
213
+ } ) ;
200
214
} ) ;
201
215
202
- it . skip ( 'should sync workspace cache on file remove' , async ( ) => {
216
+ it . skip ( 'should sync workspace cache on file remove (For some reason onDidDelete is not called timely in test env) ' , async ( ) => {
203
217
const noteName = rndName ( ) ;
204
218
205
- await createFile ( `${ noteName } .md` , '' ) ;
219
+ await createFile ( `${ noteName } .md` ) ;
206
220
207
- const workspaceCache0 = await getWorkspaceCache ( ) ;
221
+ const workspaceCache0 = await utils . getWorkspaceCache ( ) ;
208
222
209
223
expect ( [ ...workspaceCache0 . markdownUris , ...workspaceCache0 . imageUris ] ) . toHaveLength ( 1 ) ;
210
224
expect (
@@ -213,18 +227,21 @@ describe('fsWatcher feature', () => {
213
227
) ,
214
228
) . toContain ( `${ noteName } .md` ) ;
215
229
216
- await removeFile ( `${ noteName } .md` ) ;
230
+ removeFile ( `${ noteName } .md` ) ;
217
231
218
- // onDidDelete handler is not fired immediately
219
- await delay ( 100 ) ;
232
+ if ( require ( 'fs' ) . existsSync ( path . join ( getWorkspaceFolder ( ) ! , `${ noteName } .md` ) ) ) {
233
+ throw new Error ( 'boom' ) ;
234
+ }
220
235
221
- const workspaceCache = await getWorkspaceCache ( ) ;
236
+ await waitForExpect ( async ( ) => {
237
+ const workspaceCache = await utils . getWorkspaceCache ( ) ;
222
238
223
- expect ( [ ...workspaceCache . markdownUris , ...workspaceCache . imageUris ] ) . toHaveLength ( 0 ) ;
224
- expect (
225
- [ ...workspaceCache0 . markdownUris , ...workspaceCache0 . imageUris ] . map ( ( { fsPath } ) =>
226
- path . basename ( fsPath ) ,
227
- ) ,
228
- ) . not . toContain ( `${ noteName } .md` ) ;
239
+ expect ( [ ...workspaceCache . markdownUris , ...workspaceCache . imageUris ] ) . toHaveLength ( 0 ) ;
240
+ expect (
241
+ [ ...workspaceCache . markdownUris , ...workspaceCache . imageUris ] . map ( ( { fsPath } ) =>
242
+ path . basename ( fsPath ) ,
243
+ ) ,
244
+ ) . not . toContain ( `${ noteName } .md` ) ;
245
+ } ) ;
229
246
} ) ;
230
247
} ) ;
0 commit comments