Fix <ComponentTabs> to display only the selected tab, not the other way around

Signed-off-by: Oliver Sand <oliver.sand@sda-se.com>
This commit is contained in:
Oliver Sand
2021-10-11 11:19:34 +02:00
parent 9faf634497
commit 87b2d5ad88
3 changed files with 85 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-home': patch
---
Fix `<ComponentTabs>` to display only the selected tab, not the other way around.
@@ -0,0 +1,76 @@
/*
* Copyright 2021 The Backstage Authors
*
* 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 { render } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import React from 'react';
import { ComponentTabs } from './ComponentTabs';
describe('<ComponentTabs>', () => {
test('should render tabs without exploding', () => {
const { getByText } = render(
<ComponentTabs
title="Random Jokes"
tabs={[
{
label: 'TabA',
Component: () => <>ContentA</>,
},
{
label: 'TabB',
Component: () => <>ContentB</>,
},
]}
/>,
);
expect(getByText('TabA')).toBeInTheDocument();
expect(getByText('TabB')).toBeInTheDocument();
expect(getByText('ContentA')).toBeInTheDocument();
expect(getByText('ContentB')).toHaveStyle({
display: 'none',
});
});
test('should switch tab on click', () => {
const { getByText } = render(
<ComponentTabs
title="Random Jokes"
tabs={[
{
label: 'TabA',
Component: () => <>ContentA</>,
},
{
label: 'TabB',
Component: () => <>ContentB</>,
},
]}
/>,
);
expect(getByText('ContentB')).toHaveStyle({
display: 'none',
});
userEvent.click(getByText('TabB'));
expect(getByText('ContentA')).toHaveStyle({
display: 'none',
});
});
});
@@ -44,7 +44,10 @@ export const ComponentTabs = ({
))}
</Tabs>
{tabs.map(({ Component }, idx) => (
<div {...(idx === value ? { style: { display: 'none' } } : {})}>
<div
key={idx}
{...(idx !== value ? { style: { display: 'none' } } : {})}
>
<Component />
</div>
))}