Skip to content

Commit

Permalink
feat(AU-1949): Restrict WCT to verified track learners only (openedx#…
Browse files Browse the repository at this point in the history
…1345)

* feat(AU-1949): Restrict WCT to verified track learners only

* fix: use jest-when

* fix: clean tests
  • Loading branch information
Rodra committed Apr 8, 2024
1 parent 9423a88 commit 23e522e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
3 changes: 2 additions & 1 deletion plugins/UnitTranslationPlugin/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { fetchTranslationConfig } from './data/api';

const UnitTranslationPlugin = ({ id, courseId, unitId }) => {
const { language } = useModel('coursewareMeta', courseId);
const { verifiedMode } = useModel('courseHomeMeta', courseId);
const [translationConfig, setTranslationConfig] = useState({
enabled: false,
availableLanguages: [],
Expand All @@ -19,7 +20,7 @@ const UnitTranslationPlugin = ({ id, courseId, unitId }) => {

const { enabled, availableLanguages } = translationConfig;

if (!enabled || !language || !availableLanguages.length) {
if (!enabled || !language || !availableLanguages.length || !verifiedMode) {
return null;
}

Expand Down
39 changes: 35 additions & 4 deletions plugins/UnitTranslationPlugin/index.test.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { when } from 'jest-when';

import { shallow } from '@edx/react-unit-test-utils';
import { useState } from 'react';
import { useModel } from '@src/generic/model-store';
Expand All @@ -24,15 +26,23 @@ describe('<UnitTranslationPlugin />', () => {
useState.mockReturnValue([{ enabled, availableLanguages }, jest.fn()]);
};
it('render empty when translation is not enabled', () => {
useModel.mockReturnValue({ language: 'en' });
when(useModel)
.calledWith('coursewareMeta', props.courseId)
.mockReturnValueOnce({ language: 'en' })
.calledWith('courseHomeMeta', props.courseId)
.mockReturnValueOnce({ verifiedMode: { accessExpirationDate: null } });
mockInitialState({ enabled: false });

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

expect(wrapper.isEmptyRender()).toBe(true);
});
it('render empty when available languages is empty', () => {
useModel.mockReturnValue({ language: 'fr' });
when(useModel)
.calledWith('coursewareMeta', props.courseId)
.mockReturnValueOnce({ language: 'fr' })
.calledWith('courseHomeMeta', props.courseId)
.mockReturnValueOnce({ verifiedMode: { accessExpirationDate: null } });
mockInitialState({
availableLanguages: [],
});
Expand All @@ -43,7 +53,24 @@ describe('<UnitTranslationPlugin />', () => {
});

it('render empty when course language has not been set', () => {
useModel.mockReturnValue({ language: undefined });
when(useModel)
.calledWith('coursewareMeta', props.courseId)
.mockReturnValueOnce({ language: undefined })
.calledWith('courseHomeMeta', props.courseId)
.mockReturnValueOnce({ verifiedMode: { accessExpirationDate: null } });
mockInitialState({});

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

expect(wrapper.isEmptyRender()).toBe(true);
});

it('render empty when verifiedMode has not been set', () => {
when(useModel)
.calledWith('coursewareMeta', props.courseId)
.mockReturnValueOnce({ language: 'en' })
.calledWith('courseHomeMeta', props.courseId)
.mockReturnValueOnce({ verifiedMode: null });
mockInitialState({});

const wrapper = shallow(<UnitTranslationPlugin {...props} />);
Expand All @@ -52,7 +79,11 @@ describe('<UnitTranslationPlugin />', () => {
});

it('render TranslationSelection when translation is enabled and language is available', () => {
useModel.mockReturnValue({ language: 'en' });
when(useModel)
.calledWith('coursewareMeta', props.courseId)
.mockReturnValueOnce({ language: 'en' })
.calledWith('courseHomeMeta', props.courseId)
.mockReturnValueOnce({ verifiedMode: { accessExpirationDate: null } });
mockInitialState({});

const wrapper = shallow(<UnitTranslationPlugin {...props} />);
Expand Down

0 comments on commit 23e522e

Please sign in to comment.