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 2c11fe8
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 91 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
4 changes: 2 additions & 2 deletions src/components/app/data/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,7 @@ describe('resolveBFFQuery', () => {
}
return null;
});
const result = resolveBFFQuery(pathname, { enterpriseCustomerUuid: mockEnterpriseCustomer.uuid });
const result = resolveBFFQuery(pathname);
expect(matchPath).toHaveBeenCalledWith('/:enterpriseSlug', pathname);
expect(result({ enterpriseSlug: 'testEnterpriseSlug' }).queryKey).toEqual(expectedQueryKey);
});
Expand All @@ -1045,7 +1045,7 @@ describe('resolveBFFQuery', () => {
}
return null;
});
const result = resolveBFFQuery(pathname, { enterpriseCustomerUuid: mockEnterpriseCustomer.uuid });
const result = resolveBFFQuery(pathname);
expect(matchPath).toHaveBeenCalledWith('/:enterpriseSlug', pathname);
expect(result).toEqual(null);
});
Expand Down
10 changes: 1 addition & 9 deletions src/components/app/routes/data/utils.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { generatePath, matchPath, redirect } from 'react-router-dom';
import { getConfig } from '@edx/frontend-platform';
import {
fetchAuthenticatedUser,
getLoginRedirectUrl,
} from '@edx/frontend-platform/auth';
import { fetchAuthenticatedUser, getLoginRedirectUrl } from '@edx/frontend-platform/auth';
import { getProxyLoginUrl } from '@edx/frontend-enterprise-logistration';
import Cookies from 'universal-cookie';

Expand Down Expand Up @@ -41,14 +38,9 @@ export async function ensureEnterpriseAppData({
userEmail,
queryClient,
requestUrl,
enterpriseFeatures,
}) {
const matchedBFFQuery = resolveBFFQuery(
requestUrl.pathname,
{
enterpriseCustomerUuid: enterpriseCustomer.uuid,
enterpriseFeatures,
},
);
const enterpriseAppDataQueries = [];
if (!matchedBFFQuery) {
Expand Down
1 change: 0 additions & 1 deletion src/components/app/routes/loaders/rootLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ const makeRootLoader: Types.MakeRouteLoaderFunctionWithQueryClient = function ma
userEmail,
queryClient,
requestUrl,
enterpriseFeatures,
});

// Redirect to the same URL without a trailing slash, if applicable.
Expand Down
12 changes: 0 additions & 12 deletions src/components/dashboard/data/dashboardLoader.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ensureAuthenticatedUser, redirectToSearchPageForNewUser } from '../../app/routes/data';
import {
extractEnterpriseCustomer,
extractEnterpriseFeatures,
queryEnterpriseCourseEnrollments,
queryEnterprisePathwaysList,
queryEnterpriseProgramsList,
Expand Down Expand Up @@ -40,20 +39,9 @@ const makeDashboardLoader: Types.MakeRouteLoaderFunctionWithQueryClient = functi
enterpriseSlug,
});

// Extract enterprise features.
const enterpriseFeatures = await extractEnterpriseFeatures({
queryClient,
authenticatedUser,
enterpriseSlug,
});

// Attempt to resolve the BFF query for the dashboard.
const dashboardBFFQuery = resolveBFFQuery(
requestUrl.pathname,
{
enterpriseCustomerUuid: enterpriseCustomer.uuid,
enterpriseFeatures,
},
);

// Load enrollments, policies, and conditionally redirect for new users
Expand Down

0 comments on commit 2c11fe8

Please sign in to comment.