From 23e522e8936897321eb45d9044ff486da3e510e7 Mon Sep 17 00:00:00 2001 From: Rodrigo Martin Date: Mon, 8 Apr 2024 11:16:08 -0300 Subject: [PATCH] feat(AU-1949): Restrict WCT to verified track learners only (#1345) * feat(AU-1949): Restrict WCT to verified track learners only * fix: use jest-when * fix: clean tests --- plugins/UnitTranslationPlugin/index.jsx | 3 +- plugins/UnitTranslationPlugin/index.test.jsx | 39 ++++++++++++++++++-- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/plugins/UnitTranslationPlugin/index.jsx b/plugins/UnitTranslationPlugin/index.jsx index 03bf9bff3c..5085eabd94 100644 --- a/plugins/UnitTranslationPlugin/index.jsx +++ b/plugins/UnitTranslationPlugin/index.jsx @@ -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: [], @@ -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; } diff --git a/plugins/UnitTranslationPlugin/index.test.jsx b/plugins/UnitTranslationPlugin/index.test.jsx index 2bd90bbec1..9eaf5a4bcd 100644 --- a/plugins/UnitTranslationPlugin/index.test.jsx +++ b/plugins/UnitTranslationPlugin/index.test.jsx @@ -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'; @@ -24,7 +26,11 @@ describe('', () => { 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(); @@ -32,7 +38,11 @@ describe('', () => { 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: [], }); @@ -43,7 +53,24 @@ describe('', () => { }); 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(); + + 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(); @@ -52,7 +79,11 @@ describe('', () => { }); 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();