@@ -642,6 +642,86 @@ describe('VizPanel', () => {
642642 } ) ;
643643 } ) ;
644644
645+ describe ( 'Migration with shouldMigrate functionality' , ( ) => {
646+ let onPanelMigration : jest . Mock ;
647+ let shouldMigrate : jest . Mock ;
648+ let panel : VizPanel < OptionsPlugin1 , FieldConfigPlugin1 > ;
649+
650+ beforeEach ( ( ) => {
651+ onPanelMigration = jest . fn ( ) . mockReturnValue ( { option2 : 'migration option' } ) ;
652+ shouldMigrate = jest . fn ( ) ;
653+ } ) ;
654+
655+ it ( 'should call migration when shouldMigrate returns true even with same plugin version' , async ( ) => {
656+ panel = new VizPanel < OptionsPlugin1 , FieldConfigPlugin1 > ( {
657+ pluginId : 'custom-plugin-id' ,
658+ pluginVersion : '1.0.0' ,
659+ } ) ;
660+
661+ pluginToLoad = getTestPlugin1 ( ) ;
662+ pluginToLoad . onPanelMigration = onPanelMigration ;
663+ // @ts -expect-error
664+ pluginToLoad . shouldMigrate = shouldMigrate . mockReturnValue ( true ) ;
665+
666+ await panel . activate ( ) ;
667+
668+ expect ( onPanelMigration ) . toHaveBeenCalled ( ) ;
669+ expect ( shouldMigrate ) . toHaveBeenCalled ( ) ;
670+ expect ( panel . state . options . option2 ) . toBe ( 'migration option' ) ;
671+ } ) ;
672+
673+ it ( 'should run migration due to version change without calling shouldMigrate' , async ( ) => {
674+ panel = new VizPanel < OptionsPlugin1 , FieldConfigPlugin1 > ( {
675+ pluginId : 'custom-plugin-id' ,
676+ pluginVersion : '0.9.0' ,
677+ } ) ;
678+
679+ pluginToLoad = getTestPlugin1 ( ) ;
680+ pluginToLoad . onPanelMigration = onPanelMigration ;
681+ // @ts -expect-error
682+ pluginToLoad . shouldMigrate = shouldMigrate . mockReturnValue ( false ) ;
683+
684+ await panel . activate ( ) ;
685+
686+ expect ( onPanelMigration ) . toHaveBeenCalled ( ) ;
687+ expect ( shouldMigrate ) . not . toHaveBeenCalled ( ) ;
688+ expect ( panel . state . options . option2 ) . toBe ( 'migration option' ) ;
689+ } ) ;
690+
691+ it ( 'should not call migration when shouldMigrate returns false with same plugin version' , async ( ) => {
692+ panel = new VizPanel < OptionsPlugin1 , FieldConfigPlugin1 > ( {
693+ pluginId : 'custom-plugin-id' ,
694+ pluginVersion : '1.0.0' ,
695+ } ) ;
696+
697+ pluginToLoad = getTestPlugin1 ( ) ;
698+ pluginToLoad . onPanelMigration = onPanelMigration ;
699+ // @ts -expect-error
700+ pluginToLoad . shouldMigrate = shouldMigrate . mockReturnValue ( false ) ;
701+
702+ await panel . activate ( ) ;
703+
704+ expect ( shouldMigrate ) . toHaveBeenCalled ( ) ;
705+ expect ( onPanelMigration ) . not . toHaveBeenCalled ( ) ;
706+ expect ( panel . state . options . option2 ) . toBeUndefined ( ) ;
707+ } ) ;
708+
709+ it ( 'should work with existing migration when shouldMigrate is undefined' , async ( ) => {
710+ panel = new VizPanel < OptionsPlugin1 , FieldConfigPlugin1 > ( {
711+ pluginId : 'custom-plugin-id' ,
712+ pluginVersion : '0.9.0' ,
713+ } ) ;
714+
715+ pluginToLoad = getTestPlugin1 ( ) ;
716+ pluginToLoad . onPanelMigration = onPanelMigration ;
717+
718+ await panel . activate ( ) ;
719+
720+ expect ( onPanelMigration ) . toHaveBeenCalled ( ) ;
721+ expect ( panel . state . options . option2 ) . toBe ( 'migration option' ) ;
722+ } ) ;
723+ } ) ;
724+
645725 describe ( 'Should provide a panel context' , ( ) => {
646726 let panel : VizPanel < OptionsPlugin1 , FieldConfigPlugin1 > ;
647727
0 commit comments