Skip to content

Commit

Permalink
[Spaces Management] Ensure current badge can only appear for single e…
Browse files Browse the repository at this point in the history
…ntry (elastic#193195)

## Summary

Closes elastic#192811

### Checklist

Delete any items that are not applicable to this PR.

- [X] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or
  • Loading branch information
tsullivan committed Sep 18, 2024
1 parent e3f3c68 commit 5bf4501
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { mountWithIntl, shallowWithIntl } from '@kbn/test-jest-helpers';

import { SpacesGridPage } from './spaces_grid_page';
import { SpaceAvatarInternal } from '../../space_avatar/space_avatar_internal';
import type { SpacesManager } from '../../spaces_manager';
import { spacesManagerMock } from '../../spaces_manager/mocks';

const spaces = [
Expand Down Expand Up @@ -73,7 +72,7 @@ describe('SpacesGridPage', () => {

const wrapper = shallowWithIntl(
<SpacesGridPage
spacesManager={spacesManager as unknown as SpacesManager}
spacesManager={spacesManager}
getFeatures={featuresStart.getFeatures}
notifications={notificationServiceMock.createStartContract()}
getUrlForApp={getUrlForApp}
Expand Down Expand Up @@ -133,7 +132,7 @@ describe('SpacesGridPage', () => {

const wrapper = shallowWithIntl(
<SpacesGridPage
spacesManager={spacesManager as unknown as SpacesManager}
spacesManager={spacesManager}
getFeatures={featuresStart.getFeatures}
notifications={notificationServiceMock.createStartContract()}
getUrlForApp={getUrlForApp}
Expand Down Expand Up @@ -163,13 +162,21 @@ describe('SpacesGridPage', () => {
});

it('renders a "current" badge for the current space', async () => {
spacesManager.getActiveSpace.mockResolvedValue(spaces[2]);
const current = await spacesManager.getActiveSpace();
expect(current.id).toBe('custom-2');
const spacesWithCurrent = [
{ id: 'default', name: 'Default', disabledFeatures: [], _reserved: true },
{ id: 'test-1', name: 'Test', disabledFeatures: [] },
{ id: 'test-2', name: 'Test', disabledFeatures: [] },
];
const spacesManagerWithCurrent = spacesManagerMock.create();
spacesManagerWithCurrent.getSpaces = jest.fn().mockResolvedValue(spacesWithCurrent);
spacesManagerWithCurrent.getActiveSpace.mockResolvedValue(spacesWithCurrent[2]);

const current = await spacesManagerWithCurrent.getActiveSpace();
expect(current.id).toBe('test-2');

const wrapper = mountWithIntl(
<SpacesGridPage
spacesManager={spacesManager as unknown as SpacesManager}
spacesManager={spacesManagerWithCurrent}
getFeatures={featuresStart.getFeatures}
notifications={notificationServiceMock.createStartContract()}
getUrlForApp={getUrlForApp}
Expand All @@ -189,10 +196,20 @@ describe('SpacesGridPage', () => {
await act(async () => {});
wrapper.update();

const activeRow = wrapper.find('[data-test-subj="spacesListTableRow-custom-2"]');
const activeRow = wrapper.find('[data-test-subj="spacesListTableRow-test-2"]');
const nameCell = activeRow.find('[data-test-subj="spacesListTableRowNameCell"]');
const activeBadge = nameCell.find('EuiBadge');
expect(activeBadge.text()).toBe('current');

// ensure that current badge appears only once
const currentBadges = wrapper.findWhere((node) => {
return (
node.type() === 'span' &&
node.prop('data-test-subj') &&
node.prop('data-test-subj').includes('spacesListCurrentBadge')
);
});
expect(currentBadges.length).toBe(1);
});

it('renders a non-clickable "switch" action for the current space', async () => {
Expand All @@ -202,7 +219,7 @@ describe('SpacesGridPage', () => {

const wrapper = mountWithIntl(
<SpacesGridPage
spacesManager={spacesManager as unknown as SpacesManager}
spacesManager={spacesManager}
getFeatures={featuresStart.getFeatures}
notifications={notificationServiceMock.createStartContract()}
getUrlForApp={getUrlForApp}
Expand Down Expand Up @@ -234,7 +251,7 @@ describe('SpacesGridPage', () => {

const wrapper = mountWithIntl(
<SpacesGridPage
spacesManager={spacesManager as unknown as SpacesManager}
spacesManager={spacesManager}
getFeatures={featuresStart.getFeatures}
notifications={notificationServiceMock.createStartContract()}
getUrlForApp={getUrlForApp}
Expand Down Expand Up @@ -265,7 +282,7 @@ describe('SpacesGridPage', () => {

const wrapper = mountWithIntl(
<SpacesGridPage
spacesManager={spacesManager as unknown as SpacesManager}
spacesManager={spacesManager}
getFeatures={featuresStart.getFeatures}
notifications={notificationServiceMock.createStartContract()}
getUrlForApp={getUrlForApp}
Expand Down Expand Up @@ -295,7 +312,7 @@ describe('SpacesGridPage', () => {

const wrapper = mountWithIntl(
<SpacesGridPage
spacesManager={spacesManager as unknown as SpacesManager}
spacesManager={spacesManager}
getFeatures={featuresStart.getFeatures}
notifications={notificationServiceMock.createStartContract()}
getUrlForApp={getUrlForApp}
Expand Down Expand Up @@ -331,7 +348,7 @@ describe('SpacesGridPage', () => {

const wrapper = shallowWithIntl(
<SpacesGridPage
spacesManager={spacesManager as unknown as SpacesManager}
spacesManager={spacesManager}
getFeatures={featuresStart.getFeatures}
notifications={notifications}
getUrlForApp={getUrlForApp}
Expand Down Expand Up @@ -367,7 +384,7 @@ describe('SpacesGridPage', () => {

const wrapper = shallowWithIntl(
<SpacesGridPage
spacesManager={spacesManager as unknown as SpacesManager}
spacesManager={spacesManager}
getFeatures={() => Promise.reject(error)}
notifications={notifications}
getUrlForApp={getUrlForApp}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ export class SpacesGridPage extends Component<Props, State> {
{value}
</EuiLink>
</EuiFlexItem>
{this.state.activeSpace?.name === rowRecord.name && (
{this.state.activeSpace?.id === rowRecord.id && (
<EuiFlexItem grow={false}>
<EuiBadge color="primary" data-test-subj={`spacesListCurrentBadge-${rowRecord.id}`}>
{i18n.translate('xpack.spaces.management.spacesGridPage.currentSpaceMarkerText', {
Expand Down

0 comments on commit 5bf4501

Please sign in to comment.