Skip to content

Commit 3ac8b9b

Browse files
Merge pull request #103 from iblai/fix/autoplay-icon-based-on-autoplay-settings
Fix/autoplay icon based on autoplay settings
2 parents a5537bf + 07772de commit 3ac8b9b

2 files changed

Lines changed: 93 additions & 6 deletions

File tree

app/course-content/[course_id]/__tests__/layout.test.tsx

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,16 @@ vi.mock('@iblai/iblai-js/data-layer', () => ({
175175
}));
176176

177177
// Mock web-utils — layout dispatches setAdvancedDisplayMonetizationCheckoutModal
178+
// and reads the tenant `enable_course_voice_autoplay` flag via useTenantMetadata.
179+
const mockTenantMetadata = vi.hoisted(() => ({
180+
current: { enable_course_voice_autoplay: true } as Record<string, unknown>,
181+
}));
178182
vi.mock('@iblai/iblai-js/web-utils', () => ({
179183
setAdvancedDisplayMonetizationCheckoutModal: (payload: unknown) => ({
180184
type: 'setAdvancedDisplayMonetizationCheckoutModal',
181185
payload,
182186
}),
187+
useTenantMetadata: vi.fn(() => ({ metadata: mockTenantMetadata.current })),
183188
}));
184189

185190
// Mock react-redux — layout calls useDispatch + useSelector(selectMentorSpinnerHidden)
@@ -226,6 +231,7 @@ describe('CourseContentLayout', () => {
226231

227232
beforeEach(() => {
228233
vi.clearAllMocks();
234+
mockTenantMetadata.current = { enable_course_voice_autoplay: true };
229235
vi.mocked(useCourseDetail).mockReturnValue({
230236
handleFetchCourseInfo: mockHandleFetchCourseInfo,
231237
handleFetchCourseSyllabus: mockHandleFetchCourseSyllabus,
@@ -1145,10 +1151,85 @@ describe('CourseContentLayout', () => {
11451151
});
11461152

11471153
describe('agent autoplay toggle', () => {
1148-
// NOTE: layout.tsx temporarily hard-codes `autoplayToggleVisible = true`, so
1149-
// the toggle currently renders regardless of `course.agent_autoplay`. Once
1150-
// the proper gate is restored, hide-when-false / hide-when-null specs can
1151-
// be added back here.
1154+
const renderWithCourse = (course: any) => {
1155+
vi.mocked(useCourseDetail).mockReturnValue({
1156+
handleFetchCourseInfo: mockHandleFetchCourseInfo,
1157+
handleFetchCourseSyllabus: mockHandleFetchCourseSyllabus,
1158+
handleOpenLesson: mockHandleOpenLesson,
1159+
handleFetchCourseProgress: mockHandleFetchCourseProgress,
1160+
handleFetchCourseCompletion: mockHandleFetchCourseCompletion,
1161+
handleCheckCourseMonetizationAccess: mockHandleCheckCourseMonetizationAccess,
1162+
course,
1163+
courseInfoLoadingState: 'successful',
1164+
courseOutline: null,
1165+
courseOutlineLoading: false,
1166+
courseCompletion: null,
1167+
courseGradingPolicyActive: false,
1168+
} as any);
1169+
1170+
return render(
1171+
<CourseContentLayout params={defaultParams}>
1172+
<div>children</div>
1173+
</CourseContentLayout>,
1174+
);
1175+
};
1176+
1177+
it('hides the autoplay toggle when course.agent_autoplay is false', () => {
1178+
renderWithCourse({ agent_autoplay: false });
1179+
expect(screen.queryByTestId('agent-autoplay-toggle')).not.toBeInTheDocument();
1180+
expect(screen.queryByTestId('agent-autoplay-popover-switch')).not.toBeInTheDocument();
1181+
});
1182+
1183+
it('hides the autoplay toggle when course.agent_autoplay is null', () => {
1184+
renderWithCourse({ agent_autoplay: null });
1185+
expect(screen.queryByTestId('agent-autoplay-toggle')).not.toBeInTheDocument();
1186+
expect(screen.queryByTestId('agent-autoplay-popover-switch')).not.toBeInTheDocument();
1187+
});
1188+
1189+
it('hides the autoplay toggle when course.agent_autoplay is missing', () => {
1190+
renderWithCourse({});
1191+
expect(screen.queryByTestId('agent-autoplay-toggle')).not.toBeInTheDocument();
1192+
expect(screen.queryByTestId('agent-autoplay-popover-switch')).not.toBeInTheDocument();
1193+
});
1194+
1195+
it('hides the autoplay toggle when course is null (still loading)', () => {
1196+
renderWithCourse(null);
1197+
expect(screen.queryByTestId('agent-autoplay-toggle')).not.toBeInTheDocument();
1198+
expect(screen.queryByTestId('agent-autoplay-popover-switch')).not.toBeInTheDocument();
1199+
});
1200+
1201+
it('hides the autoplay toggle for truthy-but-not-true values (e.g. 1)', () => {
1202+
renderWithCourse({ agent_autoplay: 1 });
1203+
expect(screen.queryByTestId('agent-autoplay-toggle')).not.toBeInTheDocument();
1204+
expect(screen.queryByTestId('agent-autoplay-popover-switch')).not.toBeInTheDocument();
1205+
});
1206+
1207+
it('hides the autoplay toggle when tenant enable_course_voice_autoplay is false (course flag on)', () => {
1208+
mockTenantMetadata.current = { enable_course_voice_autoplay: false };
1209+
renderWithCourse({ agent_autoplay: true });
1210+
expect(screen.queryByTestId('agent-autoplay-toggle')).not.toBeInTheDocument();
1211+
expect(screen.queryByTestId('agent-autoplay-popover-switch')).not.toBeInTheDocument();
1212+
});
1213+
1214+
it('hides the autoplay toggle when tenant enable_course_voice_autoplay is missing', () => {
1215+
mockTenantMetadata.current = {};
1216+
renderWithCourse({ agent_autoplay: true });
1217+
expect(screen.queryByTestId('agent-autoplay-toggle')).not.toBeInTheDocument();
1218+
expect(screen.queryByTestId('agent-autoplay-popover-switch')).not.toBeInTheDocument();
1219+
});
1220+
1221+
it('hides the autoplay toggle for tenant truthy-but-not-true values (e.g. "true")', () => {
1222+
mockTenantMetadata.current = { enable_course_voice_autoplay: 'true' };
1223+
renderWithCourse({ agent_autoplay: true });
1224+
expect(screen.queryByTestId('agent-autoplay-toggle')).not.toBeInTheDocument();
1225+
expect(screen.queryByTestId('agent-autoplay-popover-switch')).not.toBeInTheDocument();
1226+
});
1227+
1228+
it('shows the autoplay toggle only when BOTH course.agent_autoplay AND tenant flag are true', () => {
1229+
mockTenantMetadata.current = { enable_course_voice_autoplay: true };
1230+
renderWithCourse({ agent_autoplay: true });
1231+
expect(screen.getByTestId('agent-autoplay-toggle')).toBeInTheDocument();
1232+
});
11521233

11531234
it('shows the autoplay toggle (defaults to off, play icon visible)', () => {
11541235
vi.mocked(useCourseDetail).mockReturnValue({

app/course-content/[course_id]/layout.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ import { useGetDepartmentMemberCheckQuery } from '@/services/core';
2424
import { useGetCourseBlockDetailsQuery } from '@/services/course-metadata';
2525
import { Switch } from '@/components/ui/switch';
2626
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';
27-
import { setAdvancedDisplayMonetizationCheckoutModal } from '@iblai/iblai-js/web-utils';
27+
import {
28+
setAdvancedDisplayMonetizationCheckoutModal,
29+
useTenantMetadata,
30+
} from '@iblai/iblai-js/web-utils';
2831
import { useDispatch, useSelector } from 'react-redux';
2932
import { MONETIZATION_CLOSE_PAYLOAD } from '@/constants/global';
3033
import { config } from '@/lib/config';
@@ -48,6 +51,7 @@ export default function CourseContentLayout({
4851
const dispatch = useDispatch();
4952
const mentorSpinnerHidden = useSelector(selectMentorSpinnerHidden);
5053
const { setCourseMentor } = useChatState();
54+
const { metadata } = useTenantMetadata({ org: getTenant() });
5155
const {
5256
handleFetchCourseInfo,
5357
handleFetchCourseSyllabus,
@@ -121,7 +125,9 @@ export default function CourseContentLayout({
121125
window.dispatchEvent(new CustomEvent('mentor:autoplay-changed', { detail: { enabled } }));
122126
};
123127

124-
const autoplayToggleVisible = true; // course?.agent_autoplay === true;
128+
// enable_course_voice_autoplay tenant metadata flag and agent_autoplay flag from the course settings on studio to allow this feature
129+
const autoplayToggleVisible =
130+
course?.agent_autoplay === true && metadata?.enable_course_voice_autoplay === true;
125131

126132
const { data: courseBlockDetails } = useGetCourseBlockDetailsQuery(
127133
{ blockId: currentUnitID || '', username: getUserName() },

0 commit comments

Comments
 (0)