@@ -7,6 +7,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
77import { VisualSkinConfigSection } from './VisualSkinConfigSection' ;
88
99const activateMock = vi . hoisted ( ( ) => vi . fn ( ) ) ;
10+ const deactivateMock = vi . hoisted ( ( ) => vi . fn ( ) ) ;
1011const previewMock = vi . hoisted ( ( ) => vi . fn ( ) ) ;
1112const cancelPreviewMock = vi . hoisted ( ( ) => vi . fn ( ) ) ;
1213const importMock = vi . hoisted ( ( ) => vi . fn ( ) ) ;
@@ -43,6 +44,7 @@ vi.mock('@/infrastructure/visual-skin', () => ({
4344 watchProjection : watchProjectionMock ,
4445 importPackage : importMock ,
4546 activate : activateMock ,
47+ deactivate : deactivateMock ,
4648 preview : previewMock ,
4749 cancelPreview : cancelPreviewMock ,
4850 remove : removeMock ,
@@ -62,6 +64,7 @@ describe('VisualSkinConfigSection', () => {
6264 beforeEach ( ( ) => {
6365 globalThis . IS_REACT_ACT_ENVIRONMENT = true ;
6466 activateMock . mockReset ( ) ;
67+ deactivateMock . mockReset ( ) ;
6568 previewMock . mockReset ( ) ;
6669 cancelPreviewMock . mockReset ( ) ;
6770 importMock . mockReset ( ) ;
@@ -96,6 +99,46 @@ describe('VisualSkinConfigSection', () => {
9699 const html = renderToStaticMarkup ( < VisualSkinConfigSection /> ) ;
97100 expect ( html ) . toContain ( 'data-testid="visual-skin-empty"' ) ;
98101 } ) ;
102+
103+ it . each ( [ 'active' , 'preview' ] as const ) ( 'offers deactivation while %s without removing imported skins' , async state => {
104+ if ( state === 'preview' ) {
105+ skinState . activeSkinId = null ;
106+ skinState . previewSkinId = 'sample.skin' ;
107+ }
108+ await mount ( ) ;
109+ const button = container . querySelector < HTMLButtonElement > ( '[data-testid="visual-skin-deactivate"]' ) ! ;
110+ expect ( button . textContent ) . toBe ( 'skin.deactivate' ) ;
111+ await act ( async ( ) => button . click ( ) ) ;
112+ expect ( deactivateMock ) . toHaveBeenCalledTimes ( 1 ) ;
113+ expect ( removeMock ) . not . toHaveBeenCalled ( ) ;
114+ expect ( container . querySelector ( '[data-skin-id="sample.skin"]' ) ) . not . toBeNull ( ) ;
115+ } ) ;
116+
117+ it ( 'disables deactivation once no skin or preview is selected' , async ( ) => {
118+ skinState . activeSkinId = null ;
119+ await mount ( ) ;
120+ const button = container . querySelector < HTMLButtonElement > ( '[data-testid="visual-skin-deactivate"]' ) ! ;
121+ expect ( button . disabled ) . toBe ( true ) ;
122+ await act ( async ( ) => button . click ( ) ) ;
123+ expect ( deactivateMock ) . not . toHaveBeenCalled ( ) ;
124+ } ) ;
125+
126+ it ( 'prevents duplicate deactivation while persistence is pending and allows retry after failure' , async ( ) => {
127+ let reject ! : ( error : Error ) => void ;
128+ deactivateMock . mockImplementationOnce ( ( ) => new Promise < void > ( ( _ , rejectAction ) => { reject = rejectAction ; } ) ) ;
129+ await mount ( ) ;
130+ const button = container . querySelector < HTMLButtonElement > ( '[data-testid="visual-skin-deactivate"]' ) ! ;
131+ await act ( async ( ) => button . click ( ) ) ;
132+ expect ( button . disabled ) . toBe ( true ) ;
133+ await act ( async ( ) => button . click ( ) ) ;
134+ expect ( deactivateMock ) . toHaveBeenCalledTimes ( 1 ) ;
135+ await act ( async ( ) => reject ( new Error ( 'Unable to clear selection' ) ) ) ;
136+ expect ( button . disabled ) . toBe ( false ) ;
137+ expect ( container . querySelector ( '[role="alert"]' ) ?. textContent ) . toContain ( 'Unable to clear selection' ) ;
138+ await act ( async ( ) => button . click ( ) ) ;
139+ expect ( deactivateMock ) . toHaveBeenCalledTimes ( 2 ) ;
140+ expect ( container . querySelector ( '[role="alert"]' ) ) . toBeNull ( ) ;
141+ } ) ;
99142 it ( 'distinguishes the saved skin from a temporary preview and keeps every card action reachable' , async ( ) => {
100143 skinState . skins . push ( { id : 'other.skin' , name : 'Other skin' , description : 'A long reference description' , version : '2' , author : 'Artist' } ) ;
101144 skinState . previewSkinId = 'other.skin' ;
0 commit comments