-
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.
- Loading branch information
1 parent
44b48a2
commit 1ad1078
Showing
7 changed files
with
238 additions
and
27 deletions.
There are no files selected for viewing
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
56 changes: 56 additions & 0 deletions
56
...earch/public/applications/app_search/components/credentials/credentials_list/key.test.tsx
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,56 @@ | ||
/* | ||
* 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 { shallow } from 'enzyme'; | ||
import { EuiButtonIcon } from '@elastic/eui'; | ||
|
||
import { Key } from './key'; | ||
|
||
describe('Key', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
const props = { | ||
copy: jest.fn(), | ||
toggleIsHidden: jest.fn(), | ||
isHidden: true, | ||
text: '*****', | ||
}; | ||
|
||
it('renders', () => { | ||
const wrapper = shallow(<Key {...props} />); | ||
expect(wrapper.find(EuiButtonIcon).length).toEqual(2); | ||
}); | ||
|
||
it('will call copy when the first button is clicked', () => { | ||
const wrapper = shallow(<Key {...props} />); | ||
(wrapper.find(EuiButtonIcon).at(0).props() as any).onClick(); | ||
expect(props.copy).toHaveBeenCalled(); | ||
}); | ||
|
||
it('will call hide when the second button is clicked', () => { | ||
const wrapper = shallow(<Key {...props} />); | ||
(wrapper.find(EuiButtonIcon).at(1).props() as any).onClick(); | ||
expect(props.toggleIsHidden).toHaveBeenCalled(); | ||
}); | ||
|
||
it('will render the "eye" icon when isHidden is true', () => { | ||
const wrapper = shallow(<Key {...props} />); | ||
expect((wrapper.find(EuiButtonIcon).at(1).props() as any).iconType).toBe('eye'); | ||
}); | ||
|
||
it('will render the "eyeClosed" icon when isHidden is false', () => { | ||
const wrapper = shallow(<Key {...{ ...props, isHidden: false }} />); | ||
expect((wrapper.find(EuiButtonIcon).at(1).props() as any).iconType).toBe('eyeClosed'); | ||
}); | ||
|
||
it('will render the provided text', () => { | ||
const wrapper = shallow(<Key {...props} />); | ||
expect(wrapper.text()).toContain('*****'); | ||
}); | ||
}); |
42 changes: 42 additions & 0 deletions
42
...ise_search/public/applications/app_search/components/credentials/credentials_list/key.tsx
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,42 @@ | ||
/* | ||
* 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 { EuiButtonIcon } from '@elastic/eui'; | ||
import { i18n } from '@kbn/i18n'; | ||
|
||
interface Props { | ||
copy: () => void; | ||
toggleIsHidden: () => void; | ||
isHidden: boolean; | ||
text: string; | ||
} | ||
|
||
export const Key: React.FC<Props> = ({ copy, toggleIsHidden, isHidden, text }) => { | ||
const icon = isHidden ? 'eye' : 'eyeClosed'; | ||
return ( | ||
<> | ||
<EuiButtonIcon | ||
onClick={copy} | ||
iconType="copyClipboard" | ||
aria-label={i18n.translate('xpack.enterpriseSearch.appSearch.credentials.copyApiKey', { | ||
defaultMessage: 'Copy API Key to clipboard', | ||
})} | ||
/> | ||
<EuiButtonIcon | ||
onClick={toggleIsHidden} | ||
iconType={icon} | ||
aria-label={i18n.translate( | ||
'xpack.enterpriseSearch.appSearch.credentials.toggleApiEndpoint', | ||
{ | ||
defaultMessage: 'Toggle API Key visibility', | ||
} | ||
)} | ||
/> | ||
{text} | ||
</> | ||
); | ||
}; |
61 changes: 61 additions & 0 deletions
61
x-pack/plugins/enterprise_search/public/applications/shared/hidden_text/hidden_text.test.tsx
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,61 @@ | ||
/* | ||
* 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 { shallow } from 'enzyme'; | ||
|
||
import { HiddenText } from '.'; | ||
|
||
describe('HiddenText', () => { | ||
it('provides the passed "text" in a "hiddenText" field, with all characters replaced with stars by default', () => { | ||
const wrapper = shallow( | ||
<HiddenText text="hidden_test"> | ||
{({ hiddenText, isHidden, toggle }) => <div>{hiddenText}</div>} | ||
</HiddenText> | ||
); | ||
expect(wrapper.text()).toEqual('***********'); | ||
}); | ||
|
||
it('provides a "toggle" function, which when called, changes "hiddenText" from stars back to the original text', () => { | ||
let toggleFn = () => {}; | ||
|
||
const wrapper = shallow( | ||
<HiddenText text="hidden_test"> | ||
{({ hiddenText, isHidden, toggle }) => { | ||
toggleFn = toggle; | ||
return <div>{hiddenText}</div>; | ||
}} | ||
</HiddenText> | ||
); | ||
|
||
expect(wrapper.text()).toEqual('***********'); | ||
toggleFn(); | ||
expect(wrapper.text()).toEqual('hidden_test'); | ||
toggleFn(); | ||
expect(wrapper.text()).toEqual('***********'); | ||
}); | ||
|
||
it('provides a "hidden" boolean, which which tracks whether or not the text is starred or not', () => { | ||
let toggleFn = () => {}; | ||
let isHiddenBool = false; | ||
|
||
shallow( | ||
<HiddenText text="hidden_test"> | ||
{({ hiddenText, isHidden, toggle }) => { | ||
isHiddenBool = isHidden; | ||
toggleFn = toggle; | ||
return <div>{hiddenText}</div>; | ||
}} | ||
</HiddenText> | ||
); | ||
|
||
expect(isHiddenBool).toEqual(true); | ||
toggleFn(); | ||
expect(isHiddenBool).toEqual(false); | ||
toggleFn(); | ||
expect(isHiddenBool).toEqual(true); | ||
}); | ||
}); |
29 changes: 29 additions & 0 deletions
29
x-pack/plugins/enterprise_search/public/applications/shared/hidden_text/hidden_text.tsx
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,29 @@ | ||
/* | ||
* 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, { useState, ReactElement } from 'react'; | ||
|
||
interface ChildrenProps { | ||
toggle: () => void; | ||
isHidden: boolean; | ||
hiddenText: string; | ||
} | ||
|
||
interface Props { | ||
text: string; | ||
children(props: ChildrenProps): ReactElement; | ||
} | ||
|
||
export const HiddenText: React.FC<Props> = ({ text, children }) => { | ||
const [isHidden, toggleIsHidden] = useState(true); | ||
const hiddenText = isHidden ? text.replace(/./g, '*') : text; | ||
|
||
return children({ | ||
hiddenText, | ||
isHidden, | ||
toggle: () => toggleIsHidden(!isHidden), | ||
}); | ||
}; |
7 changes: 7 additions & 0 deletions
7
x-pack/plugins/enterprise_search/public/applications/shared/hidden_text/index.ts
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,7 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export { HiddenText } from './hidden_text'; |