-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use correct enterpriseFeatures in loaders (#1241)
- Loading branch information
1 parent
400ae26
commit 71e99a3
Showing
7 changed files
with
141 additions
and
8 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
src/components/app/data/queries/extractEnterpriseFeatures.test.js
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 |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { when, resetAllWhenMocks } from 'jest-when'; | ||
import { authenticatedUserFactory, enterpriseCustomerFactory } from '../services/data/__factories__'; | ||
import extractEnterpriseFeatures from './extractEnterpriseFeatures'; | ||
import { queryEnterpriseLearner } from './queries'; | ||
|
||
const mockEnsureQueryData = jest.fn(); | ||
const mockQueryClient = { | ||
ensureQueryData: mockEnsureQueryData, | ||
}; | ||
const mockAuthenticatedUser = authenticatedUserFactory(); | ||
const mockEnterpriseCustomer = enterpriseCustomerFactory(); | ||
|
||
const getQueryEnterpriseLearner = ({ hasEnterpriseSlug = true } = {}) => queryEnterpriseLearner( | ||
mockAuthenticatedUser.username, | ||
hasEnterpriseSlug ? mockEnterpriseCustomer.slug : undefined, | ||
); | ||
|
||
describe('extractEnterpriseFeatures', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
resetAllWhenMocks(); | ||
}); | ||
|
||
it.each([ | ||
{ | ||
routeEnterpriseSlug: mockEnterpriseCustomer.slug, | ||
enterpriseFeatures: { featureA: true, featureB: false }, | ||
}, | ||
{ | ||
routeEnterpriseSlug: undefined, | ||
enterpriseFeatures: { featureA: true, featureB: false }, | ||
}, | ||
{ | ||
routeEnterpriseSlug: mockEnterpriseCustomer.slug, | ||
enterpriseFeatures: { featureA: true, featureB: false }, | ||
}, | ||
])('should return the correct enterprise features (%s)', async ({ | ||
routeEnterpriseSlug, | ||
enterpriseFeatures, | ||
}) => { | ||
const args = { | ||
queryClient: mockQueryClient, | ||
authenticatedUser: mockAuthenticatedUser, | ||
enterpriseSlug: routeEnterpriseSlug, | ||
}; | ||
|
||
const queryEnterpriseLearnerResult = { | ||
enterpriseFeatures, | ||
}; | ||
const queryEnterpriseLearnerQueryKey = getQueryEnterpriseLearner({ | ||
hasEnterpriseSlug: !!routeEnterpriseSlug, | ||
}).queryKey; | ||
|
||
when(mockEnsureQueryData) | ||
.calledWith( | ||
expect.objectContaining({ | ||
queryKey: queryEnterpriseLearnerQueryKey, | ||
}), | ||
) | ||
.mockResolvedValue(queryEnterpriseLearnerResult); | ||
|
||
const features = await extractEnterpriseFeatures(args); | ||
expect(features).toEqual(enterpriseFeatures); | ||
}); | ||
|
||
it('should throw an error if enterprise features cannot be found', async () => { | ||
const routeEnterpriseSlug = mockEnterpriseCustomer.slug; | ||
const args = { | ||
queryClient: mockQueryClient, | ||
authenticatedUser: mockAuthenticatedUser, | ||
enterpriseSlug: routeEnterpriseSlug, | ||
}; | ||
|
||
const queryEnterpriseLearnerQueryKey = getQueryEnterpriseLearner({ | ||
hasEnterpriseSlug: !!routeEnterpriseSlug, | ||
}).queryKey; | ||
|
||
when(mockEnsureQueryData) | ||
.calledWith( | ||
expect.objectContaining({ | ||
queryKey: queryEnterpriseLearnerQueryKey, | ||
}), | ||
) | ||
.mockRejectedValue(new Error('Could not retrieve enterprise features')); | ||
|
||
await expect(extractEnterpriseFeatures(args)).rejects.toThrow( | ||
'Could not retrieve enterprise features', | ||
); | ||
}); | ||
}); |
27 changes: 27 additions & 0 deletions
27
src/components/app/data/queries/extractEnterpriseFeatures.ts
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { queryEnterpriseLearner } from './queries'; | ||
|
||
interface ExtractEnterpriseCustomerArgs { | ||
queryClient: Types.QueryClient; | ||
authenticatedUser: Types.AuthenticatedUser; | ||
enterpriseSlug?: string; | ||
} | ||
|
||
/** | ||
* Extracts the enterpriseFeatures from the enterpriseLearnerData for the current user and enterprise slug. | ||
*/ | ||
async function extractEnterpriseFeatures({ | ||
queryClient, | ||
authenticatedUser, | ||
enterpriseSlug, | ||
} : ExtractEnterpriseCustomerArgs) : Promise<Types.EnterpriseFeatures> { | ||
// Retrieve linked enterprise customers for the current user from query cache, or | ||
// fetch from the server if not available. | ||
const linkedEnterpriseCustomersQuery = queryEnterpriseLearner(authenticatedUser.username, enterpriseSlug); | ||
const enterpriseLearnerData = await queryClient.ensureQueryData<Types.EnterpriseLearnerData>( | ||
linkedEnterpriseCustomersQuery, | ||
); | ||
const { enterpriseFeatures } = enterpriseLearnerData; | ||
return enterpriseFeatures; | ||
} | ||
|
||
export default extractEnterpriseFeatures; |
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
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