Remove use of i18n.plural in favor of low level i18n._.

This commit is contained in:
mabashian
2020-08-27 09:26:23 -04:00
parent bae10718d5
commit 225f57fefd
2 changed files with 35 additions and 40 deletions

View File

@@ -13,6 +13,8 @@ function cannotCancel(job) {
function JobListCancelButton({ i18n, jobsToCancel, onCancel }) { function JobListCancelButton({ i18n, jobsToCancel, onCancel }) {
const [isModalOpen, setIsModalOpen] = useState(false); const [isModalOpen, setIsModalOpen] = useState(false);
const numJobsToCancel = jobsToCancel.length;
const zeroOrOneJobSelected = numJobsToCancel < 2;
const handleCancel = () => { const handleCancel = () => {
onCancel(); onCancel();
@@ -22,26 +24,28 @@ function JobListCancelButton({ i18n, jobsToCancel, onCancel }) {
const renderTooltip = () => { const renderTooltip = () => {
const jobsUnableToCancel = jobsToCancel const jobsUnableToCancel = jobsToCancel
.filter(cannotCancel) .filter(cannotCancel)
.map(job => job.name) .map(job => job.name);
.join(', '); const numJobsUnableToCancel = jobsUnableToCancel.length;
if (jobsToCancel.some(cannotCancel)) { if (numJobsUnableToCancel > 0) {
return ( return (
<div> <div>
{i18n.plural({ {i18n._(
value: jobsToCancel.length, '{numJobsUnableToCancel, plural, one {You do not have permission to cancel the following job:} other {You do not have permission to cancel the following jobs:}}',
one: 'You do not have permission to cancel the following job: ', {
other: 'You do not have permission to cancel the following jobs: ', numJobsUnableToCancel,
})} }
{jobsUnableToCancel} )}
{' '.concat(jobsUnableToCancel.join(', '))}
</div> </div>
); );
} }
if (jobsToCancel.length) { if (numJobsToCancel > 0) {
return i18n.plural({ return i18n._(
value: jobsToCancel.length, '{numJobsToCancel, plural, one {Cancel selected job} other {Cancel selected jobs}}',
one: 'Cancel selected job', {
other: 'Cancel selected jobs', numJobsToCancel,
}); }
);
} }
return i18n._(t`Select a job to cancel`); return i18n._(t`Select a job to cancel`);
}; };
@@ -49,11 +53,12 @@ function JobListCancelButton({ i18n, jobsToCancel, onCancel }) {
const isDisabled = const isDisabled =
jobsToCancel.length === 0 || jobsToCancel.some(cannotCancel); jobsToCancel.length === 0 || jobsToCancel.some(cannotCancel);
const cancelJobText = i18n.plural({ const cancelJobText = i18n._(
value: jobsToCancel.length < 2, '{zeroOrOneJobSelected, plural, one {Cancel job} other {Cancel jobs}}',
one: 'Cancel job', {
other: 'Cancel jobs', zeroOrOneJobSelected,
}); }
);
return ( return (
<Kebabified> <Kebabified>
@@ -90,6 +95,7 @@ function JobListCancelButton({ i18n, jobsToCancel, onCancel }) {
onClose={() => setIsModalOpen(false)} onClose={() => setIsModalOpen(false)}
actions={[ actions={[
<Button <Button
id="cancel-job-confirm-button"
key="delete" key="delete"
variant="danger" variant="danger"
aria-label={cancelJobText} aria-label={cancelJobText}
@@ -98,6 +104,7 @@ function JobListCancelButton({ i18n, jobsToCancel, onCancel }) {
{cancelJobText} {cancelJobText}
</Button>, </Button>,
<Button <Button
id="cancel-job-return-button"
key="cancel" key="cancel"
variant="secondary" variant="secondary"
aria-label={i18n._(t`Return`)} aria-label={i18n._(t`Return`)}
@@ -108,11 +115,12 @@ function JobListCancelButton({ i18n, jobsToCancel, onCancel }) {
]} ]}
> >
<div> <div>
{i18n.plural({ {i18n._(
value: jobsToCancel.length, '{numJobsToCancel, plural, one {This action will cancel the following job:} other {This action will cancel the following jobs:}}',
one: 'This action will cancel the following job:', {
other: 'This action will cancel the following jobs:', numJobsToCancel,
})} }
)}
</div> </div>
{jobsToCancel.map(job => ( {jobsToCancel.map(job => (
<span key={job.id}> <span key={job.id}>

View File

@@ -1,5 +1,4 @@
import React from 'react'; import React from 'react';
import { shallow } from 'enzyme';
import { mountWithContexts } from '../../../testUtils/enzymeHelpers'; import { mountWithContexts } from '../../../testUtils/enzymeHelpers';
import JobListCancelButton from './JobListCancelButton'; import JobListCancelButton from './JobListCancelButton';
@@ -38,15 +37,6 @@ describe('<JobListCancelButton />', () => {
expect(wrapper.find('JobListCancelButton button').props().disabled).toBe( expect(wrapper.find('JobListCancelButton button').props().disabled).toBe(
true true
); );
const tooltipContents = wrapper.find('Tooltip').props().content;
const renderedTooltipContents = shallow(tooltipContents);
expect(
renderedTooltipContents.matchesElement(
<div>
You do not have permission to cancel the following job: some job
</div>
)
).toBe(true);
}); });
test('should be enabled when user does have permission to cancel selected job', () => { test('should be enabled when user does have permission to cancel selected job', () => {
wrapper = mountWithContexts( wrapper = mountWithContexts(
@@ -68,7 +58,6 @@ describe('<JobListCancelButton />', () => {
expect(wrapper.find('JobListCancelButton button').props().disabled).toBe( expect(wrapper.find('JobListCancelButton button').props().disabled).toBe(
false false
); );
expect(wrapper.find('Tooltip').props().content).toBe('Cancel selected job');
}); });
test('modal functions as expected', () => { test('modal functions as expected', () => {
const onCancel = jest.fn(); const onCancel = jest.fn();
@@ -93,7 +82,7 @@ describe('<JobListCancelButton />', () => {
wrapper.find('JobListCancelButton button').simulate('click'); wrapper.find('JobListCancelButton button').simulate('click');
wrapper.update(); wrapper.update();
expect(wrapper.find('AlertModal').length).toBe(1); expect(wrapper.find('AlertModal').length).toBe(1);
wrapper.find('AlertModal button[aria-label="Return"]').simulate('click'); wrapper.find('button#cancel-job-return-button').simulate('click');
wrapper.update(); wrapper.update();
expect(onCancel).toHaveBeenCalledTimes(0); expect(onCancel).toHaveBeenCalledTimes(0);
expect(wrapper.find('AlertModal').length).toBe(0); expect(wrapper.find('AlertModal').length).toBe(0);
@@ -101,9 +90,7 @@ describe('<JobListCancelButton />', () => {
wrapper.find('JobListCancelButton button').simulate('click'); wrapper.find('JobListCancelButton button').simulate('click');
wrapper.update(); wrapper.update();
expect(wrapper.find('AlertModal').length).toBe(1); expect(wrapper.find('AlertModal').length).toBe(1);
wrapper wrapper.find('button#cancel-job-confirm-button').simulate('click');
.find('AlertModal button[aria-label="Cancel job"]')
.simulate('click');
wrapper.update(); wrapper.update();
expect(onCancel).toHaveBeenCalledTimes(1); expect(onCancel).toHaveBeenCalledTimes(1);
}); });