-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9205534
commit d344054
Showing
7 changed files
with
197 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 83 additions & 25 deletions
108
src/components/app/data/hooks/useEnterpriseCustomer.test.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.