forked from openedx/frontend-app-authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhooks.test.ts
More file actions
79 lines (64 loc) · 2.99 KB
/
hooks.test.ts
File metadata and controls
79 lines (64 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { renderHook } from '@testing-library/react';
import { useUserPermissions } from '@src/authz/data/apiHooks';
import { mockWaffleFlags } from '@src/data/apiHooks.mock';
import { useCourseUserPermissions } from './hooks';
import { COURSE_PERMISSIONS } from './constants';
jest.mock('@src/authz/data/apiHooks', () => ({
useUserPermissions: jest.fn(),
}));
const courseId = 'course-v1:org+course+run';
const permissions = {
canView: { action: COURSE_PERMISSIONS.VIEW_GRADING_SETTINGS, scope: courseId },
canEdit: { action: COURSE_PERMISSIONS.EDIT_GRADING_SETTINGS, scope: courseId },
};
describe('useCourseUserPermissions', () => {
beforeEach(() => {
jest.clearAllMocks();
jest.mocked(useUserPermissions).mockReturnValue({
isLoading: false,
data: undefined,
} as unknown as ReturnType<typeof useUserPermissions>);
});
it('defaults all permissions to true when authz is disabled', () => {
mockWaffleFlags({ enableAuthzCourseAuthoring: false });
const { result } = renderHook(() => useCourseUserPermissions(courseId, permissions));
expect(result.current.isLoading).toBe(false);
expect(result.current.isAuthzEnabled).toBe(false);
expect(result.current.canView).toBe(true);
expect(result.current.canEdit).toBe(true);
});
it('returns actual permission values when authz is enabled and permissions are loaded', () => {
mockWaffleFlags({ enableAuthzCourseAuthoring: true });
jest.mocked(useUserPermissions).mockReturnValue({
isLoading: false,
data: { canView: true, canEdit: false },
} as unknown as ReturnType<typeof useUserPermissions>);
const { result } = renderHook(() => useCourseUserPermissions(courseId, permissions));
expect(result.current.isLoading).toBe(false);
expect(result.current.isAuthzEnabled).toBe(true);
expect(result.current.canView).toBe(true);
expect(result.current.canEdit).toBe(false);
});
it('returns isLoading=true and no permission keys while authz permissions are loading', () => {
mockWaffleFlags({ enableAuthzCourseAuthoring: true });
jest.mocked(useUserPermissions).mockReturnValue({
isLoading: true,
data: undefined,
} as unknown as ReturnType<typeof useUserPermissions>);
const { result } = renderHook(() => useCourseUserPermissions(courseId, permissions));
expect(result.current.isLoading).toBe(true);
expect(result.current.isAuthzEnabled).toBe(true);
expect(result.current.canView).toBeUndefined();
expect(result.current.canEdit).toBeUndefined();
});
it('falls back to false for permissions absent from server response when authz is enabled', () => {
mockWaffleFlags({ enableAuthzCourseAuthoring: true });
jest.mocked(useUserPermissions).mockReturnValue({
isLoading: false,
data: {},
} as unknown as ReturnType<typeof useUserPermissions>);
const { result } = renderHook(() => useCourseUserPermissions(courseId, permissions));
expect(result.current.canView).toBe(false);
expect(result.current.canEdit).toBe(false);
});
});