Files
frontend-app-learning/plugins/UnitTranslationPlugin/index.test.jsx
Leangseu Kim 7652fa46d1 Lk/translation only for verified (#1355)
* chore: update verified mode logic

* chore: add is staff logic

* chore: add test
2024-04-15 11:57:20 -03:00

103 lines
2.6 KiB
JavaScript

import { when } from 'jest-when';
import { shallow } from '@edx/react-unit-test-utils';
import { useState } from 'react';
import { useModel } from '@src/generic/model-store';
import UnitTranslationPlugin from './index';
jest.mock('@src/generic/model-store');
jest.mock('./data/api', () => ({
fetchTranslationConfig: jest.fn(),
}));
jest.mock('./translation-selection', () => 'TranslationSelection');
jest.mock('react', () => ({
...jest.requireActual('react'),
useState: jest.fn(),
}));
describe('<UnitTranslationPlugin />', () => {
const props = {
id: 'id',
courseId: 'courseId',
unitId: 'unitId',
};
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 });
};
beforeEach(() => {
jest.clearAllMocks();
});
it('render empty when translation is not enabled', () => {
mockInitialState({ enabled: false });
const wrapper = shallow(<UnitTranslationPlugin {...props} />);
expect(wrapper.isEmptyRender()).toBe(true);
});
it('render empty when available languages is empty', () => {
mockInitialState({
availableLanguages: [],
});
const wrapper = shallow(<UnitTranslationPlugin {...props} />);
expect(wrapper.isEmptyRender()).toBe(true);
});
it('render empty when course language has not been set', () => {
mockInitialState({
language: null,
});
const wrapper = shallow(<UnitTranslationPlugin {...props} />);
expect(wrapper.isEmptyRender()).toBe(true);
});
it('render empty when student is enroll as verified', () => {
mockInitialState({
enrollmentMode: 'audit',
});
const wrapper = shallow(<UnitTranslationPlugin {...props} />);
expect(wrapper.isEmptyRender()).toBe(true);
});
it('render translation when the user is staff', () => {
mockInitialState({
enrollmentMode: 'audit',
isStaff: true,
});
const wrapper = shallow(<UnitTranslationPlugin {...props} />);
expect(wrapper.snapshot).toMatchSnapshot();
});
it('render TranslationSelection when translation is enabled and language is available', () => {
mockInitialState({});
const wrapper = shallow(<UnitTranslationPlugin {...props} />);
expect(wrapper.snapshot).toMatchSnapshot();
});
});