Skip to content

Commit 7652fa4

Browse files
authored
Lk/translation only for verified (openedx#1355)
* chore: update verified mode logic * chore: add is staff logic * chore: add test
1 parent 2347ce8 commit 7652fa4

File tree

5 files changed

+70
-32
lines changed

5 files changed

+70
-32
lines changed

plugins/UnitTranslationPlugin/__snapshots__/index.test.jsx.snap

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,17 @@ exports[`<UnitTranslationPlugin /> render TranslationSelection when translation
1313
unitId="unitId"
1414
/>
1515
`;
16+
17+
exports[`<UnitTranslationPlugin /> render translation when the user is staff 1`] = `
18+
<TranslationSelection
19+
availableLanguages={
20+
Array [
21+
"en",
22+
]
23+
}
24+
courseId="courseId"
25+
id="id"
26+
language="en"
27+
unitId="unitId"
28+
/>
29+
`;

plugins/UnitTranslationPlugin/index.jsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,34 @@ import React, { useEffect, useState } from 'react';
22
import PropTypes from 'prop-types';
33

44
import { useModel } from '@src/generic/model-store';
5+
import { VERIFIED_MODES } from '@src/constants';
56

67
import TranslationSelection from './translation-selection';
78
import { fetchTranslationConfig } from './data/api';
89

910
const UnitTranslationPlugin = ({ id, courseId, unitId }) => {
10-
const { language } = useModel('coursewareMeta', courseId);
11-
const { enrollmentMode } = useModel('courseHomeMeta', courseId);
11+
const { language, enrollmentMode } = useModel('coursewareMeta', courseId);
12+
const { isStaff } = useModel('courseHomeMeta', courseId);
1213
const [translationConfig, setTranslationConfig] = useState({
1314
enabled: false,
1415
availableLanguages: [],
1516
});
1617

17-
const verifiedMode = enrollmentMode === 'verified';
18+
const hasVerifiedEnrollment = isStaff || (
19+
enrollmentMode !== null
20+
&& enrollmentMode !== undefined
21+
&& VERIFIED_MODES.includes(enrollmentMode)
22+
);
1823

1924
useEffect(() => {
20-
if (verifiedMode) {
25+
if (hasVerifiedEnrollment) {
2126
fetchTranslationConfig(courseId).then(setTranslationConfig);
2227
}
2328
}, []);
2429

2530
const { enabled, availableLanguages } = translationConfig;
2631

27-
if (!verifiedMode || !enabled || !language || !availableLanguages.length) {
32+
if (!hasVerifiedEnrollment || !enabled || !language || !availableLanguages.length) {
2833
return null;
2934
}
3035

plugins/UnitTranslationPlugin/index.test.jsx

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,23 @@ describe('<UnitTranslationPlugin />', () => {
2222
courseId: 'courseId',
2323
unitId: 'unitId',
2424
};
25-
const mockInitialState = ({ enabled = true, availableLanguages = ['en'] }) => {
26-
useState.mockReturnValue([{ enabled, availableLanguages }, jest.fn()]);
25+
const mockInitialState = ({
26+
enabled = true,
27+
availableLanguages = ['en'],
28+
language = 'en',
29+
enrollmentMode = 'verified',
30+
isStaff = false,
31+
}) => {
32+
useState.mockReturnValueOnce([{ enabled, availableLanguages }, jest.fn()]);
33+
34+
when(useModel)
35+
.calledWith('coursewareMeta', props.courseId)
36+
.mockReturnValueOnce({ language, enrollmentMode });
37+
38+
when(useModel)
39+
.calledWith('courseHomeMeta', props.courseId)
40+
.mockReturnValueOnce({ isStaff });
2741
};
28-
when(useModel)
29-
.calledWith('coursewareMeta', props.courseId)
30-
.mockReturnValue({ language: 'en' })
31-
.calledWith('courseHomeMeta', props.courseId)
32-
.mockReturnValue({ enrollmentMode: 'verified' });
3342

3443
beforeEach(() => {
3544
jest.clearAllMocks();
@@ -53,27 +62,36 @@ describe('<UnitTranslationPlugin />', () => {
5362
});
5463

5564
it('render empty when course language has not been set', () => {
56-
when(useModel)
57-
.calledWith('coursewareMeta', props.courseId)
58-
.mockReturnValueOnce({ language: null });
59-
mockInitialState({});
65+
mockInitialState({
66+
language: null,
67+
});
6068

6169
const wrapper = shallow(<UnitTranslationPlugin {...props} />);
6270

6371
expect(wrapper.isEmptyRender()).toBe(true);
6472
});
6573

6674
it('render empty when student is enroll as verified', () => {
67-
when(useModel)
68-
.calledWith('courseHomeMeta', props.courseId)
69-
.mockReturnValueOnce({ enrollmentMode: 'audit' });
70-
mockInitialState({});
75+
mockInitialState({
76+
enrollmentMode: 'audit',
77+
});
7178

7279
const wrapper = shallow(<UnitTranslationPlugin {...props} />);
7380

7481
expect(wrapper.isEmptyRender()).toBe(true);
7582
});
7683

84+
it('render translation when the user is staff', () => {
85+
mockInitialState({
86+
enrollmentMode: 'audit',
87+
isStaff: true,
88+
});
89+
90+
const wrapper = shallow(<UnitTranslationPlugin {...props} />);
91+
92+
expect(wrapper.snapshot).toMatchSnapshot();
93+
});
94+
7795
it('render TranslationSelection when translation is enabled and language is available', () => {
7896
mockInitialState({});
7997

src/constants.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,14 @@ export const REDIRECT_MODES = {
3333
HOME_REDIRECT: 'home-redirect',
3434
SURVEY_REDIRECT: 'survey-redirect',
3535
};
36+
37+
export const VERIFIED_MODES = [
38+
'professional',
39+
'verified',
40+
'no-id-professional',
41+
'credit',
42+
'masters',
43+
'executive-education',
44+
'paid-executive-education',
45+
'paid-bootcamp',
46+
];

src/courseware/course/chat/Chat.jsx

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import PropTypes from 'prop-types';
55
import { Xpert } from '@edx/frontend-lib-learning-assistant';
66
import { injectIntl } from '@edx/frontend-platform/i18n';
77

8+
import { VERIFIED_MODES } from '@src/constants';
89
import { useModel } from '../../../generic/model-store';
910

1011
const Chat = ({
@@ -20,21 +21,10 @@ const Chat = ({
2021
} = useSelector(state => state.specialExams);
2122
const course = useModel('coursewareMeta', courseId);
2223

23-
const VERIFIED_MODES = [
24-
'professional',
25-
'verified',
26-
'no-id-professional',
27-
'credit',
28-
'masters',
29-
'executive-education',
30-
'paid-executive-education',
31-
'paid-bootcamp',
32-
];
33-
3424
const hasVerifiedEnrollment = (
3525
enrollmentMode !== null
3626
&& enrollmentMode !== undefined
37-
&& [...VERIFIED_MODES].some(mode => mode === enrollmentMode)
27+
&& VERIFIED_MODES.includes(enrollmentMode)
3828
);
3929

4030
const validDates = () => {

0 commit comments

Comments
 (0)