Skip to content

Commit

Permalink
chore: update additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brobro10000 committed Feb 11, 2025
1 parent 9205534 commit d344054
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 52 deletions.
1 change: 0 additions & 1 deletion src/components/app/data/hooks/useBFF.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export default function useBFF({
const matchedBFFQuery = resolveBFFQuery(
location.pathname,
);
console.log(enterpriseSlug, 'slug');
// Determine which query to call, the original hook or the new BFF
let queryConfig = {};
if (matchedBFFQuery) {
Expand Down
108 changes: 83 additions & 25 deletions src/components/app/data/hooks/useEnterpriseCustomer.test.jsx
Original file line number Diff line number Diff line change
@@ -1,53 +1,111 @@
import { renderHook } from '@testing-library/react-hooks';
import { QueryClientProvider } from '@tanstack/react-query';
import { AppContext } from '@edx/frontend-platform/react';
import { MemoryRouter, useParams } from 'react-router-dom';
import { authenticatedUserFactory, enterpriseCustomerFactory } from '../services/data/__factories__';
import useEnterpriseCustomer from './useEnterpriseCustomer';
import { queryClient } from '../../../../utils/tests';
import { fetchEnterpriseLearnerData } from '../services';
import { fetchEnterpriseLearnerDashboard, fetchEnterpriseLearnerData } from '../services';

jest.mock('../services', () => ({
...jest.requireActual('../services'),
fetchEnterpriseLearnerData: jest.fn().mockResolvedValue(null),
fetchEnterpriseLearnerDashboard: jest.fn().mockResolvedValue(null),
}));
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useParams: jest.fn(),
}));
const mockEnterpriseCustomer = enterpriseCustomerFactory();
const mockAuthenticatedUser = authenticatedUserFactory();
const mockEnterpriseLearnerData = {
enterpriseCustomer: mockEnterpriseCustomer,
enterpriseCustomerUserRoleAssignments: [],
activeEnterpriseCustomer: null,
activeEnterpriseCustomerUserRoleAssignments: [],
transformed: {
enterpriseCustomer: mockEnterpriseCustomer,
enterpriseCustomerUserRoleAssignments: [],
activeEnterpriseCustomer: null,
activeEnterpriseCustomerUserRoleAssignments: [],
allLinkedEnterpriseCustomerUsers: [],
staffEnterpriseCustomer: null,
enterpriseFeatures: {
isBFFEnabled: false,
},
shouldUpdateActiveEnterpriseCustomerUser: null,
},
};
const mockBFFDashboardData = {
enterpriseCustomer: {
...mockEnterpriseCustomer,
isBFFEnabled: true,
},
allLinkedEnterpriseCustomerUsers: [],
enterpriseFeatures: {},
staffEnterpriseCustomer: null,
enterpriseFeatures: {
isBFFEnabled: true,
},
shouldUpdateActiveEnterpriseCustomerUser: false,
enterpriseCustomerUserSubsidies: {
subscriptions: {
customerAgreement: {},
subscriptionLicenses: [],
subscriptionLicensesByStatus: {
activated: [],
assigned: [],
expired: [],
revoked: [],
},
},
},
enterpriseCourseEnrollments: [],
errors: [],
warnings: [],
};

const mockExpectedEnterpriseCustomers = (isMatchedRoute) => (isMatchedRoute
? mockBFFDashboardData.enterpriseCustomer
: mockEnterpriseLearnerData.transformed.enterpriseCustomer);

describe('useEnterpriseCustomer', () => {
const Wrapper = ({ children }) => (
const Wrapper = ({ routes = null, children }) => (
<QueryClientProvider client={queryClient()}>
<AppContext.Provider value={{ authenticatedUser: mockAuthenticatedUser }}>
{children}
</AppContext.Provider>
<MemoryRouter initialEntries={[routes]}>
<AppContext.Provider value={{ authenticatedUser: mockAuthenticatedUser }}>
{children}
</AppContext.Provider>
</MemoryRouter>
</QueryClientProvider>
);
beforeEach(() => {
jest.clearAllMocks();
fetchEnterpriseLearnerData.mockResolvedValue(mockEnterpriseLearnerData);
fetchEnterpriseLearnerDashboard.mockResolvedValue(mockBFFDashboardData);
useParams.mockReturnValue({ enterpriseSlug: 'test-slug' });
});
it('should return enterprise customer metadata correctly', async () => {
const { result, waitForNextUpdate } = renderHook(() => useEnterpriseCustomer(), { wrapper: Wrapper });
it.each([
{ isMatchedRoute: false },
{ isMatchedRoute: true },
])('should return enterprise customers correctly (%s)', async ({ isMatchedRoute }) => {
const mockSelect = jest.fn(data => data.transformed);
const { result, waitForNextUpdate } = renderHook(
() => {
if (isMatchedRoute) {
return useEnterpriseCustomer({ select: mockSelect });
}
return useEnterpriseCustomer();
},
{
wrapper: ({ children }) => Wrapper({
routes: isMatchedRoute ? '/test-enterprise' : 'test-enterprise/search',
children,
}),
},
);
await waitForNextUpdate();
const actualEnterpriseCustomer = result.current.data;
expect(actualEnterpriseCustomer.uuid).toEqual(mockEnterpriseCustomer.uuid);
expect(actualEnterpriseCustomer.slug).toEqual(mockEnterpriseCustomer.slug);
});
it('should return enterprise customer metadata correctly with select', async () => {
const { result, waitForNextUpdate } = renderHook(() => useEnterpriseCustomer({
select: (data) => data,
}), { wrapper: Wrapper });
await waitForNextUpdate();
const actualEnterpriseCustomerSelectArgs = result.current.data;
expect(actualEnterpriseCustomerSelectArgs.original).toEqual(mockEnterpriseLearnerData);
expect(actualEnterpriseCustomerSelectArgs.transformed).toEqual(mockEnterpriseCustomer);
if (isMatchedRoute) {
expect(mockSelect).toHaveBeenCalledTimes(2);
} else {
expect(mockSelect).toHaveBeenCalledTimes(0);
}

const actualEnterpriseFeatures = result.current.data;
expect(actualEnterpriseFeatures).toEqual(mockExpectedEnterpriseCustomers(isMatchedRoute));
});
});
2 changes: 2 additions & 0 deletions src/components/app/data/hooks/useEnterpriseFeatures.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const mockEnterpriseLearnerData = {
enterpriseFeatures: {
isBFFEnabled: false,
},
shouldUpdateActiveEnterpriseCustomerUser: null,
},
};

Expand All @@ -38,6 +39,7 @@ const mockBFFDashboardData = {
enterpriseFeatures: {
isBFFEnabled: true,
},
shouldUpdateActiveEnterpriseCustomerUser: false,
enterpriseCustomerUserSubsidies: {
subscriptions: {
customerAgreement: {},
Expand Down
87 changes: 72 additions & 15 deletions src/components/app/data/hooks/useEnterpriseLearner.test.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { renderHook } from '@testing-library/react-hooks';
import { QueryClientProvider } from '@tanstack/react-query';
import { AppContext } from '@edx/frontend-platform/react';
import { useParams } from 'react-router-dom';
import { MemoryRouter, useParams } from 'react-router-dom';
import { authenticatedUserFactory, enterpriseCustomerFactory } from '../services/data/__factories__';
import { queryClient } from '../../../../utils/tests';
import { fetchEnterpriseLearnerData } from '../services';
import { fetchEnterpriseLearnerDashboard, fetchEnterpriseLearnerData } from '../services';
import useEnterpriseLearner from './useEnterpriseLearner';

jest.mock('../services', () => ({
...jest.requireActual('../services'),
fetchEnterpriseLearnerData: jest.fn().mockResolvedValue(null),
fetchEnterpriseLearnerDashboard: jest.fn().mockResolvedValue(null),
}));
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
Expand All @@ -25,31 +26,87 @@ const mockEnterpriseLearnerData = {
allLinkedEnterpriseCustomerUsers: [],
enterpriseFeatures: {},
staffEnterpriseCustomer: null,
shouldUpdateActiveEnterpriseCustomerUser: null,
};

const mockBFFDashboardData = {
enterpriseCustomer: {
...mockEnterpriseCustomer,
isBFFEnabled: true,
},
allLinkedEnterpriseCustomerUsers: [],
enterpriseFeatures: {
isBFFEnabled: true,
},
shouldUpdateActiveEnterpriseCustomerUser: false,
enterpriseCustomerUserSubsidies: {
subscriptions: {
customerAgreement: {},
subscriptionLicenses: [],
subscriptionLicensesByStatus: {
activated: [],
assigned: [],
expired: [],
revoked: [],
},
},
},
enterpriseCourseEnrollments: [],
errors: [],
warnings: [],
};

const mockExpectedEnterpriseLearner = (isMatchedRoute) => (isMatchedRoute
? {
enterpriseCustomer: mockBFFDashboardData.enterpriseCustomer,
allLinkedEnterpriseCustomerUsers: mockBFFDashboardData.allLinkedEnterpriseCustomerUsers,
enterpriseFeatures: mockBFFDashboardData.enterpriseFeatures,
}
: mockEnterpriseLearnerData);

describe('useEnterpriseLearner', () => {
const Wrapper = ({ children }) => (
const Wrapper = ({ routes = null, children }) => (
<QueryClientProvider client={queryClient()}>
<AppContext.Provider value={{ authenticatedUser: mockAuthenticatedUser }}>
{children}
</AppContext.Provider>
<MemoryRouter initialEntries={[routes]}>
<AppContext.Provider value={{ authenticatedUser: mockAuthenticatedUser }}>
{children}
</AppContext.Provider>
</MemoryRouter>
</QueryClientProvider>
);
beforeEach(() => {
jest.clearAllMocks();
fetchEnterpriseLearnerData.mockResolvedValue(mockEnterpriseLearnerData);
fetchEnterpriseLearnerDashboard.mockResolvedValue(mockBFFDashboardData);
useParams.mockReturnValue({ enterpriseSlug: mockEnterpriseCustomer.slug });
});
it('should handle resolved value correctly', async () => {
const { result, waitForNextUpdate } = renderHook(() => useEnterpriseLearner(), { wrapper: Wrapper });
it.each([
{ isMatchedRoute: false },
{ isMatchedRoute: true },
])('should return enterprise learners correctly (%s)', async ({ isMatchedRoute }) => {
const mockSelect = jest.fn(data => data.transformed);
const { result, waitForNextUpdate } = renderHook(
() => {
if (isMatchedRoute) {
return useEnterpriseLearner({ select: mockSelect });
}
return useEnterpriseLearner();
},
{
wrapper: ({ children }) => Wrapper({
routes: isMatchedRoute ? '/test-enterprise' : 'test-enterprise/search',
children,
}),
},
);
await waitForNextUpdate();
if (isMatchedRoute) {
expect(mockSelect).toHaveBeenCalledTimes(2);
} else {
expect(mockSelect).toHaveBeenCalledTimes(0);
}

expect(result.current).toEqual(
expect.objectContaining({
data: mockEnterpriseLearnerData,
isLoading: false,
isFetching: false,
}),
);
const actualEnterpriseFeatures = result.current.data;
expect(actualEnterpriseFeatures).toEqual(mockExpectedEnterpriseLearner(isMatchedRoute));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ describe('fetchEnterpriseLearnerData', () => {
enterpriseCustomer: expectedEnterpriseCustomer,
})),
staffEnterpriseCustomer: isStaffUser ? expectedEnterpriseCustomer : undefined,
shouldUpdateActiveEnterpriseCustomerUser: null,
});
});

Expand All @@ -192,6 +193,7 @@ describe('fetchEnterpriseLearnerData', () => {
activeEnterpriseCustomerUserRoleAssignments: [],
allLinkedEnterpriseCustomerUsers: [],
staffEnterpriseCustomer: null,
shouldUpdateActiveEnterpriseCustomerUser: null,
});
});
});
Expand Down
2 changes: 0 additions & 2 deletions src/components/app/routes/loaders/rootLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ const makeRootLoader: Types.MakeRouteLoaderFunctionWithQueryClient = function ma
);
}

console.log(enterpriseLearnerData);

let {
enterpriseCustomer,
activeEnterpriseCustomer,
Expand Down
Loading

0 comments on commit d344054

Please sign in to comment.