Skip to content

Commit

Permalink
Merge pull request #1397 from carbon-design-system/1351-ensure-i18n-s…
Browse files Browse the repository at this point in the history
…trings-tested

Add tests for i18n strings to ensure they appear on the DOM
  • Loading branch information
Jesse Sprinkle authored Jul 28, 2020
2 parents e49467f + 3473ce8 commit 9d0acd0
Show file tree
Hide file tree
Showing 12 changed files with 463 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/components/BarChartCard/BarChartCard.test.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import { mount } from 'enzyme';
import { render, fireEvent, screen } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';

import Table from '../Table/Table';
import { barChartData } from '../../utils/barChartDataSample';
Expand Down Expand Up @@ -178,4 +179,16 @@ describe('BarChartCard', () => {
);
expect(wrapper.find('GroupedBarChart')).toHaveLength(1);
});

it('i18n string test', () => {
const i18nTest = {
noDataLabel: 'no-data-label',
};

const i18nDefault = BarChartCard.defaultProps.i18n;

render(<BarChartCard {...barChartCardProps} values={[]} i18n={i18nTest} />);
expect(screen.getByText(i18nTest.noDataLabel)).toBeInTheDocument();
expect(screen.queryByText(i18nDefault.noDataLabel)).not.toBeInTheDocument();
});
});
117 changes: 117 additions & 0 deletions src/components/DateTimePicker/DateTimePicker.test.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import React from 'react';
import { mount } from 'enzyme';
import moment from 'moment';
import { render, fireEvent, screen } from '@testing-library/react';

import '@testing-library/jest-dom/extend-expect';
import DateTimePicker, {
INTERVAL_VALUES,
RELATIVE_VALUES,
Expand Down Expand Up @@ -345,4 +347,119 @@ describe('DateTimePicker', () => {
wrapper.find('.iot--date-time-picker__listitem--preset-selected').hostNodes()
).toHaveLength(1);
});

it('i18n string test', () => {
const i18nTest = {
toLabel: 'to-label',
toNowLabel: 'to-now-label',
calendarLabel: 'calendar-label',
presetLabels: ['last-30-min', 'last-1-hour', 'last-6-hour', 'last-12-hour', 'last-24-hour'],
intervalLabels: ['mins', 'hrs', 'dys', 'wks', 'mths', 'yrs'],
relativeLabels: ['today', 'yesterday'],
customRangeLinkLabel: 'custom-range',
customRangeLabel: 'custom-range-2',
relativeLabel: 'relative',
lastLabel: 'last',
invalidNumberLabel: 'number-is-not-valid',
relativeToLabel: 'relative-to',
absoluteLabel: 'absolute',
startTimeLabel: 'start-time',
endTimeLabel: 'end-time',
applyBtnLabel: 'apply',
cancelBtnLabel: 'cancel',
backBtnLabel: 'back',
};

const presets = [
{
label: 'last-30-min',
offset: 30,
},
{
label: 'last-1-hour',
offset: 60,
},
{
label: 'last-6-hour',
offset: 360,
},
{
label: 'last-12-hour',
offset: 720,
},
{
label: 'last-24-hour',
offset: 1440,
},
];

const i18nDefault = DateTimePicker.defaultProps.i18n;

const relatives = [
{
label: 'today',
value: RELATIVE_VALUES.TODAY,
},
{
label: 'yesterday',
value: RELATIVE_VALUES.YESTERDAY,
},
];

render(<DateTimePicker presets={presets} i18n={i18nTest} relatives={relatives} />);
i18nTest.presetLabels.forEach(label => {
expect(screen.getAllByText(label)[0]).toBeInTheDocument();
});
expect(screen.getAllByText(i18nTest.toNowLabel, { exact: false })[0]).toBeInTheDocument();
expect(screen.getByLabelText(i18nTest.calendarLabel)).toBeInTheDocument();
expect(screen.getByText(i18nTest.customRangeLinkLabel)).toBeInTheDocument();
expect(screen.getByText(i18nTest.applyBtnLabel)).toBeInTheDocument();
expect(screen.getByText(i18nTest.cancelBtnLabel)).toBeInTheDocument();

i18nDefault.presetLabels.forEach(label => {
expect(screen.queryByText(label)).not.toBeInTheDocument();
});
expect(screen.queryByText(i18nDefault.toNowLabel, { exact: false })).not.toBeInTheDocument();
expect(screen.queryByLabelText(i18nDefault.calendarLabel)).not.toBeInTheDocument();
expect(screen.queryByText(i18nDefault.customRangeLinkLabel)).not.toBeInTheDocument();
expect(screen.queryByText(i18nDefault.applyBtnLabel)).not.toBeInTheDocument();
expect(screen.queryByText(i18nDefault.cancelBtnLabel)).not.toBeInTheDocument();
// custom relative range screen
fireEvent.click(screen.getByText(i18nTest.customRangeLinkLabel));
i18nTest.intervalLabels.forEach(label => {
expect(screen.getAllByText(label)[0]).toBeInTheDocument();
});
i18nTest.relativeLabels.forEach(label => {
expect(screen.getAllByText(label)[0]).toBeInTheDocument();
});
expect(screen.getByText(i18nTest.customRangeLabel)).toBeInTheDocument();
expect(screen.getByText(i18nTest.backBtnLabel)).toBeInTheDocument();
expect(screen.getByText(i18nTest.relativeLabel)).toBeInTheDocument();
expect(screen.getByText(i18nTest.lastLabel)).toBeInTheDocument();

i18nDefault.intervalLabels.forEach(label => {
expect(screen.queryByText(label)).not.toBeInTheDocument();
});
i18nDefault.relativeLabels.forEach(label => {
expect(screen.queryByText(label)).not.toBeInTheDocument();
});
expect(screen.queryByText(i18nDefault.customRangeLabel)).not.toBeInTheDocument();
expect(screen.queryByText(i18nDefault.backBtnLabel)).not.toBeInTheDocument();
expect(screen.queryByText(i18nDefault.relativeLabel)).not.toBeInTheDocument();
expect(screen.queryByText(i18nDefault.lastLabel)).not.toBeInTheDocument();
// custom range absolute screen.
fireEvent.click(screen.getByText(i18nTest.absoluteLabel));
expect(screen.getByText(i18nTest.startTimeLabel)).toBeInTheDocument();
expect(screen.getByText(i18nTest.endTimeLabel)).toBeInTheDocument();

expect(screen.queryByText(i18nDefault.startTimeLabel)).not.toBeInTheDocument();
expect(screen.queryByText(i18nDefault.endTimeLabel)).not.toBeInTheDocument();
// click apply
fireEvent.click(screen.getByText(i18nTest.applyBtnLabel));
expect(screen.getAllByTitle(new RegExp(`.*${i18nTest.toLabel}.*`))[0]).toBeInTheDocument();

expect(
screen.queryByTitle(new RegExp(`.*\\s${i18nDefault.toLabel}\\s.*`))
).not.toBeInTheDocument();
});
});
1 change: 0 additions & 1 deletion src/components/FlyoutMenu/FlyoutMenu.test.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react';
import '@testing-library/jest-dom';
import { fireEvent, render, screen } from '@testing-library/react';

import FlyoutMenu, { FlyoutMenuDirection } from './FlyoutMenu';
Expand Down
17 changes: 17 additions & 0 deletions src/components/ImageCard/ImageHotspots.test.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React from 'react';
import ReactDOM from 'react-dom';
import { act, isDOMComponent } from 'react-dom/test-utils';
import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';

import ImageHotspots, {
calculateImageHeight,
Expand Down Expand Up @@ -191,4 +193,19 @@ describe('ImageHotspots', () => {
expect(mockSetMinimap).toHaveBeenCalled();
expect(mockSetOptions).toHaveBeenCalledWith(expect.objectContaining({ draggable: true }));
});
it('i18n string tests', () => {
const i18nTest = {
zoomIn: 'zoom-in',
zoomOut: 'zoom-out',
zoomToFit: 'zoom-to-fit',
};

const i18nDefault = ImageHotspots.defaultProps.i18n;

render(<ImageHotspots i18n={i18nTest} />);
expect(screen.getByTitle(i18nTest.zoomIn)).toBeInTheDocument();
expect(screen.getByTitle(i18nTest.zoomOut)).toBeInTheDocument();
expect(screen.queryByTitle(i18nDefault.zoomIn)).not.toBeInTheDocument();
expect(screen.queryByTitle(i18nDefault.zoomOut)).not.toBeInTheDocument();
});
});
16 changes: 16 additions & 0 deletions src/components/List/ListHeader/ListHeader.test.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';

import ListHeader from './ListHeader';

Expand All @@ -18,4 +19,19 @@ describe('ListHeader', () => {
render(<ListHeader i18n={{}} />);
expect(screen.queryByText('List Header')).toBeNull();
});

it('ListHeader i18n string tests', () => {
const i18nTest = {
searchPlaceHolderText: 'enter-value',
};

const searchProp = {};

const i18nDefault = ListHeader.defaultProps.i18n;

render(<ListHeader i18n={i18nTest} search={searchProp} />);

expect(screen.getByText(i18nTest.searchPlaceHolderText)).toBeInTheDocument();
expect(screen.queryByText(i18nDefault.searchPlaceHolderText)).not.toBeInTheDocument();
});
});
15 changes: 15 additions & 0 deletions src/components/List/ListItem/ListItem.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,19 @@ describe('ListItem', () => {
expect(rowActionOnClick).toHaveBeenCalledTimes(1);
expect(screen.getByTitle('iconTitle')).toBeVisible();
});

it('ListItem i18n string test', () => {
const i18nTest = {
expand: 'expand',
close: 'close',
};
const i18nDefaults = ListItem.defaultProps.i18n;
const { rerender } = render(<ListItem i18n={i18nTest} id="1" value="" isExpandable />);
expect(screen.getByLabelText(i18nTest.close)).toBeInTheDocument();
expect(screen.queryByLabelText(i18nDefaults.close)).not.toBeInTheDocument();

rerender(<ListItem i18n={i18nTest} id="1" value="" isExpandable expanded />);
expect(screen.getByLabelText(i18nTest.expand)).toBeInTheDocument();
expect(screen.queryByLabelText(i18nDefaults.expand)).not.toBeInTheDocument();
});
});
20 changes: 20 additions & 0 deletions src/components/PageTitleBar/PageTitleBar.test.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React from 'react';
import { mount } from 'enzyme';
import { SkeletonText, Tabs, Tab } from 'carbon-components-react';
import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';

import Button from '../Button';

Expand Down Expand Up @@ -116,4 +118,22 @@ describe('PageTitleBar', () => {
const wrapper = mount(<PageTitleBar title={commonPageTitleBarProps.title} isLoading />);
expect(wrapper.find(SkeletonText)).toHaveLength(1);
});

it('i18n string test', () => {
const i18nTest = {
editIconDescription: 'edit-icon-description',
tooltipIconDescription: 'tooltip-icon-description',
};

const i18nDefault = PageTitleBar.defaultProps.i18n;
render(
<PageTitleBar title="testTitle" i18n={i18nTest} editable description="test" collapsed />
);

expect(screen.getByText(i18nTest.editIconDescription)).toBeInTheDocument();
expect(screen.getByLabelText(i18nTest.tooltipIconDescription)).toBeInTheDocument();

expect(screen.queryByText(i18nDefault.editIconDescription)).not.toBeInTheDocument();
expect(screen.queryByLabelText(i18nDefault.tooltipIconDescription)).not.toBeInTheDocument();
});
});
36 changes: 35 additions & 1 deletion src/components/PageWizard/PageWizard.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { shallow } from 'enzyme';
import { render, fireEvent, screen } from '@testing-library/react';

import PageWizard from './PageWizard';
import PageWizard, { defaultProps } from './PageWizard';
import { content, StepValidation } from './PageWizard.story';

describe('PageWizard', () => {
Expand Down Expand Up @@ -124,4 +124,38 @@ describe('PageWizard', () => {
const wrapper = shallow(<PageWizard currentStepId="step1">{content[0]}</PageWizard>);
expect(wrapper.find('[data-testid="iot--progress-indicator-testid"]')).toHaveLength(0);
});

it('i18n string tests', () => {
const i18nTest = {
back: 'back',
next: 'next',
cancel: 'cancel',
submit: 'submit',
close: 'close',
};

const i18nDefault = defaultProps.i18n;
const { rerender } = render(
<PageWizard i18n={i18nTest} currentStepId="step1" error="error">
{content}
</PageWizard>
);

expect(screen.getByText(i18nTest.next)).toBeInTheDocument();
expect(screen.getByText(i18nTest.cancel)).toBeInTheDocument();
expect(screen.getByLabelText(i18nTest.close)).toBeInTheDocument();
expect(screen.queryByText(i18nDefault.next)).not.toBeInTheDocument();
expect(screen.queryByText(i18nDefault.cancel)).not.toBeInTheDocument();
expect(screen.queryByLabelText(i18nDefault.close)).not.toBeInTheDocument();

rerender(
<PageWizard currentStepId="step5" i18n={i18nTest}>
{content}
</PageWizard>
);
expect(screen.getByText(i18nTest.back)).toBeInTheDocument();
expect(screen.getByText(i18nTest.submit)).toBeInTheDocument();
expect(screen.queryByText(i18nDefault.back)).not.toBeInTheDocument();
expect(screen.queryByText(i18nDefault.submit)).not.toBeInTheDocument();
});
});
Loading

0 comments on commit 9d0acd0

Please sign in to comment.