Skip to content

Commit de75b52

Browse files
committed
squash!: Improve way of mocking validateUserPermissions, remove unneeded changes
1 parent 9521819 commit de75b52

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

src/library-authoring/create-container/CreateContainerModal.test.tsx

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,10 @@ describe('CreateContainerModal container linking', () => {
8080
const createButton = await screen.findByRole('button', { name: /create/i });
8181
await user.click(createButton);
8282
await waitFor(() => {
83-
const request = axiosMock.history.post.find(req => req.url.match(/\/api\/libraries\/.*\/containers/));
84-
expect(request).toBeDefined();
83+
expect(axiosMock.history.post).toHaveLength(1);
8584
});
86-
const request = axiosMock.history.post.find(req => req.url.match(/\/api\/libraries\/.*\/containers/));
87-
expect(request.url).toMatch(/\/api\/libraries\/.*\/containers/);
88-
expect(JSON.parse(request.data)).toEqual({
85+
expect(axiosMock.history.post[0].url).toMatch(/\/api\/libraries\/.*\/containers/);
86+
expect(JSON.parse(axiosMock.history.post[0].data)).toEqual({
8987
can_stand_alone: true,
9088
container_type: 'section',
9189
display_name: 'Test Section',
@@ -116,11 +114,9 @@ describe('CreateContainerModal container linking', () => {
116114
const createButton = await screen.findByRole('button', { name: /create/i });
117115
await user.click(createButton);
118116
await waitFor(() => {
119-
const request = axiosMock.history.post.find(req => req.url.match(/\/api\/libraries\/.*\/containers/));
120-
expect(request).toBeDefined();
117+
expect(axiosMock.history.post[0].url).toMatch(/\/api\/libraries\/.*\/containers/);
121118
});
122-
const request = axiosMock.history.post.find(req => req.url.match(/\/api\/libraries\/.*\/containers/));
123-
expect(JSON.parse(request.data)).toEqual({
119+
expect(JSON.parse(axiosMock.history.post[0].data)).toEqual({
124120
can_stand_alone: false,
125121
container_type: 'subsection',
126122
display_name: 'Test Subsection',
@@ -148,4 +144,4 @@ describe('CreateContainerModal container linking', () => {
148144
expect(mockShowToast).toHaveBeenCalledWith(expect.stringMatching(/error/i));
149145
});
150146
});
151-
});
147+
});

src/library-authoring/library-info/LibraryInfo.test.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {
88
waitFor,
99
initializeMocks,
1010
} from '@src/testUtils';
11+
import { validateUserPermissions } from '@src/authz/data/api';
12+
import { CONTENT_LIBRARY_PERMISSIONS } from '@src/authz/constants';
1113
import { mockContentLibrary } from '../data/api.mocks';
1214
import { getCommitLibraryChangesUrl } from '../data/api';
1315
import { LibraryProvider } from '../common/context/LibraryContext';
@@ -33,6 +35,7 @@ const render = (libraryId: string = mockLibraryId) => baseRender(<LibraryInfo />
3335

3436
let axiosMock: MockAdapter;
3537
let mockShowToast: (message: string) => void;
38+
let validateUserPermissionsMock: jest.SpiedFunction<typeof validateUserPermissions>;
3639

3740
mockContentLibrary.applyMock();
3841

@@ -41,6 +44,14 @@ describe('<LibraryInfo />', () => {
4144
const mocks = initializeMocks();
4245
axiosMock = mocks.axiosMock;
4346
mockShowToast = mocks.mockShowToast;
47+
validateUserPermissionsMock = mocks.validateUserPermissionsMock;
48+
49+
validateUserPermissionsMock.mockResolvedValue([
50+
{
51+
action: CONTENT_LIBRARY_PERMISSIONS.PUBLISH_LIBRARY_CONTENT,
52+
allowed: true,
53+
},
54+
]);
4455
});
4556

4657
afterEach(() => {

src/testUtils.tsx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ import * as authzApi from '@src/authz/data/api';
2727
import { ToastContext, type ToastContextData } from './generic/toast-context';
2828
import initializeReduxStore, { type DeprecatedReduxState } from './store';
2929
import { getApiWaffleFlagsUrl } from './data/api';
30-
import { CONTENT_LIBRARY_PERMISSIONS } from './authz/constants';
3130

3231
/** @deprecated Use React Query and/or regular React Context instead of redux */
3332
let reduxStore: Store;
3433
let queryClient: QueryClient;
3534
let axiosMock: MockAdapter;
35+
let validateUserPermissionsMock: jest.SpiedFunction<typeof authzApi.validateUserPermissions>;
3636

3737
/** To use this: `const { mockShowToast } = initializeMocks()` and `expect(mockShowToast).toHaveBeenCalled()` */
3838
let mockToastContext: ToastContextData = {
@@ -195,19 +195,16 @@ export function initializeMocks({ user = defaultUser, initialState = undefined }
195195
jest.clearAllMocks();
196196

197197
// Mock user permissions to avoid breaking tests that monitor axios calls
198-
jest.spyOn(authzApi, 'validateUserPermissions').mockResolvedValue([
199-
{
200-
action: CONTENT_LIBRARY_PERMISSIONS.PUBLISH_LIBRARY_CONTENT,
201-
allowed: true,
202-
},
203-
]);
198+
// If needed, override the mockResolvedValue in your test
199+
validateUserPermissionsMock = jest.spyOn(authzApi, 'validateUserPermissions').mockResolvedValue([]);
204200

205201
return {
206202
reduxStore,
207203
axiosMock,
208204
mockShowToast: mockToastContext.showToast,
209205
mockToastAction: mockToastContext.toastAction,
210206
queryClient,
207+
validateUserPermissionsMock,
211208
};
212209
}
213210

0 commit comments

Comments
 (0)