@@ -5,7 +5,6 @@ import type {
55 MessengerEvents ,
66 MockAnyNamespace ,
77} from '@metamask/messenger' ;
8- import type { SemVerVersion } from '@metamask/utils' ;
98
109import type { AbstractClientConfigApiService } from './client-config-api-service/abstract-client-config-api-service' ;
1110import {
@@ -192,7 +191,7 @@ describe('RemoteFeatureFlagController', () => {
192191 expect ( controller . state . remoteFeatureFlags ) . toStrictEqual ( MOCK_FLAGS ) ;
193192 } ) ;
194193
195- it ( 'invalidates the cache when clientVersion changes and fetches new flags ' , async ( ) => {
194+ it ( 'resets cache and fetch when clientVersion changes' , async ( ) => {
196195 const versionedFlags = {
197196 exploreFeature : {
198197 versions : {
@@ -204,27 +203,63 @@ describe('RemoteFeatureFlagController', () => {
204203 const clientConfigApiService = buildClientConfigApiService ( {
205204 remoteFeatureFlags : versionedFlags ,
206205 } ) ;
207- const controller = createController ( {
208- clientConfigApiService,
209- clientVersion : '7.64.0' ,
210- state : {
211- previousClientVersion : '7.62.0' as SemVerVersion ,
212- remoteFeatureFlags : { exploreFeature : { enabled : false } } ,
213- cacheTimestamp : Date . now ( ) ,
214- } ,
215- } ) ;
216206
217- expect ( controller . state . cacheTimestamp ) . toBe ( 0 ) ;
207+ /**
208+ * Test Util - Arrange, Act, Assert for client version change
209+ *
210+ * @param opts - The options for the arrangeActAssertClientVersionChange test
211+ * @param opts.clientVersion - The client version to use
212+ * @param opts.controllerState - The controller state to use
213+ * @param opts.expectedFeatureFlagState - The expected feature flag state
214+ */
215+ const arrangeActAssertClientVersionChange = async ( opts : {
216+ clientVersion : string ;
217+ controllerState ?: RemoteFeatureFlagControllerState ;
218+ expectedFeatureFlagState : boolean ;
219+ } ) : Promise < void > => {
220+ const { clientVersion, controllerState, expectedFeatureFlagState } =
221+ opts ;
222+
223+ // Arrange - setup controller
224+ jest . clearAllMocks ( ) ;
225+ const controller = createController ( {
226+ clientConfigApiService,
227+ clientVersion,
228+ state : controllerState ,
229+ } ) ;
218230
219- await controller . updateRemoteFeatureFlags ( ) ;
231+ // Assert - controller cache is set to 0 (either by initial state or reset by client version change)
232+ expect ( controller . state . cacheTimestamp ) . toBe ( 0 ) ;
220233
221- expect (
222- clientConfigApiService . fetchRemoteFeatureFlags ,
223- ) . toHaveBeenCalledTimes ( 1 ) ;
224- expect ( controller . state . remoteFeatureFlags . exploreFeature ) . toStrictEqual ( {
225- enabled : true ,
234+ // Act / Assert - We should make a network request and update the cache (cache is reset to 0)
235+ await controller . updateRemoteFeatureFlags ( ) ;
236+ expect (
237+ clientConfigApiService . fetchRemoteFeatureFlags ,
238+ ) . toHaveBeenCalledTimes ( 1 ) ;
239+
240+ // Act / Assert - subsequent fetches should not call network again (cache is populated)
241+ await controller . updateRemoteFeatureFlags ( ) ;
242+ expect (
243+ clientConfigApiService . fetchRemoteFeatureFlags ,
244+ ) . toHaveBeenCalledTimes ( 1 ) ;
245+
246+ // Assert - flag state is as expected
247+ expect (
248+ controller . state . remoteFeatureFlags . exploreFeature ,
249+ ) . toStrictEqual ( {
250+ enabled : expectedFeatureFlagState ,
251+ } ) ;
252+ } ;
253+
254+ await arrangeActAssertClientVersionChange ( {
255+ clientVersion : '7.62.0' ,
256+ expectedFeatureFlagState : false ,
257+ } ) ;
258+
259+ await arrangeActAssertClientVersionChange ( {
260+ clientVersion : '7.64.0' ,
261+ expectedFeatureFlagState : true ,
226262 } ) ;
227- expect ( controller . state . previousClientVersion ) . toBe ( '7.64.0' ) ;
228263 } ) ;
229264
230265 it ( 'makes a network request to fetch when cache is expired, and then updates the cache' , async ( ) => {
0 commit comments