-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ML] Migrates ml-form-label to EUI/React. (#21059)
- Migrates the ml-form-label directive to use EUI/React. - Exposes both FormLabel and JsonTooltip as React components from individual files so they can be used in a React context when the wrapping element also has been already ported to React. - Adds jests based tests for the FormLabel and JsonTooltip components. They try where possible to make the same assertions like the mocha based tests. The mocha based tests are kept in the code for now so the code gets still tested in a angular based context and as a reference to have the same mocha/jest based tests side by side as a reference for migration. - The FormLabel component is done in a way so it supports transclusion in both cases when used with React alone (using the children prop) and angularjs (using a ref callback and angular's transclude()).
- Loading branch information
Showing
11 changed files
with
293 additions
and
68 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
x-pack/plugins/ml/public/components/form_label/__snapshots__/form_label.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`FormLabel Basic initialization 1`] = ` | ||
<React.Fragment> | ||
<label | ||
className="euiFormLabel" | ||
id="ml_aria_label_undefined" | ||
/> | ||
<Component | ||
position="top" | ||
/> | ||
</React.Fragment> | ||
`; | ||
|
||
exports[`FormLabel Full initialization 1`] = ` | ||
<React.Fragment> | ||
<label | ||
className="euiFormLabel" | ||
id="ml_aria_label_uid" | ||
> | ||
Label Text | ||
</label> | ||
<Component | ||
id="uid" | ||
position="top" | ||
/> | ||
</React.Fragment> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
x-pack/plugins/ml/public/components/form_label/form_label.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { shallow } from 'enzyme'; | ||
import React from 'react'; | ||
|
||
import { FormLabel } from './form_label'; | ||
|
||
describe('FormLabel', () => { | ||
|
||
test('Basic initialization', () => { | ||
const wrapper = shallow(<FormLabel />); | ||
const props = wrapper.props(); | ||
expect(props.labelId).toBeUndefined(); | ||
expect(wrapper.find('label').text()).toBe(''); | ||
expect(wrapper).toMatchSnapshot(); | ||
}); | ||
|
||
test('Full initialization', () => { | ||
const labelId = 'uid'; | ||
const labelText = 'Label Text'; | ||
const wrapper = shallow(<FormLabel labelId={labelId}>{labelText}</FormLabel>); | ||
|
||
const labelElement = wrapper.find('label'); | ||
expect(labelElement.props().id).toBe(`ml_aria_label_${labelId}`); | ||
expect(labelElement.text()).toBe(labelText); | ||
expect(wrapper).toMatchSnapshot(); | ||
}); | ||
}); |
52 changes: 52 additions & 0 deletions
52
x-pack/plugins/ml/public/components/form_label/form_label_directive.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
|
||
|
||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
|
||
import angular from 'angular'; | ||
import { uiModules } from 'ui/modules'; | ||
const module = uiModules.get('apps/ml', ['react']); | ||
|
||
import { FormLabel } from './form_label'; | ||
|
||
// directive for creating a form label including a hoverable icon | ||
// to provide additional information in a tooltip. label and tooltip | ||
// text elements get unique ids based on label-id so they can be | ||
// referenced by attributes, for example: | ||
// | ||
// <ml-form-label label-id="uid">Label Text</ml-form-label> | ||
// <input | ||
// type="text" | ||
// aria-labelledby="ml_aria_label_uid" | ||
// aria-describedby="ml_aria_description_uid" | ||
// /> | ||
module.directive('mlFormLabel', function () { | ||
return { | ||
scope: { | ||
labelId: '@' | ||
}, | ||
restrict: 'E', | ||
replace: false, | ||
transclude: true, | ||
link: (scope, element, attrs, ctrl, transclude) => { | ||
const props = { | ||
labelId: scope.labelId, | ||
labelClassName: 'kuiFormLabel', | ||
// transclude the label text/elements from the angular template | ||
// to the labelRef from the react component. | ||
ref: c => angular.element(c.labelRef.current).append(transclude()) | ||
}; | ||
|
||
ReactDOM.render( | ||
React.createElement(FormLabel, props), | ||
element[0] | ||
); | ||
} | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,5 @@ | |
|
||
|
||
|
||
import './form_label'; | ||
import './form_label_directive'; | ||
import './styles/main.less'; |
54 changes: 54 additions & 0 deletions
54
x-pack/plugins/ml/public/components/json_tooltip/__snapshots__/json_tooltip.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`JsonTooltip Initialization with a non-existing tooltip attribute doesn't throw an error 1`] = ` | ||
<span | ||
aria-hidden="true" | ||
className="ml-info-icon" | ||
> | ||
<EuiIconTip | ||
aria-label="Info" | ||
content="" | ||
type="questionInCircle" | ||
/> | ||
<span | ||
className="ml-info-tooltip-text" | ||
id="ml_aria_description_non_existing_attribute" | ||
/> | ||
</span> | ||
`; | ||
|
||
exports[`JsonTooltip Initialize with existing tooltip attribute 1`] = ` | ||
<span | ||
aria-hidden="true" | ||
className="ml-info-icon" | ||
> | ||
<EuiIconTip | ||
aria-label="Info" | ||
content="Unique identifier for job, can use lowercase alphanumeric and underscores." | ||
type="questionInCircle" | ||
/> | ||
<span | ||
className="ml-info-tooltip-text" | ||
id="ml_aria_description_new_job_id" | ||
> | ||
Unique identifier for job, can use lowercase alphanumeric and underscores. | ||
</span> | ||
</span> | ||
`; | ||
|
||
exports[`JsonTooltip Plain initialization doesn't throw an error 1`] = ` | ||
<span | ||
aria-hidden="true" | ||
className="ml-info-icon" | ||
> | ||
<EuiIconTip | ||
aria-label="Info" | ||
content="" | ||
type="questionInCircle" | ||
/> | ||
<span | ||
className="ml-info-tooltip-text" | ||
id="ml_aria_description_undefined" | ||
/> | ||
</span> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
x-pack/plugins/ml/public/components/json_tooltip/json_tooltip.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { shallow } from 'enzyme'; | ||
import React from 'react'; | ||
|
||
import { JsonTooltip } from './json_tooltip'; | ||
import tooltips from './tooltips.json'; | ||
|
||
describe('JsonTooltip', () => { | ||
|
||
test(`Plain initialization doesn't throw an error`, () => { | ||
const wrapper = shallow(<JsonTooltip />); | ||
expect(wrapper).toMatchSnapshot(); | ||
}); | ||
|
||
test(`Initialization with a non-existing tooltip attribute doesn't throw an error`, () => { | ||
const id = 'non_existing_attribute'; | ||
const wrapper = shallow(<JsonTooltip id={id} />); | ||
expect(wrapper).toMatchSnapshot(); | ||
}); | ||
|
||
test('Initialize with existing tooltip attribute', () => { | ||
const id = 'new_job_id'; | ||
const wrapper = shallow(<JsonTooltip id={id} />); | ||
|
||
// test the rendered span element which should be referenced by aria-describedby | ||
const span = wrapper.find('span.ml-info-tooltip-text'); | ||
expect(span.props().id).toBe(`ml_aria_description_${id}`); | ||
expect(span.text()).toBe(tooltips[id].text); | ||
expect(wrapper).toMatchSnapshot(); | ||
}); | ||
}); |
Oops, something went wrong.