1+ import { siteHasFeature } from '@automattic/jetpack-script-data' ;
12import { act , renderHook } from '@testing-library/react' ;
23import { useDispatch , useSelect } from '@wordpress/data' ;
34import { usePerNetworkCustomization } from '../' ;
45
6+ jest . mock ( '@automattic/jetpack-script-data' , ( ) => {
7+ const actual = jest . requireActual ( '@automattic/jetpack-script-data' ) ;
8+ return {
9+ ...actual ,
10+ siteHasFeature : jest . fn ( ) ,
11+ } ;
12+ } ) ;
13+
514jest . mock ( '@wordpress/data' , ( ) => {
615 const actual = jest . requireActual ( '@wordpress/data' ) ;
716 const mocks = {
@@ -21,37 +30,29 @@ jest.mock( '../../use-featured-image', () => jest.fn( () => null ) );
2130// Mock useMediaDetails to avoid nested useSelect calls
2231jest . mock ( '../../use-media-details' , ( ) => jest . fn ( ( ) => [ null ] ) ) ;
2332
33+ const mockUsePostMeta = jest . fn ( ) ;
34+
2435// Mock usePostMeta to avoid nested hook calls
2536jest . mock ( '../../use-post-meta' , ( ) => ( {
26- usePostMeta : jest . fn ( ( ) => ( {
27- attachedMedia : [ ] ,
28- imageGeneratorSettings : { enabled : false } ,
29- mediaSource : undefined ,
30- shareMessage : '' ,
31- } ) ) ,
37+ usePostMeta : ( ) => mockUsePostMeta ( ) ,
3238} ) ) ;
3339
3440// Mock useAnalytics to avoid deep dependency chain
3541jest . mock ( '@automattic/jetpack-shared-extension-utils' , ( ) => ( {
3642 useAnalytics : jest . fn ( ( ) => ( { recordEvent : jest . fn ( ) } ) ) ,
3743} ) ) ;
3844
39- // Mock hasSocialPaidFeatures to return true by default
40- jest . mock ( '../../../utils' , ( ) => {
41- const actual = jest . requireActual ( '../../../utils' ) ;
42- return {
43- ...actual ,
44- hasSocialPaidFeatures : jest . fn ( ( ) => true ) ,
45- } ;
46- } ) ;
47-
4845const mockUseDispatch = useDispatch as jest . Mock ;
4946const mockUseSelect = useSelect as jest . Mock ;
47+ const mockSiteHasFeature = siteHasFeature as jest . Mock ;
5048
51- const createMockSelect = ( meta : Record < string , unknown > = { } ) => {
49+ const createMockSelect = (
50+ meta : Record < string , unknown > = { } ,
51+ connections : Array < Record < string , unknown > > = [ ]
52+ ) => {
5253 return ( ) => ( {
5354 getEditedPostAttribute : jest . fn ( ) . mockReturnValue ( meta ) ,
54- getConnections : jest . fn ( ) . mockReturnValue ( [ ] ) ,
55+ getConnections : jest . fn ( ) . mockReturnValue ( connections ) ,
5556 getEnabledConnections : jest . fn ( ) . mockReturnValue ( [ ] ) ,
5657 getDisabledConnections : jest . fn ( ) . mockReturnValue ( [ ] ) ,
5758 } ) ;
@@ -68,6 +69,18 @@ describe( 'usePerNetworkCustomization', () => {
6869 editPost : mockEditPost ,
6970 customizeConnectionById : mockCustomizeConnectionById ,
7071 } ) ;
72+
73+ mockUsePostMeta . mockReturnValue ( {
74+ attachedMedia : [ ] ,
75+ imageGeneratorSettings : { enabled : false } ,
76+ jetpackSocialOptions : { } ,
77+ mediaSource : undefined ,
78+ shareMessage : '' ,
79+ } ) ;
80+
81+ mockSiteHasFeature . mockImplementation ( feature =>
82+ [ 'social-message-templates' , 'social-enhanced-publishing' ] . includes ( feature )
83+ ) ;
7184 } ) ;
7285
7386 it ( 'should return isEnabled as false when meta key is not set' , ( ) => {
@@ -78,6 +91,7 @@ describe( 'usePerNetworkCustomization', () => {
7891 const { result } = renderHook ( ( ) => usePerNetworkCustomization ( ) ) ;
7992
8093 expect ( result . current . isEnabled ) . toBe ( false ) ;
94+ expect ( mockEditPost ) . not . toHaveBeenCalled ( ) ;
8195 } ) ;
8296
8397 it ( 'should return isEnabled as true when meta key is true' , ( ) => {
@@ -92,6 +106,143 @@ describe( 'usePerNetworkCustomization', () => {
92106 const { result } = renderHook ( ( ) => usePerNetworkCustomization ( ) ) ;
93107
94108 expect ( result . current . isEnabled ) . toBe ( true ) ;
109+ expect ( mockEditPost ) . not . toHaveBeenCalled ( ) ;
110+ } ) ;
111+
112+ it ( 'should default to enabled when message templates are enabled and a connection has a custom template' , ( ) => {
113+ mockUseSelect . mockImplementation ( ( selector : ( select : unknown ) => unknown ) => {
114+ return selector (
115+ createMockSelect (
116+ {
117+ _wpas_customize_per_network : false ,
118+ } ,
119+ [
120+ {
121+ connection_id : 'connection-1' ,
122+ template : 'Custom template' ,
123+ } ,
124+ ]
125+ )
126+ ) ;
127+ } ) ;
128+
129+ const { result } = renderHook ( ( ) => usePerNetworkCustomization ( ) ) ;
130+
131+ expect ( result . current . isEnabled ) . toBe ( true ) ;
132+ expect ( mockEditPost ) . toHaveBeenCalledWith ( {
133+ meta : {
134+ _wpas_customize_per_network : true ,
135+ } ,
136+ } ) ;
137+ } ) ;
138+
139+ it ( 'should respect an explicitly saved global mode when a connection has a custom template' , ( ) => {
140+ mockUsePostMeta . mockReturnValue ( {
141+ attachedMedia : [ ] ,
142+ imageGeneratorSettings : { enabled : false } ,
143+ jetpackSocialOptions : {
144+ customize_per_network_user_set : true ,
145+ } ,
146+ mediaSource : undefined ,
147+ shareMessage : '' ,
148+ } ) ;
149+ mockUseSelect . mockImplementation ( ( selector : ( select : unknown ) => unknown ) => {
150+ return selector (
151+ createMockSelect (
152+ {
153+ _wpas_customize_per_network : false ,
154+ } ,
155+ [
156+ {
157+ connection_id : 'connection-1' ,
158+ template : 'Custom template' ,
159+ } ,
160+ ]
161+ )
162+ ) ;
163+ } ) ;
164+
165+ const { result } = renderHook ( ( ) => usePerNetworkCustomization ( ) ) ;
166+
167+ expect ( result . current . isEnabled ) . toBe ( false ) ;
168+ expect ( mockEditPost ) . not . toHaveBeenCalled ( ) ;
169+ } ) ;
170+
171+ it ( 'should not default to enabled when the connection template is blank' , ( ) => {
172+ mockUseSelect . mockImplementation ( ( selector : ( select : unknown ) => unknown ) => {
173+ return selector (
174+ createMockSelect ( { } , [
175+ {
176+ connection_id : 'connection-1' ,
177+ template : ' ' ,
178+ } ,
179+ ] )
180+ ) ;
181+ } ) ;
182+
183+ const { result } = renderHook ( ( ) => usePerNetworkCustomization ( ) ) ;
184+
185+ expect ( result . current . isEnabled ) . toBe ( false ) ;
186+ expect ( mockEditPost ) . not . toHaveBeenCalled ( ) ;
187+ } ) ;
188+
189+ it ( 'should not default to enabled when message templates are disabled' , ( ) => {
190+ mockSiteHasFeature . mockReturnValue ( false ) ;
191+ mockUseSelect . mockImplementation ( ( selector : ( select : unknown ) => unknown ) => {
192+ return selector (
193+ createMockSelect ( { } , [
194+ {
195+ connection_id : 'connection-1' ,
196+ template : 'Custom template' ,
197+ } ,
198+ ] )
199+ ) ;
200+ } ) ;
201+
202+ const { result } = renderHook ( ( ) => usePerNetworkCustomization ( ) ) ;
203+
204+ expect ( result . current . isEnabled ) . toBe ( false ) ;
205+ expect ( mockEditPost ) . not . toHaveBeenCalled ( ) ;
206+ } ) ;
207+
208+ it ( 'should allow turning off the template-based default for the current editor session' , ( ) => {
209+ let meta = { } ;
210+ const connections = [
211+ {
212+ connection_id : 'connection-1' ,
213+ template : 'Custom template' ,
214+ } ,
215+ ] ;
216+
217+ mockEditPost . mockImplementation ( ( update : { meta : Record < string , unknown > } ) => {
218+ meta = {
219+ ...meta ,
220+ ...update . meta ,
221+ } ;
222+ } ) ;
223+ mockUseSelect . mockImplementation ( ( selector : ( select : unknown ) => unknown ) => {
224+ return selector ( createMockSelect ( meta , connections ) ) ;
225+ } ) ;
226+
227+ const { result, rerender } = renderHook ( ( ) => usePerNetworkCustomization ( ) ) ;
228+
229+ expect ( result . current . isEnabled ) . toBe ( true ) ;
230+
231+ act ( ( ) => {
232+ result . current . toggle ( ) ;
233+ } ) ;
234+ rerender ( ) ;
235+
236+ expect ( result . current . isEnabled ) . toBe ( false ) ;
237+ expect ( mockEditPost ) . toHaveBeenLastCalledWith ( {
238+ meta : {
239+ _wpas_customize_per_network : false ,
240+ jetpack_social_options : {
241+ customize_per_network_user_set : true ,
242+ version : 2 ,
243+ } ,
244+ } ,
245+ } ) ;
95246 } ) ;
96247
97248 it ( 'should toggle the meta value when toggle is called' , ( ) => {
@@ -112,6 +263,10 @@ describe( 'usePerNetworkCustomization', () => {
112263 expect ( mockEditPost ) . toHaveBeenCalledWith ( {
113264 meta : {
114265 _wpas_customize_per_network : true ,
266+ jetpack_social_options : {
267+ customize_per_network_user_set : true ,
268+ version : 2 ,
269+ } ,
115270 } ,
116271 } ) ;
117272 } ) ;
0 commit comments