@@ -14,7 +14,7 @@ import {
1414 waitForDialog
1515} from '@jupyterlab/testing' ;
1616import { ServiceManagerMock } from '@jupyterlab/services/lib/testutils' ;
17- import { UUID } from '@lumino/coreutils' ;
17+ import { PromiseDelegate , UUID } from '@lumino/coreutils' ;
1818import { Widget } from '@lumino/widgets' ;
1919
2020describe ( 'docregistry/context' , ( ) => {
@@ -295,6 +295,99 @@ describe('docregistry/context', () => {
295295 await context . initialize ( false ) ;
296296 await expect ( context . ready ) . resolves . not . toThrow ( ) ;
297297 } ) ;
298+
299+ it ( 'should resolve if the file is renamed while it is loading' , async ( ) => {
300+ const oldPath = context . path ;
301+ const newPath = UUID . uuid4 ( ) + '.txt' ;
302+ await manager . contents . save ( oldPath , {
303+ type : factory . contentType ,
304+ format : factory . fileFormat ,
305+ content : 'foo'
306+ } ) ;
307+
308+ const get = manager . contents . get as jest . MockedFunction <
309+ typeof manager . contents . get
310+ > ;
311+ const getImplementation = get . getMockImplementation ( ) ;
312+ if ( ! getImplementation ) {
313+ throw new Error ( 'Missing contents get mock implementation' ) ;
314+ }
315+
316+ const started = new PromiseDelegate < void > ( ) ;
317+ const delayedGet = new PromiseDelegate < Contents . IModel > ( ) ;
318+ let isFirstGet = true ;
319+ get . mockImplementation ( ( path , options ) => {
320+ if ( path === oldPath && isFirstGet ) {
321+ isFirstGet = false ;
322+ started . resolve ( ) ;
323+ return delayedGet . promise ;
324+ }
325+ return getImplementation ( path , options ) ;
326+ } ) ;
327+
328+ try {
329+ const initialized = context . initialize ( false ) ;
330+ await started . promise ;
331+ await manager . contents . rename ( oldPath , newPath ) ;
332+ delayedGet . reject ( new Error ( 'Stale path' ) ) ;
333+
334+ await expect ( initialized ) . resolves . not . toThrow ( ) ;
335+ await expect ( context . ready ) . resolves . not . toThrow ( ) ;
336+ expect ( context . path ) . toBe ( newPath ) ;
337+ expect ( context . contentsModel ! . path ) . toBe ( newPath ) ;
338+ expect ( context . model . toString ( ) ) . toBe ( 'foo' ) ;
339+ expect (
340+ document . body . getElementsByClassName ( 'jp-Dialog' )
341+ ) . toHaveLength ( 0 ) ;
342+ } finally {
343+ get . mockImplementation ( getImplementation ) ;
344+ }
345+ } ) ;
346+
347+ it ( 'should ignore stale contents if the file is renamed while it is loading' , async ( ) => {
348+ const oldPath = context . path ;
349+ const newPath = UUID . uuid4 ( ) + '.txt' ;
350+ const staleContents = await manager . contents . save ( oldPath , {
351+ type : factory . contentType ,
352+ format : factory . fileFormat ,
353+ content : 'foo'
354+ } ) ;
355+
356+ const get = manager . contents . get as jest . MockedFunction <
357+ typeof manager . contents . get
358+ > ;
359+ const getImplementation = get . getMockImplementation ( ) ;
360+ if ( ! getImplementation ) {
361+ throw new Error ( 'Missing contents get mock implementation' ) ;
362+ }
363+
364+ const started = new PromiseDelegate < void > ( ) ;
365+ const delayedGet = new PromiseDelegate < Contents . IModel > ( ) ;
366+ let isFirstGet = true ;
367+ get . mockImplementation ( ( path , options ) => {
368+ if ( path === oldPath && isFirstGet ) {
369+ isFirstGet = false ;
370+ started . resolve ( ) ;
371+ return delayedGet . promise ;
372+ }
373+ return getImplementation ( path , options ) ;
374+ } ) ;
375+
376+ try {
377+ const initialized = context . initialize ( false ) ;
378+ await started . promise ;
379+ await manager . contents . rename ( oldPath , newPath ) ;
380+ delayedGet . resolve ( staleContents ) ;
381+
382+ await expect ( initialized ) . resolves . not . toThrow ( ) ;
383+ await expect ( context . ready ) . resolves . not . toThrow ( ) ;
384+ expect ( context . path ) . toBe ( newPath ) ;
385+ expect ( context . contentsModel ! . path ) . toBe ( newPath ) ;
386+ expect ( context . model . toString ( ) ) . toBe ( 'foo' ) ;
387+ } finally {
388+ get . mockImplementation ( getImplementation ) ;
389+ }
390+ } ) ;
298391 } ) ;
299392
300393 describe ( '#disposed' , ( ) => {
0 commit comments