Skip to content

Commit c1f8201

Browse files
refactor: clean up tests and resolve linting issues
1 parent 9f93669 commit c1f8201

2 files changed

Lines changed: 60 additions & 27 deletions

File tree

packages/remote-feature-flag-controller/src/remote-feature-flag-controller.test.ts

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import type {
55
MessengerEvents,
66
MockAnyNamespace,
77
} from '@metamask/messenger';
8-
import type { SemVerVersion } from '@metamask/utils';
98

109
import type { AbstractClientConfigApiService } from './client-config-api-service/abstract-client-config-api-service';
1110
import {
@@ -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 () => {

packages/remote-feature-flag-controller/src/remote-feature-flag-controller.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -205,21 +205,19 @@ export class RemoteFeatureFlagController extends BaseController<
205205
...state,
206206
};
207207

208-
const previousClientVersion = initialState.previousClientVersion;
209-
const hasPreviousClientVersion = typeof previousClientVersion === 'string';
210-
const hasValidPreviousClientVersion =
211-
hasPreviousClientVersion && isValidSemVerVersion(previousClientVersion);
212-
const shouldInvalidateCache =
213-
hasPreviousClientVersion &&
214-
(!hasValidPreviousClientVersion || previousClientVersion !== clientVersion);
208+
const hasClientVersionChanged =
209+
isValidSemVerVersion(initialState.previousClientVersion) &&
210+
initialState.previousClientVersion !== clientVersion;
215211

216212
super({
217213
name: controllerName,
218214
metadata: remoteFeatureFlagControllerMetadata,
219215
messenger,
220216
state: {
221217
...initialState,
222-
cacheTimestamp: shouldInvalidateCache ? 0 : initialState.cacheTimestamp,
218+
cacheTimestamp: hasClientVersionChanged
219+
? 0
220+
: initialState.cacheTimestamp,
223221
previousClientVersion: clientVersion,
224222
},
225223
});

0 commit comments

Comments
 (0)