Skip to content

Commit

Permalink
optimize the test code and add newHomePage check
Browse files Browse the repository at this point in the history
Signed-off-by: yubonluo <[email protected]>
  • Loading branch information
yubonluo committed Aug 23, 2024
1 parent 7f95ba1 commit 63b84a4
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,8 @@ const overview = i18n.translate('workspace.detail.overview', {
});

function getOwners(currentWorkspace: WorkspaceAttributeWithPermission) {
if (currentWorkspace.permissions) {
const { groups = [], users = [] } = currentWorkspace.permissions.write;
return [...groups, ...users];
}
return [];
const { groups = [], users = [] } = currentWorkspace?.permissions?.write || {};
return [...groups, ...users];
}

interface WorkspaceDetailPanelProps {
Expand Down
9 changes: 6 additions & 3 deletions src/plugins/workspace/public/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import { workspaceClientMock, WorkspaceClientMock } from './workspace_client.moc
import { WorkspacePlugin, WorkspacePluginStartDeps } from './plugin';
import { contentManagementPluginMocks } from '../../content_management/public';

// Expect 6 app registrations: create, fatal error, detail, initial, navigation, and list apps.
const registrationAppNumber = 6;

describe('Workspace plugin', () => {
const mockDependencies: WorkspacePluginStartDeps = {
contentManagement: contentManagementPluginMocks.createStartContract(),
Expand All @@ -40,7 +43,7 @@ describe('Workspace plugin', () => {
savedObjectsManagement: savedObjectManagementSetupMock,
management: managementPluginMock.createSetupContract(),
});
expect(setupMock.application.register).toBeCalledTimes(6);
expect(setupMock.application.register).toBeCalledTimes(registrationAppNumber);
expect(WorkspaceClientMock).toBeCalledTimes(1);
expect(savedObjectManagementSetupMock.columns.register).toBeCalledTimes(1);
});
Expand All @@ -53,7 +56,7 @@ describe('Workspace plugin', () => {
workspacePlugin.start(coreStart, mockDependencies);
coreStart.workspaces.currentWorkspaceId$.next('foo');
expect(coreStart.savedObjects.client.setCurrentWorkspace).toHaveBeenCalledWith('foo');
expect(setupMock.application.register).toBeCalledTimes(6);
expect(setupMock.application.register).toBeCalledTimes(registrationAppNumber);
expect(WorkspaceClientMock).toBeCalledTimes(1);
expect(workspaceClientMock.enterWorkspace).toBeCalledTimes(0);
});
Expand Down Expand Up @@ -90,7 +93,7 @@ describe('Workspace plugin', () => {
await workspacePlugin.setup(setupMock, {
management: managementPluginMock.createSetupContract(),
});
expect(setupMock.application.register).toBeCalledTimes(6);
expect(setupMock.application.register).toBeCalledTimes(registrationAppNumber);
expect(WorkspaceClientMock).toBeCalledTimes(1);
expect(workspaceClientMock.enterWorkspace).toBeCalledWith('workspaceId');
expect(setupMock.getStartServices).toBeCalledTimes(2);
Expand Down
72 changes: 58 additions & 14 deletions src/plugins/workspace/server/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@
* SPDX-License-Identifier: Apache-2.0
*/

import {
OnPostAuthHandler,
OnPreRoutingHandler,
SavedObjectsClientContract,
} from 'src/core/server';
import { coreMock, httpServerMock, savedObjectsClientMock } from '../../../core/server/mocks';
import { OnPostAuthHandler, OnPreRoutingHandler } from 'src/core/server';
import { coreMock, httpServerMock, uiSettingsServiceMock } from '../../../core/server/mocks';
import { WorkspacePlugin } from './plugin';
import { getWorkspaceState, updateWorkspaceState } from '../../../core/server/utils';
import * as utilsExports from './utils';
Expand Down Expand Up @@ -186,6 +182,24 @@ describe('Workspace server plugin', () => {

describe('#setUpRedirectPage', () => {
const setupMock = coreMock.createSetup();
const uiSettingsMock = uiSettingsServiceMock.createClient();
setupMock.getStartServices.mockResolvedValue([
{
...coreMock.createStart(),
uiSettings: {
asScopedToClient: () => ({
...uiSettingsMock,
get: jest.fn().mockImplementation((key) => {
if (key === 'home:useNewHomePage') {
return Promise.resolve(true);
}
}),
}),
},
},
{},
{},
]);
const initializerContextConfigMock = coreMock.createPluginInitializerContext({
enabled: true,
permission: {
Expand Down Expand Up @@ -281,14 +295,21 @@ describe('Workspace server plugin', () => {
const request = httpServerMock.createOpenSearchDashboardsRequest({
path: '/',
});
const savedObjectsClient: jest.Mocked<SavedObjectsClientContract> = savedObjectsClientMock.create();
const mockUiSettings = {
get: jest.fn().mockResolvedValue('default'),
};
setupMock.getStartServices.mockResolvedValue([
{
uiSettings: { asScopedToClient: () => mockUiSettings },
savedObjects: { getScopedClient: () => savedObjectsClient },
...coreMock.createStart(),
uiSettings: {
asScopedToClient: () => ({
...uiSettingsMock,
get: jest.fn().mockImplementation((key) => {
if (key === 'defaultWorkspace') {
return Promise.resolve('defaultWorkspace');
} else if (key === 'home:useNewHomePage') {
return Promise.resolve('true');
}
}),
}),
},
},
{},
{},
Expand All @@ -302,7 +323,7 @@ describe('Workspace server plugin', () => {
per_page: 100,
page: 1,
workspaces: [
{ id: 'default', name: 'default-workspace' },
{ id: 'defaultWorkspace', name: 'default-workspace' },
{ id: 'workspace-2', name: 'workspace-2' },
],
},
Expand All @@ -312,10 +333,33 @@ describe('Workspace server plugin', () => {
await registerOnPostAuthFn(request, response, toolKitMock);
expect(response.redirected).toBeCalledWith({
headers: {
location: '/mock-server-basepath/w/default/app/workspace_navigation',
location: '/mock-server-basepath/w/defaultWorkspace/app/workspace_navigation',
},
});
});

it('with / request path and home:useNewHomePage is false', async () => {
const request = httpServerMock.createOpenSearchDashboardsRequest({
path: '/',
});
setupMock.getStartServices.mockResolvedValue([
{
...coreMock.createStart(),
uiSettings: {
asScopedToClient: () => ({
...uiSettingsMock,
get: jest.fn().mockResolvedValue(false),
}),
},
},
{},
{},
]);
const toolKitMock = httpServerMock.createToolkit();

await registerOnPostAuthFn(request, response, toolKitMock);
expect(toolKitMock.next).toBeCalledTimes(1);
});
});

it('#start', async () => {
Expand Down
13 changes: 9 additions & 4 deletions src/plugins/workspace/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ export class WorkspacePlugin implements Plugin<WorkspacePluginSetup, WorkspacePl
core.http.registerOnPostAuth(async (request, response, toolkit) => {
const path = request.url.pathname;
if (path === '/') {
const [coreStart] = await core.getStartServices();
const uiSettings = coreStart.uiSettings.asScopedToClient(
coreStart.savedObjects.getScopedClient(request)
);
const useNewHomePage = await uiSettings.get('home:useNewHomePage');
if (!useNewHomePage) {
return toolkit.next();
}

const workspaceListResponse = await this.client?.list(
{ request, logger: this.logger },
{ page: 1, perPage: 100 }
Expand All @@ -130,10 +139,6 @@ export class WorkspacePlugin implements Plugin<WorkspacePluginSetup, WorkspacePl
},
});
}
const [coreStart] = await core.getStartServices();
const uiSettings = coreStart.uiSettings.asScopedToClient(
coreStart.savedObjects.getScopedClient(request)
);
// Temporarily use defaultWorkspace as a placeholder
const defaultWorkspaceId = await uiSettings.get('defaultWorkspace');
const defaultWorkspace = workspaceList.find(
Expand Down

0 comments on commit 63b84a4

Please sign in to comment.