Skip to content

Commit

Permalink
chore: Update useIsBFFEnabled
Browse files Browse the repository at this point in the history
  • Loading branch information
brobro10000 committed Feb 11, 2025
1 parent f7387f8 commit b97a32a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 67 deletions.
10 changes: 4 additions & 6 deletions src/components/app/data/hooks/useIsBFFEnabled.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { isBFFEnabled } from '../utils';
import useEnterpriseCustomer from './useEnterpriseCustomer';
import useEnterpriseFeatures from './useEnterpriseFeatures';
import { useLocation } from 'react-router-dom';
import { resolveBFFQuery } from '../queries';

export default function useIsBFFEnabled() {
const { data: enterpriseCustomer } = useEnterpriseCustomer();
const { data: enterpriseFeatures } = useEnterpriseFeatures();
return isBFFEnabled(enterpriseCustomer.uuid, enterpriseFeatures);
const location = useLocation();
return !!resolveBFFQuery(location.pathname);
}
36 changes: 0 additions & 36 deletions src/components/app/data/hooks/useIsBFFEnabled.test.js

This file was deleted.

53 changes: 53 additions & 0 deletions src/components/app/data/hooks/useIsBFFEnabled.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { renderHook } from '@testing-library/react-hooks';
import { QueryClientProvider } from '@tanstack/react-query';
import { MemoryRouter, useLocation } from 'react-router-dom';
import { AppContext } from '@edx/frontend-platform/react';
import useIsBFFEnabled from './useIsBFFEnabled';
import { queryEnterpriseLearnerDashboardBFF, resolveBFFQuery } from '../queries';
import { queryClient } from '../../../../utils/tests';
import { authenticatedUserFactory } from '../services/data/__factories__';

jest.mock('../queries', () => ({
...jest.requireActual('../queries'),
resolveBFFQuery: jest.fn(),
}));
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useLocation: jest.fn(),
}));
const mockAuthenticatedUser = authenticatedUserFactory();

describe('useIsBFFEnabled', () => {
const Wrapper = ({ routes = null, children }) => (
<QueryClientProvider client={queryClient()}>
<MemoryRouter initialEntries={[routes]}>
<AppContext.Provider value={{ authenticatedUser: mockAuthenticatedUser }}>
{children}
</AppContext.Provider>
</MemoryRouter>
</QueryClientProvider>
);
beforeEach(() => {
jest.clearAllMocks();
useLocation.mockReturnValue({ pathname: '/test-enterprise' });
});

it.each([
{ hasBFFEnabled: false },
{ hasBFFEnabled: true },
])('should return expected value based on whether BFF is enabled (%s)', ({ hasBFFEnabled }) => {
const route = hasBFFEnabled ? '/test-enterprise' : 'test-enterprise/search';

resolveBFFQuery.mockReturnValue(hasBFFEnabled ? queryEnterpriseLearnerDashboardBFF : null);
useLocation.mockReturnValue({ pathname: route });

const { result } = renderHook(() => useIsBFFEnabled(), {
wrapper: ({ children }) => Wrapper({
routes: route,
children,
}),
});

expect(result.current).toBe(hasBFFEnabled);
});
});
26 changes: 1 addition & 25 deletions src/components/app/data/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import dayjs from 'dayjs';
import { logError } from '@edx/frontend-platform/logging';

import { getConfig } from '@edx/frontend-platform/config';
import { POLICY_TYPES } from '../../enterprise-user-subsidy/enterprise-offers/data/constants';
import { LICENSE_STATUS } from '../../enterprise-user-subsidy/data/constants';
import {
Expand Down Expand Up @@ -929,31 +927,9 @@ export function transformCourseMetadataByAllocatedCourseRunAssignments({
return courseMetadata;
}

export function isBFFEnabledForEnterpriseCustomer(enterpriseCustomerUuid) {
const { FEATURE_ENABLE_BFF_API_FOR_ENTERPRISE_CUSTOMERS } = getConfig();
if (!FEATURE_ENABLE_BFF_API_FOR_ENTERPRISE_CUSTOMERS) {
return false;
}
return FEATURE_ENABLE_BFF_API_FOR_ENTERPRISE_CUSTOMERS.includes(enterpriseCustomerUuid);
}

export function isBFFEnabled(enterpriseCustomerUuid, enterpriseFeatures) {
// Check the following conditions:
// 1. BFF is enabled for the enterprise customer.
// 2. BFF is enabled for the request user via Waffle flag (supporting percentage-based rollout)
return true;
const isBFFEnabledForCustomer = isBFFEnabledForEnterpriseCustomer(enterpriseCustomerUuid);
if (isBFFEnabledForCustomer || enterpriseFeatures?.enterpriseLearnerBffEnabled) {
return true;
}

// Otherwise, BFF is not enabled.
return false;
}

/**
* Adds a subscription license to the subscription licenses grouped by status.
* @param {Oject} args
* @param {Object} args
* @param {Object} args.subscriptionLicensesByStatus - The subscription licenses grouped by status.
* @param {Object} args.subscriptionLicense - The subscription license to add to the subscription licenses by status.
* @returns {Object} - Returns the updated subscription licenses grouped by status.
Expand Down

0 comments on commit b97a32a

Please sign in to comment.