Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add unit tests to feature flags #12503

Merged
merged 4 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions app/components/hooks/MinimumVersions/useMinimumVersions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,21 @@ describe('useMinimumVersions', () => {
jest.clearAllMocks();
(useNavigation as jest.Mock).mockReturnValue(mockNavigation);
});

it('requires update only if automaticSecurityChecksEnabled', () => {
(useSelector as jest.Mock).mockImplementation(() => ({
security: { automaticSecurityChecksEnabled: false },
featureFlags: {
featureFlags: { mobileMinimumVersions: { appMinimumBuild: 100 } },
engine: {
backgroundState: {
RemoteFeatureFlagController: {
remoteFeatureFlags: {
mobileMinimumVersions: {
appMinimumBuild: 100,
appleMinimumOS: 100,
androidMinimumAPIVersion: 100,
},
},
},
},
},
}));

Expand All @@ -54,8 +63,18 @@ describe('useMinimumVersions', () => {
it('requires update only if currentBuildNumber is lower than appMinimumBuild', () => {
(useSelector as jest.Mock).mockImplementation(() => ({
security: { automaticSecurityChecksEnabled: true },
featureFlags: {
featureFlags: { mobileMinimumVersions: { appMinimumBuild: 100 } },
engine: {
backgroundState: {
RemoteFeatureFlagController: {
remoteFeatureFlags: {
mobileMinimumVersions: {
appMinimumBuild: 100,
appleMinimumOS: 100,
androidMinimumAPIVersion: 100,
},
},
},
},
},
}));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
import { ControllerMessenger } from '@metamask/base-controller';
import {
RemoteFeatureFlagController,
RemoteFeatureFlagControllerMessenger,
} from '@metamask/remote-feature-flag-controller';
import { createRemoteFeatureFlagController } from './utils';

describe('RemoteFeatureFlagController utils', () => {
let messenger: RemoteFeatureFlagControllerMessenger;

beforeEach(() => {
messenger =
new ControllerMessenger() as unknown as RemoteFeatureFlagControllerMessenger;
jest.clearAllMocks();
});

describe('createRemoteFeatureFlagController', () => {
it('should create controller with correct config when enabled', () => {
process.env.METAMASK_ENVIRONMENT = 'production';
process.env.METAMASK_BUILD_TYPE = 'main';

const controller = createRemoteFeatureFlagController({
state: undefined,
messenger,
disabled: false,
});

expect(controller).toBeDefined();

// Initializing with am empty object should return an empty obj?
expect(controller.state).toStrictEqual({
cacheTimestamp: 0,
remoteFeatureFlags: {},
});
});

it('should handle initial state correctly', () => {
const initialState = {
remoteFeatureFlags: {
testFlag: true,
},
cacheTimestamp: 123,
};

const controller = createRemoteFeatureFlagController({
state: initialState,
messenger,
disabled: false,
});

expect(controller.state).toStrictEqual(initialState);
});

it('should call updateRemoteFeatureFlags when enabled', () => {
const spy = jest.spyOn(
RemoteFeatureFlagController.prototype,
'updateRemoteFeatureFlags',
);

createRemoteFeatureFlagController({
state: undefined,
messenger,
disabled: false,
});

expect(spy).toHaveBeenCalled();
});

it('should not throw when receive corrupted data', () => {
const initialState = {
corruptedData: true,
};

const controller = createRemoteFeatureFlagController({
// @ts-expect-error giving a wrong initial state
state: initialState,
messenger,
disabled: false,
});

expect(controller.state).toStrictEqual({
cacheTimestamp: 0,
corruptedData: true,
remoteFeatureFlags: {},
});
});

describe('environment handling', () => {
it('should use Development environment for local', () => {
process.env.METAMASK_ENVIRONMENT = 'local';
const controller = createRemoteFeatureFlagController({
state: undefined,
messenger,
disabled: false,
});

expect(controller).toBeDefined();
expect(controller.state).toStrictEqual({
cacheTimestamp: 0,
remoteFeatureFlags: {},
});
});

it('should use ReleaseCandidate environment for pre-release', () => {
process.env.METAMASK_ENVIRONMENT = 'pre-release';
const controller = createRemoteFeatureFlagController({
state: undefined,
messenger,
disabled: false,
});

expect(controller).toBeDefined();
expect(controller.state).toStrictEqual({
cacheTimestamp: 0,
remoteFeatureFlags: {},
});
});

it('should use Production environment for production', () => {
process.env.METAMASK_ENVIRONMENT = 'production';
const controller = createRemoteFeatureFlagController({
state: undefined,
messenger,
disabled: false,
});

expect(controller).toBeDefined();
expect(controller.state).toStrictEqual({
cacheTimestamp: 0,
remoteFeatureFlags: {},
});
});

it('should default to Development environment for unknown values', () => {
process.env.METAMASK_ENVIRONMENT = 'unknown';
const controller = createRemoteFeatureFlagController({
state: undefined,
messenger,
disabled: false,
});

expect(controller).toBeDefined();
expect(controller.state).toStrictEqual({
cacheTimestamp: 0,
remoteFeatureFlags: {},
});
});
});

describe('build type handling', () => {
it('should use Main distribution for main build type', () => {
process.env.METAMASK_BUILD_TYPE = 'main';
const controller = createRemoteFeatureFlagController({
state: undefined,
messenger,
disabled: false,
});

expect(controller).toBeDefined();
expect(controller.state).toStrictEqual({
cacheTimestamp: 0,
remoteFeatureFlags: {},
});
});

it('should use Flask distribution for flask build type', () => {
process.env.METAMASK_BUILD_TYPE = 'flask';
const controller = createRemoteFeatureFlagController({
state: undefined,
messenger,
disabled: false,
});

expect(controller).toBeDefined();
expect(controller.state).toStrictEqual({
cacheTimestamp: 0,
remoteFeatureFlags: {},
});
});
});
});
});
Loading