Skip to content

Commit f36766a

Browse files
committed
fix: solve coverage issues
1 parent b25e60a commit f36766a

3 files changed

Lines changed: 113 additions & 0 deletions

File tree

src/authz/hooks.test.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { renderHook } from '@testing-library/react';
2+
import { useUserPermissions } from '@src/authz/data/apiHooks';
3+
import { mockWaffleFlags } from '@src/data/apiHooks.mock';
4+
import { useUserPermissionsWithAuthzCourse } from './hooks';
5+
import { COURSE_PERMISSIONS } from './constants';
6+
7+
jest.mock('@src/authz/data/apiHooks', () => ({
8+
useUserPermissions: jest.fn(),
9+
}));
10+
11+
const courseId = 'course-v1:org+course+run';
12+
const permissions = {
13+
canView: { action: COURSE_PERMISSIONS.VIEW_GRADING_SETTINGS, scope: courseId },
14+
canEdit: { action: COURSE_PERMISSIONS.EDIT_GRADING_SETTINGS, scope: courseId },
15+
};
16+
17+
describe('useUserPermissionsWithAuthzCourse', () => {
18+
beforeEach(() => {
19+
jest.clearAllMocks();
20+
jest.mocked(useUserPermissions).mockReturnValue({
21+
isLoading: false,
22+
data: undefined,
23+
} as unknown as ReturnType<typeof useUserPermissions>);
24+
});
25+
26+
it('defaults all permissions to true when authz is disabled', () => {
27+
mockWaffleFlags({ enableAuthzCourseAuthoring: false });
28+
29+
const { result } = renderHook(() => useUserPermissionsWithAuthzCourse(courseId, permissions));
30+
31+
expect(result.current.isLoading).toBe(false);
32+
expect(result.current.isAuthzEnabled).toBe(false);
33+
expect(result.current.permissions.canView).toBe(true);
34+
expect(result.current.permissions.canEdit).toBe(true);
35+
});
36+
37+
it('returns actual permission values when authz is enabled and permissions are loaded', () => {
38+
mockWaffleFlags({ enableAuthzCourseAuthoring: true });
39+
jest.mocked(useUserPermissions).mockReturnValue({
40+
isLoading: false,
41+
data: { canView: true, canEdit: false },
42+
} as unknown as ReturnType<typeof useUserPermissions>);
43+
44+
const { result } = renderHook(() => useUserPermissionsWithAuthzCourse(courseId, permissions));
45+
46+
expect(result.current.isLoading).toBe(false);
47+
expect(result.current.isAuthzEnabled).toBe(true);
48+
expect(result.current.permissions.canView).toBe(true);
49+
expect(result.current.permissions.canEdit).toBe(false);
50+
});
51+
52+
it('returns isLoading=true and empty permissions while authz permissions are loading', () => {
53+
mockWaffleFlags({ enableAuthzCourseAuthoring: true });
54+
jest.mocked(useUserPermissions).mockReturnValue({
55+
isLoading: true,
56+
data: undefined,
57+
} as unknown as ReturnType<typeof useUserPermissions>);
58+
59+
const { result } = renderHook(() => useUserPermissionsWithAuthzCourse(courseId, permissions));
60+
61+
expect(result.current.isLoading).toBe(true);
62+
expect(result.current.isAuthzEnabled).toBe(true);
63+
expect(result.current.permissions).toEqual({});
64+
});
65+
66+
it('falls back to false for permissions absent from server response when authz is enabled', () => {
67+
mockWaffleFlags({ enableAuthzCourseAuthoring: true });
68+
jest.mocked(useUserPermissions).mockReturnValue({
69+
isLoading: false,
70+
data: {},
71+
} as unknown as ReturnType<typeof useUserPermissions>);
72+
73+
const { result } = renderHook(() => useUserPermissionsWithAuthzCourse(courseId, permissions));
74+
75+
expect(result.current.permissions.canView).toBe(false);
76+
expect(result.current.permissions.canEdit).toBe(false);
77+
});
78+
});

src/grading-settings/assignment-section/AssignmentSection.test.jsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ describe('<AssignmentSection />', () => {
108108
expect(getByText(messages.totalNumberErrorMessage.defaultMessage)).toBeInTheDocument();
109109
});
110110
});
111+
it('should disable all inputs and delete button when isEditable is false', async () => {
112+
const { getAllByRole, getByText } = render(<RootWrapper isEditable={false} />);
113+
await waitFor(() => {
114+
const inputs = getAllByRole('textbox').concat(getAllByRole('spinbutton'));
115+
inputs.forEach((input) => expect(input).toBeDisabled());
116+
const deleteBtn = getByText(messages.assignmentDeleteButton.defaultMessage).closest('button');
117+
expect(deleteBtn).toBeDisabled();
118+
});
119+
});
120+
111121
it('checking correct error msg if total weight have negative number', async () => {
112122
const { getByText, getByTestId } = render(<RootWrapper />);
113123
await waitFor(() => {

src/grading-settings/grading-scale/GradingScale.test.jsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,31 @@ describe('<GradingScale />', () => {
122122
});
123123
});
124124

125+
it('should disable inputs and buttons when isEditable is false', async () => {
126+
const { getAllByTestId, queryAllByTestId } = render(
127+
<IntlProvider locale="en" messages={{}}>
128+
<GradingScale
129+
gradeCutoffs={gradeCutoffs}
130+
gradeLetters={gradeLetters}
131+
sortedGrades={sortedGrades}
132+
resetDataRef={{ current: false }}
133+
showSavePrompt={jest.fn()}
134+
setShowSuccessAlert={jest.fn()}
135+
setGradingData={jest.fn()}
136+
setOverrideInternetConnectionAlert={jest.fn()}
137+
setEligibleGrade={jest.fn()}
138+
isEditable={false}
139+
/>
140+
</IntlProvider>,
141+
);
142+
await waitFor(() => {
143+
const segmentInputs = getAllByTestId('grading-scale-segment-input');
144+
segmentInputs.forEach((input) => expect(input).toBeDisabled());
145+
const removeButtons = queryAllByTestId('grading-scale-btn-remove');
146+
removeButtons.forEach((btn) => expect(btn).toBeDisabled());
147+
});
148+
});
149+
125150
it('should render GradingScale component with more than 5 grades', async () => {
126151
const { getAllByTestId } = render(
127152
<IntlProvider locale="en" messages={{}}>

0 commit comments

Comments
 (0)