Skip to content

Commit

Permalink
Fix tests for lib and internal components
Browse files Browse the repository at this point in the history
* Fix tests for lib/EditableTextInput component
* Add preliminary tests for StatusPropertiesLabel
  • Loading branch information
bastian-src committed Mar 27, 2024
1 parent eef578f commit 72165a7
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 44 deletions.
57 changes: 26 additions & 31 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
const { foremanLocation, foremanRelativePath } = require('@theforeman/find-foreman')
const tfmConfig = require('@theforeman/test/src/pluginConfig');
const {
foremanRelativePath,
foremanLocation,
} = require('@theforeman/find-foreman');

const foremanReactRelative = 'webpack/assets/javascripts/react_app';
const foremanFull = foremanLocation();
const foremanReactFull = foremanRelativePath(foremanReactRelative);

// Jest configuration
module.exports = {
testURL: 'http://localhost/',
setupFiles: [
'./webpack/test_setup.js',
],
setupFilesAfterEnv: [
'./webpack/global_test_setup.js',
'@testing-library/jest-dom'
],
testPathIgnorePatterns: [
'/node_modules/',
'<rootDir>/foreman/',
'<rootDir>/.+fixtures.+',
'<rootDir>/engines',
],
moduleDirectories: [
`${foremanFull}/node_modules`,
`${foremanFull}/node_modules/@theforeman/vendor-core/node_modules`,
'node_modules',
'webpack/test-utils',
],
modulePathIgnorePatterns: [
'<rootDir>/foreman/',
],
moduleNameMapper: {
'^.+\\.(css|scss)$': 'identity-obj-proxy',
'^foremanReact(.*)$': `${foremanReactFull}/$1`,
},
};
// Find correct path to foremanReact so we do not have to mock it in tests
tfmConfig.moduleNameMapper['^foremanReact(.*)$'] = `${foremanReactFull}/$1`;

tfmConfig.setupFiles = ['./webpack/test_setup.js'];
tfmConfig.setupFilesAfterEnv = [
'./webpack/global_test_setup.js',
'@testing-library/jest-dom',
];

// Do not use default resolver
tfmConfig.resolver = null;
// Specify module dirs instead
tfmConfig.moduleDirectories = [
`${foremanFull}/node_modules`,
`${foremanFull}/node_modules/@theforeman/vendor-core/node_modules`,
'node_modules',
'webpack/test-utils',
];

module.exports = tfmConfig;
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* eslint-disable promise/prefer-await-to-then */
// Configure Enzyme
import { mount } from '@theforeman/test';
import React from 'react';
import { Provider } from 'react-redux';
import store from 'foremanReact/redux';
import LabelIcon from 'foremanReact/components/common/LabelIcon';
import StatusPropertiesLabel from './StatusPropertiesLabel';

// jest.mock('foremanReact/common/withReactRoutes', () => Component => props => (
// <div className="component-with-mocked-routes">
// <Component {...props} />
// </div>
// ));

const defaultProps = {
color: 'blue',
iconChild: <LabelIcon text="test" />,
statusContent: 'some content',
linkUrl: '/test/link',
tooltipText: 'Some nice tooltip',
};

describe('StatusPropertiesLabel', () => {
const wrapper = mount(
<Provider store={store}>
<StatusPropertiesLabel {...defaultProps} />
</Provider>
);

it('includes components', () => {
expect(wrapper.find('Tooltip').exists()).toBe(true);
expect(wrapper.find('Tooltip')).toHaveLength(1);
expect(wrapper.find('Label').exists()).toBe(true);
expect(wrapper.find('Label')).toHaveLength(1);
expect(wrapper.find('Link').exists()).toBe(true);
expect(wrapper.find('Link')).toHaveLength(1);
});

it('passes properties', () => {
// ToolTip
const tooltip = wrapper.find('Tooltip');
expect(tooltip.props()).toHaveProperty('content');
expect(tooltip.prop('content')).toContain(defaultProps.tooltipText);
// Label
const label = wrapper.find('Label');
expect(label.props()).toHaveProperty('icon');
expect(label.prop('icon')).toEqual(defaultProps.iconChild);
expect(label.props()).toHaveProperty('color');
expect(label.prop('color')).toEqual(defaultProps.color);
// Link
const link = wrapper.find('Link');
expect(link.props()).toHaveProperty('to');
expect(link.prop('to')).toEqual(defaultProps.linkUrl);
});
});
14 changes: 6 additions & 8 deletions webpack/lib/EditableTextInput/EditableTextInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,14 @@ const EditableTextInput = ({
setCurrentAttribute(attribute);
};

const onSubmit = async event => {
if (!event.shiftKey) {
setEditing(false);
if (isPassword) {
if (inputValue?.length > 0) {
setPasswordPlaceholder(PASSWORD_MASK);
}
const onSubmit = async () => {
setEditing(false);
if (isPassword) {
if (inputValue?.length > 0) {
setPasswordPlaceholder(PASSWORD_MASK);
}
await onEdit(inputValue, attribute);
}
await onEdit(inputValue, attribute);
};

const onClear = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
render,
patientlyWaitFor,
fireEvent,
} from './react-testing-lib-wrapper';
} from '../../react-testing-lib-wrapper';
import EditableTextInput from '../EditableTextInput';

const actualValue = 'burger';
Expand Down Expand Up @@ -38,16 +38,16 @@ test('Passed function is called after editing and hitting enter', async () => {
);

getByLabelText(`edit ${attribute}`).click();
const textInputLabel = `${attribute} text input`;
fireEvent.change(getByLabelText(textInputLabel), {
const textInputLabel = getByLabelText(`${attribute} text input`);
fireEvent.change(textInputLabel, {
target: { value: actualValue },
});
fireEvent.keyUp(getByLabelText(textInputLabel), {
fireEvent.keyUp(textInputLabel, {
key: 'Enter',
code: 'Enter',
});

await patientlyWaitFor(() => expect(mockEdit.mock.calls).toHaveLength(1));

expect(head(mockEdit.mock.calls)).toContain(actualValue); // first arg
});

Expand Down
File renamed without changes.

0 comments on commit 72165a7

Please sign in to comment.