Skip to content

Commit

Permalink
Added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rmathis committed Jun 11, 2021
1 parent 1d15059 commit ad12b5c
Show file tree
Hide file tree
Showing 8 changed files with 250 additions and 13 deletions.
12 changes: 3 additions & 9 deletions ui/src/app/metadata/component/DeleteConfirmation.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,20 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faExclamationTriangle } from '@fortawesome/free-solid-svg-icons';
import Translate from '../../i18n/components/translate';

export function DeleteConfirmation({ children, onConfirm, onCancel, body, title }) {
export function DeleteConfirmation({ children, body, title }) {

const [deleting, setDeleting] = React.useState(false);

const ref = React.useRef();

const onConfirmClick = () => {
if (onConfirm) {
onConfirm();
}
if (ref.current && typeof ref.current === 'function') {
ref.current();
}
setDeleting(false);
}

const onCancelClick = () => {
if (onCancel) {
onCancel();
}
setDeleting(false);
};

Expand All @@ -47,9 +41,9 @@ export function DeleteConfirmation({ children, onConfirm, onCancel, body, title
</p>
</Modal.Body>
<Modal.Footer>
<Button variant="danger" onClick={() => onConfirmClick()}>
<Button className="mr-2" variant="danger" onClick={() => onConfirmClick()}>
<Translate value="action.delete">Delete</Translate>
</Button>{' '}
</Button>
<Button variant="secondary" onClick={() => onCancelClick()}>
<Translate value="action.cancel">Cancel</Translate>
</Button>
Expand Down
46 changes: 46 additions & 0 deletions ui/src/app/metadata/component/DeleteConfirmation.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React from 'react';
import ReactDOM from 'react-dom';
import { act } from 'react-dom/test-utils';

import { DeleteConfirmation } from './DeleteConfirmation';

jest.mock('../../i18n/components/translate', () => {
return 'span';
})

const noop = jest.fn();

let container;

beforeEach(() => {
container = document.createElement('div');
document.body.appendChild(container);
});

afterEach(() => {
document.body.removeChild(container);
container = null;
});

test('Delete confirmation', () => {
act(() => {
ReactDOM.render(
<DeleteConfirmation title={`message.delete-filter-title`} body={`message.delete-filter-body`}>
{(block) => <button onClick={() => block(() => noop())}></button>}
</DeleteConfirmation>,
container);
});

const initiator = container.querySelector('button');

act(() => {
initiator.dispatchEvent(new MouseEvent('click', { bubbles: true }));
});

let modal = container.querySelector('.modal');
const confirm = container.querySelector('.btn-danger');

expect(modal).toBeDefined();
expect(confirm).toBeDefined();

});
3 changes: 0 additions & 3 deletions ui/src/app/metadata/domain/provider/BaseProviderDefinition.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,6 @@ export const HttpMetadataResolverAttributesSchema = {
options: DurationOptions,
'ui:placeholder': 'label.duration'
},
proxyPort: {
'ui:widget': 'updown'
},
httpClientRef: {
'ui:widget': 'hidden'
},
Expand Down
160 changes: 160 additions & 0 deletions ui/src/app/metadata/domain/provider/BaseProviderDefinition.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
import { BaseProviderDefinition } from './BaseProviderDefinition';
import schema from '../../../../testing/dynamic-http.schema';
const addErrorMockFn = jest.fn();

const providers = [
{
resourceId: 1,
name: 'foo',
xmlId: 'bar'
},
{
resourceId: 2,
name: 'baz',
xmlId: 'xmlId'
}
];

const e = {
name: { addError: addErrorMockFn },
xmlId: { addError: addErrorMockFn }
};

describe('validator function', () => {
it('should validate against the providers', () => {
const validator = BaseProviderDefinition.validator(providers, 2);
const errors = validator({
name: 'foo',
xmlId: 'xmlId'
}, e);

expect(addErrorMockFn).toHaveBeenCalledTimes(2);
});

it('should check against the current id', () => {
const validator = BaseProviderDefinition.validator(providers, 1);
const errors = validator({
name: 'foo',
xmlId: 'naz'
}, e);

expect(addErrorMockFn).toHaveBeenCalledTimes(1);
});
});

describe('parser function', () => {

const base = {
metadataFilters: [
{
resourceId: 'foo',
name: 'foo',
'@type': 'NameIDFormat'
}
]
};

const changes = {
metadataFilters: [
{
'@type': "RequiredValidUntil",
audId: 19,
createdBy: "root",
createdDate: "2021-06-11T13:29:18.384219",
current: false,
filterEnabled: false,
maxValidityInterval: "PT30S",
resourceId: "299feea8-3e2c-433e-bc2e-48d28bf84a8c"
}
]
};

it('should validate against the providers', () => {
const parsed = BaseProviderDefinition.parser(
changes, base
);

expect(parsed.metadataFilters.length).toBe(2);
});
});

describe('formatter function', () => {

const md = {
metadataFilters: [
{
resourceId: 'foo',
name: 'foo',
'@type': 'NameIDFormat'
},
{
'@type': "RequiredValidUntil",
audId: 19,
createdBy: "root",
createdDate: "2021-06-11T13:29:18.384219",
current: false,
filterEnabled: false,
maxValidityInterval: "PT30S",
resourceId: "299feea8-3e2c-433e-bc2e-48d28bf84a8c"
}
]
};

it('should validate against the providers', () => {
const parsed = BaseProviderDefinition.formatter(
md, schema
);

expect(parsed.metadataFilters.length).toBe(3);
});

it('should return no changes if no filters are on the provider', () => {
const parsed = BaseProviderDefinition.formatter(
{}, schema
);

expect(parsed.metadataFilters).toBeUndefined();
});

it('should return no changes if no provider is passed', () => {
const parsed = BaseProviderDefinition.formatter(
null, schema
);

expect(parsed).toBeNull();
});
});

describe('display function', () => {

const md = {
metadataFilters: [
{
resourceId: 'foo',
name: 'foo',
'@type': 'NameIDFormat'
},
{
'@type': "RequiredValidUntil",
audId: 19,
createdBy: "root",
createdDate: "2021-06-11T13:29:18.384219",
current: false,
filterEnabled: false,
maxValidityInterval: "PT30S",
resourceId: "299feea8-3e2c-433e-bc2e-48d28bf84a8c"
}
]
};

it('should validate against the providers', () => {
const parsed = BaseProviderDefinition.display(md);
expect(Array.isArray(parsed)).toBe(false);
});

it('should return an unmodified provider if no filters are present', () => {
const local = {};
const parsed = BaseProviderDefinition.display(local);
expect(parsed).toEqual(local);
});
});
15 changes: 15 additions & 0 deletions ui/src/app/metadata/domain/source/SourceDefinition.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { SourceBase } from './SourceDefinition';

describe('SourceDefinition', () => {
describe('parser', () => {
it('should remove null values', () => {
expect(SourceBase.parser({
foo: null,
bar: 'baz',
baz: {
bar: null
}
})).toEqual({bar: 'baz'});
});
})
});
1 change: 0 additions & 1 deletion ui/src/app/metadata/editor/MetadataEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export function MetadataEditor ({ current }) {

const { state, dispatch } = React.useContext(MetadataFormContext);
const { metadata, errors } = state;

const onChange = (changes) => {
dispatch(setFormDataAction(changes.formData));
dispatch(setFormErrorAction(changes.errors));
Expand Down
3 changes: 3 additions & 0 deletions ui/src/testing/dynamic-http.schema.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions ui/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9400,6 +9400,11 @@ react-infinite-scroll-component@^6.1.0:
dependencies:
throttle-debounce "^2.1.0"

"react-is@^16.12.0 || ^17.0.0", react-is@^17.0.2:
version "17.0.2"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-17.0.2.tgz#e691d4a8e9c789365655539ab372762b0efb54f0"
integrity sha512-w2GsyukL62IJnlaff/nRegPQR94C/XXamvMWmSHRJ4y7Ts/4ocGRmTHvOs8PSE6pB3dWOrD/nueuU5sduBsQ4w==

react-is@^16.3.2, react-is@^16.6.0, react-is@^16.7.0, react-is@^16.8.1, react-is@^16.9.0:
version "16.13.1"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
Expand Down Expand Up @@ -9564,6 +9569,24 @@ react-scroll@^1.8.2:
lodash.throttle "^4.1.1"
prop-types "^15.7.2"

react-shallow-renderer@^16.13.1:
version "16.14.1"
resolved "https://registry.yarnpkg.com/react-shallow-renderer/-/react-shallow-renderer-16.14.1.tgz#bf0d02df8a519a558fd9b8215442efa5c840e124"
integrity sha512-rkIMcQi01/+kxiTE9D3fdS959U1g7gs+/rborw++42m1O9FAQiNI/UNRZExVUoAOprn4umcXf+pFRou8i4zuBg==
dependencies:
object-assign "^4.1.1"
react-is "^16.12.0 || ^17.0.0"

react-test-renderer@^17.0.2:
version "17.0.2"
resolved "https://registry.yarnpkg.com/react-test-renderer/-/react-test-renderer-17.0.2.tgz#4cd4ae5ef1ad5670fc0ef776e8cc7e1231d9866c"
integrity sha512-yaQ9cB89c17PUb0x6UfWRs7kQCorVdHlutU1boVPEsB8IDZH6n9tHxMacc3y0JoXOJUsZb/t/Mb8FUWMKaM7iQ==
dependencies:
object-assign "^4.1.1"
react-is "^17.0.2"
react-shallow-renderer "^16.13.1"
scheduler "^0.20.2"

react-transition-group@^4.4.1:
version "4.4.1"
resolved "https://registry.yarnpkg.com/react-transition-group/-/react-transition-group-4.4.1.tgz#63868f9325a38ea5ee9535d828327f85773345c9"
Expand Down

0 comments on commit ad12b5c

Please sign in to comment.