@@ -15,7 +15,8 @@ import {
1515 extractStorybookPaths ,
1616 runGraphGeneration ,
1717 maybeWriteTrace ,
18- applyIntelliStory
18+ applyIntelliStory ,
19+ writeIntelliStoryTrace
1920} from '../src/intelliStory.js' ;
2021
2122const NODE_MAJOR = parseInt ( process . versions . node . split ( '.' ) [ 0 ] , 10 ) ;
@@ -401,6 +402,67 @@ describe('intelliStory', () => {
401402 } ) ;
402403 } ) ;
403404
405+ describe ( 'writeIntelliStoryTrace()' , ( ) => {
406+ beforeEach ( ( ) => jasmine . clock ( ) . install ( ) ) ;
407+ afterEach ( ( ) => jasmine . clock ( ) . uninstall ( ) ) ;
408+
409+ const fullData = {
410+ affected_stories : [ ] ,
411+ vertices : [ { kind : 'component' , file_path : 'A.jsx' } ] ,
412+ edges : [ ] ,
413+ transitive_closure_matrix_sparse : [ ]
414+ } ;
415+
416+ // Flush microtasks between clock ticks so the poll loop advances.
417+ async function drainPolls ( promise , rounds = 20 ) {
418+ for ( let i = 0 ; i < rounds ; i ++ ) {
419+ await Promise . resolve ( ) ;
420+ await Promise . resolve ( ) ;
421+ jasmine . clock ( ) . tick ( 5000 ) ;
422+ }
423+ return promise ;
424+ }
425+
426+ it ( 'is a no-op when trace is disabled (defaults its logger and config)' , async ( ) => {
427+ let getStatus = jasmine . createSpy ( 'getStatus' ) ;
428+ // no config and no log arg — exercises `intelliStoryConfig || {}` and the default logger param
429+ await writeIntelliStoryTrace ( { build : { id : '1' } , client : { getStatus } } ) ;
430+ expect ( getStatus ) . not . toHaveBeenCalled ( ) ;
431+ } ) ;
432+
433+ it ( 'is a no-op when the Percy build was never created' , async ( ) => {
434+ let log = mockLog ( ) ;
435+ let getStatus = jasmine . createSpy ( 'getStatus' ) ;
436+ await writeIntelliStoryTrace ( { client : { getStatus } } , { trace : true } , log ) ;
437+ expect ( getStatus ) . not . toHaveBeenCalled ( ) ;
438+ } ) ;
439+
440+ it ( 'skips the trace when the graph reports failed' , async ( ) => {
441+ let log = mockLog ( ) ;
442+ let write = spyOn ( fs , 'writeFileSync' ) ;
443+ let percy = { build : { id : '1' } , client : { getStatus : async ( ) => ( { status : 'failed' } ) } } ;
444+ await writeIntelliStoryTrace ( percy , { trace : true } , log ) ;
445+ expect ( write ) . not . toHaveBeenCalled ( ) ;
446+ expect ( log . debug ) . toHaveBeenCalled ( ) ;
447+ } ) ;
448+
449+ it ( 'skips the trace when polling times out' , async ( ) => {
450+ let log = mockLog ( ) ;
451+ let write = spyOn ( fs , 'writeFileSync' ) ;
452+ let percy = { build : { id : '1' } , client : { getStatus : async ( ) => ( { status : 'in_progress' } ) } } ;
453+ await drainPolls ( writeIntelliStoryTrace ( percy , { trace : true } , log ) ) ;
454+ expect ( write ) . not . toHaveBeenCalled ( ) ;
455+ } ) ;
456+
457+ it ( 'fetches the finalized graph data and writes the trace when done' , async ( ) => {
458+ let log = mockLog ( ) ;
459+ let write = spyOn ( fs , 'writeFileSync' ) ;
460+ let percy = { build : { id : '1' } , client : { getStatus : async ( ) => ( { status : 'done' , data : fullData } ) } } ;
461+ await writeIntelliStoryTrace ( percy , { trace : true } , log ) ;
462+ expect ( write ) . toHaveBeenCalledTimes ( 1 ) ;
463+ } ) ;
464+ } ) ;
465+
404466 describe ( 'runGraphGeneration() polling' , ( ) => {
405467 beforeEach ( ( ) => jasmine . clock ( ) . install ( ) ) ;
406468 afterEach ( ( ) => jasmine . clock ( ) . uninstall ( ) ) ;
0 commit comments