-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create TextSelectCopyMulti that allows multi lines and adding comments (
#1194) * Remove deprecated use of exec for copying to clipboard * Rename existing Tabs to TabIcon * Add and use @react/user-event library for testing clipboard
- Loading branch information
Showing
14 changed files
with
1,478 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
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
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
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
46 changes: 46 additions & 0 deletions
46
web/packages/teleport/src/components/TextSelectCopy/TextSelectCopyMulti.story.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,46 @@ | ||
/** | ||
* Copyright 2022 Gravitational, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import React from 'react'; | ||
|
||
import { render } from 'design/utils/testing'; | ||
|
||
import * as stories from './TextSelectCopyMulti.story'; | ||
|
||
test('render multi bash texts', () => { | ||
const { container } = render(<stories.BashMulti />); | ||
expect(container).toMatchSnapshot(); | ||
}); | ||
|
||
test('render multi bash texts with comment', () => { | ||
const { container } = render(<stories.BashMultiWithComment />); | ||
expect(container).toMatchSnapshot(); | ||
}); | ||
|
||
test('render single bash text', () => { | ||
const { container } = render(<stories.BashSingle />); | ||
expect(container).toMatchSnapshot(); | ||
}); | ||
|
||
test('render single bash text with comment', () => { | ||
const { container } = render(<stories.BashSingleWithComment />); | ||
expect(container).toMatchSnapshot(); | ||
}); | ||
|
||
test('render non bash single text', () => { | ||
const { container } = render(<stories.NonBash />); | ||
expect(container).toMatchSnapshot(); | ||
}); |
106 changes: 106 additions & 0 deletions
106
web/packages/teleport/src/components/TextSelectCopy/TextSelectCopyMulti.story.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,106 @@ | ||
/** | ||
* Copyright 2022 Gravitational, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import React from 'react'; | ||
|
||
import { TextSelectCopyMulti as Component } from './TextSelectCopyMulti'; | ||
|
||
export default { | ||
title: 'Teleport/TextSelectCopy/Multi', | ||
}; | ||
|
||
export const BashMulti = () => { | ||
return ( | ||
<Component | ||
lines={[ | ||
{ | ||
text: `sudo tctl -c cfg-all users add --roles=access,editor george_washington`, | ||
}, | ||
{ | ||
text: 'sudo DEBUG=1 teleport start -c cfg-all -d', | ||
}, | ||
{ | ||
text: 'yarn start-teleport-e --target=https://localhost:3080/web', | ||
}, | ||
]} | ||
/> | ||
); | ||
}; | ||
|
||
export const BashMultiWithComment = () => { | ||
return ( | ||
<Component | ||
lines={[ | ||
{ | ||
text: `sudo curl https://apt.releases.teleport.dev/gpg \\\n-o /usr/share/keyrings/teleport-archive-keyring.asc`, | ||
comment: `Download Teleport's PGP public key`, | ||
}, | ||
{ | ||
text: 'sudo DEBUG=1 teleport start -c cfg-all -d', | ||
}, | ||
{ | ||
text: | ||
`echo "deb [signed-by=/usr/share/keyrings/teleport-archive-keyring.asc] \\\n` + | ||
`https://apt.releases.teleport.dev/stable/v10" \\\n` + | ||
`| sudo tee /etc/apt/sources.list.d/teleport.list > /dev/null`, | ||
comment: | ||
`Add the Teleport APT repository for v10. You'll need to update this` + | ||
`\nfile for each major release of Teleport.\n` + | ||
`Note: if using a fork of Debian or Ubuntu you may need to use '$ID_LIKE'\n` + | ||
`and the codename your distro was forked from instead of '$ID' and '$VERSION_CODENAME'.\n`, | ||
}, | ||
]} | ||
/> | ||
); | ||
}; | ||
|
||
export const BashSingle = () => { | ||
return ( | ||
<Component | ||
lines={[ | ||
{ | ||
text: `sudo tctl -c cfg-all users add --roles=access,editor george_washington`, | ||
}, | ||
]} | ||
/> | ||
); | ||
}; | ||
|
||
export const BashSingleWithComment = () => { | ||
return ( | ||
<Component | ||
lines={[ | ||
{ | ||
text: `sudo tctl -c cfg-all users add --roles=access,editor george_washington`, | ||
comment: `Add the Teleport API repository for v10. You'll need to update this.`, | ||
}, | ||
]} | ||
/> | ||
); | ||
}; | ||
|
||
export const NonBash = () => { | ||
return ( | ||
<Component | ||
lines={[ | ||
{ | ||
text: 'some -c text to be copied and it is super long to test scrolling', | ||
}, | ||
]} | ||
bash={false} | ||
/> | ||
); | ||
}; |
69 changes: 69 additions & 0 deletions
69
web/packages/teleport/src/components/TextSelectCopy/TextSelectCopyMulti.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,69 @@ | ||
/** | ||
* Copyright 2022 Gravitational, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import React from 'react'; | ||
|
||
import { render, screen, userEvent } from 'design/utils/testing'; | ||
|
||
import { TextSelectCopyMulti } from './TextSelectCopyMulti'; | ||
|
||
jest.useFakeTimers(); | ||
|
||
test('changing of icon when button is clicked', async () => { | ||
const user = userEvent.setup({ delay: null }); | ||
render(<TextSelectCopyMulti lines={[{ text: 'some text to copy' }]} />); | ||
|
||
// Init button states. | ||
expect(screen.queryByTestId('btn-copy')).toBeVisible(); | ||
expect(screen.queryByTestId('btn-check')).not.toBeVisible(); | ||
|
||
// Clicking copy button should change the button icon to "check". | ||
await user.click(screen.getByTestId('btn-copy')); | ||
expect(screen.getByTestId('btn-check')).toBeVisible(); | ||
expect(screen.getByTestId('btn-copy')).not.toBeVisible(); | ||
|
||
const clipboardText = await navigator.clipboard.readText(); | ||
expect(clipboardText).toBe('some text to copy'); | ||
|
||
// After set time out, the buttons should return to its initial state. | ||
jest.runAllTimers(); | ||
expect(screen.queryByTestId('btn-copy')).toBeVisible(); | ||
expect(screen.queryByTestId('btn-check')).not.toBeVisible(); | ||
}); | ||
|
||
test('correct copying of texts', async () => { | ||
const user = userEvent.setup({ delay: null }); | ||
render( | ||
<TextSelectCopyMulti | ||
lines={[ | ||
{ text: 'text to copy1', comment: 'first comment' }, | ||
{ text: 'text to copy2', comment: 'second comment' }, | ||
]} | ||
/> | ||
); | ||
|
||
// Test copying of each text. | ||
const btns = screen.queryAllByRole('button'); | ||
expect(btns).toHaveLength(2); | ||
|
||
await user.click(btns[1]); | ||
let clipboardText = await navigator.clipboard.readText(); | ||
expect(clipboardText).toBe('text to copy2'); | ||
|
||
await user.click(btns[0]); | ||
clipboardText = await navigator.clipboard.readText(); | ||
expect(clipboardText).toBe('text to copy1'); | ||
}); |
Oops, something went wrong.