From 7652fa46d158d4936f86831770feb4b1ed5704e7 Mon Sep 17 00:00:00 2001
From: Leangseu Kim <83240113+leangseu-edx@users.noreply.github.com>
Date: Mon, 15 Apr 2024 10:57:20 -0400
Subject: [PATCH] Lk/translation only for verified (#1355)
* chore: update verified mode logic
* chore: add is staff logic
* chore: add test
---
.../__snapshots__/index.test.jsx.snap | 14 ++++++
plugins/UnitTranslationPlugin/index.jsx | 15 ++++--
plugins/UnitTranslationPlugin/index.test.jsx | 48 +++++++++++++------
src/constants.js | 11 +++++
src/courseware/course/chat/Chat.jsx | 14 +-----
5 files changed, 70 insertions(+), 32 deletions(-)
diff --git a/plugins/UnitTranslationPlugin/__snapshots__/index.test.jsx.snap b/plugins/UnitTranslationPlugin/__snapshots__/index.test.jsx.snap
index 65d2737937..de7768e3a9 100644
--- a/plugins/UnitTranslationPlugin/__snapshots__/index.test.jsx.snap
+++ b/plugins/UnitTranslationPlugin/__snapshots__/index.test.jsx.snap
@@ -13,3 +13,17 @@ exports[` render TranslationSelection when translation
unitId="unitId"
/>
`;
+
+exports[` render translation when the user is staff 1`] = `
+
+`;
diff --git a/plugins/UnitTranslationPlugin/index.jsx b/plugins/UnitTranslationPlugin/index.jsx
index 038865ebdf..54e3e1565c 100644
--- a/plugins/UnitTranslationPlugin/index.jsx
+++ b/plugins/UnitTranslationPlugin/index.jsx
@@ -2,29 +2,34 @@ import React, { useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import { useModel } from '@src/generic/model-store';
+import { VERIFIED_MODES } from '@src/constants';
import TranslationSelection from './translation-selection';
import { fetchTranslationConfig } from './data/api';
const UnitTranslationPlugin = ({ id, courseId, unitId }) => {
- const { language } = useModel('coursewareMeta', courseId);
- const { enrollmentMode } = useModel('courseHomeMeta', courseId);
+ const { language, enrollmentMode } = useModel('coursewareMeta', courseId);
+ const { isStaff } = useModel('courseHomeMeta', courseId);
const [translationConfig, setTranslationConfig] = useState({
enabled: false,
availableLanguages: [],
});
- const verifiedMode = enrollmentMode === 'verified';
+ const hasVerifiedEnrollment = isStaff || (
+ enrollmentMode !== null
+ && enrollmentMode !== undefined
+ && VERIFIED_MODES.includes(enrollmentMode)
+ );
useEffect(() => {
- if (verifiedMode) {
+ if (hasVerifiedEnrollment) {
fetchTranslationConfig(courseId).then(setTranslationConfig);
}
}, []);
const { enabled, availableLanguages } = translationConfig;
- if (!verifiedMode || !enabled || !language || !availableLanguages.length) {
+ if (!hasVerifiedEnrollment || !enabled || !language || !availableLanguages.length) {
return null;
}
diff --git a/plugins/UnitTranslationPlugin/index.test.jsx b/plugins/UnitTranslationPlugin/index.test.jsx
index 9a7c7b1aab..e606369b0f 100644
--- a/plugins/UnitTranslationPlugin/index.test.jsx
+++ b/plugins/UnitTranslationPlugin/index.test.jsx
@@ -22,14 +22,23 @@ describe('', () => {
courseId: 'courseId',
unitId: 'unitId',
};
- const mockInitialState = ({ enabled = true, availableLanguages = ['en'] }) => {
- useState.mockReturnValue([{ enabled, availableLanguages }, jest.fn()]);
+ const mockInitialState = ({
+ enabled = true,
+ availableLanguages = ['en'],
+ language = 'en',
+ enrollmentMode = 'verified',
+ isStaff = false,
+ }) => {
+ useState.mockReturnValueOnce([{ enabled, availableLanguages }, jest.fn()]);
+
+ when(useModel)
+ .calledWith('coursewareMeta', props.courseId)
+ .mockReturnValueOnce({ language, enrollmentMode });
+
+ when(useModel)
+ .calledWith('courseHomeMeta', props.courseId)
+ .mockReturnValueOnce({ isStaff });
};
- when(useModel)
- .calledWith('coursewareMeta', props.courseId)
- .mockReturnValue({ language: 'en' })
- .calledWith('courseHomeMeta', props.courseId)
- .mockReturnValue({ enrollmentMode: 'verified' });
beforeEach(() => {
jest.clearAllMocks();
@@ -53,10 +62,9 @@ describe('', () => {
});
it('render empty when course language has not been set', () => {
- when(useModel)
- .calledWith('coursewareMeta', props.courseId)
- .mockReturnValueOnce({ language: null });
- mockInitialState({});
+ mockInitialState({
+ language: null,
+ });
const wrapper = shallow();
@@ -64,16 +72,26 @@ describe('', () => {
});
it('render empty when student is enroll as verified', () => {
- when(useModel)
- .calledWith('courseHomeMeta', props.courseId)
- .mockReturnValueOnce({ enrollmentMode: 'audit' });
- mockInitialState({});
+ mockInitialState({
+ enrollmentMode: 'audit',
+ });
const wrapper = shallow();
expect(wrapper.isEmptyRender()).toBe(true);
});
+ it('render translation when the user is staff', () => {
+ mockInitialState({
+ enrollmentMode: 'audit',
+ isStaff: true,
+ });
+
+ const wrapper = shallow();
+
+ expect(wrapper.snapshot).toMatchSnapshot();
+ });
+
it('render TranslationSelection when translation is enabled and language is available', () => {
mockInitialState({});
diff --git a/src/constants.js b/src/constants.js
index caac22bced..4829b60779 100644
--- a/src/constants.js
+++ b/src/constants.js
@@ -33,3 +33,14 @@ export const REDIRECT_MODES = {
HOME_REDIRECT: 'home-redirect',
SURVEY_REDIRECT: 'survey-redirect',
};
+
+export const VERIFIED_MODES = [
+ 'professional',
+ 'verified',
+ 'no-id-professional',
+ 'credit',
+ 'masters',
+ 'executive-education',
+ 'paid-executive-education',
+ 'paid-bootcamp',
+];
diff --git a/src/courseware/course/chat/Chat.jsx b/src/courseware/course/chat/Chat.jsx
index 6f56406531..4dbe81398c 100644
--- a/src/courseware/course/chat/Chat.jsx
+++ b/src/courseware/course/chat/Chat.jsx
@@ -5,6 +5,7 @@ import PropTypes from 'prop-types';
import { Xpert } from '@edx/frontend-lib-learning-assistant';
import { injectIntl } from '@edx/frontend-platform/i18n';
+import { VERIFIED_MODES } from '@src/constants';
import { useModel } from '../../../generic/model-store';
const Chat = ({
@@ -20,21 +21,10 @@ const Chat = ({
} = useSelector(state => state.specialExams);
const course = useModel('coursewareMeta', courseId);
- const VERIFIED_MODES = [
- 'professional',
- 'verified',
- 'no-id-professional',
- 'credit',
- 'masters',
- 'executive-education',
- 'paid-executive-education',
- 'paid-bootcamp',
- ];
-
const hasVerifiedEnrollment = (
enrollmentMode !== null
&& enrollmentMode !== undefined
- && [...VERIFIED_MODES].some(mode => mode === enrollmentMode)
+ && VERIFIED_MODES.includes(enrollmentMode)
);
const validDates = () => {