Skip to content

Commit

Permalink
add util and copy unit test
Browse files Browse the repository at this point in the history
Signed-off-by: yubonluo <yubonluo@amazon.com>
  • Loading branch information
yubonluo committed Mar 26, 2024
1 parent 2be9d0c commit 49c6134
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { httpServiceMock } from '../../../../core/public/mocks';
import { duplicateSavedObjects } from './duplicate_saved_objects';

describe('copy saved objects', () => {
it('make http call with body provided', async () => {
const httpClient = httpServiceMock.createStartContract();
const objects = [
{ type: 'dashboard', id: '1' },
{ type: 'visualization', id: '2' },
];
const includeReferencesDeep = true;
const targetWorkspace = '1';
await duplicateSavedObjects(httpClient, objects, includeReferencesDeep, targetWorkspace);
expect(httpClient.post).toMatchInlineSnapshot(
'',
`
[MockFunction] {
"calls": Array [
Array [
"/api/saved_objects/_copy",
Object {
"body": "{\\"objects\\":[{\\"type\\":\\"dashboard\\",\\"id\\":\\"1\\"},{\\"type\\":\\"visualization\\",\\"id\\":\\"2\\"}],\\"includeReferencesDeep\\":true,\\"targetWorkspace\\":\\"1\\"}",
},
],
],
"results": Array [
Object {
"type": "return",
"value": undefined,
},
],
}
`
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { WorkspaceAttribute, WorkspaceObject, WorkspacesStart } from 'opensearch-dashboards/public';
import {
WorkspaceOption,
capitalizeFirstLetter,
getTargetWorkspacesOptions,
workspaceToOption,
} from './utils';
import { BehaviorSubject } from 'rxjs';

describe('duplicate mode utils', () => {
it('should covert workspace to option', () => {
const workspace: WorkspaceAttribute = {
id: '1',
name: 'Workspace 1',
};
const workspaceOption: WorkspaceOption = workspaceToOption(workspace);
expect(workspaceOption.label).toBe(workspace.name);
expect(workspaceOption.key).toBe(workspace.id);
expect(workspaceOption.value).toBe(workspace);
});

it('should add suffix when workspace is current workspace', () => {
const workspace: WorkspaceAttribute = {
id: '1',
name: 'Workspace 1',
};
const workspaceOption: WorkspaceOption = workspaceToOption(workspace, '1');
expect(workspaceOption.label).toBe('Workspace 1 (current)');
expect(workspaceOption.key).toBe(workspace.id);
expect(workspaceOption.value).toBe(workspace);
});

it('should get target workspace options', () => {
const workspaces: WorkspacesStart = {
currentWorkspaceId$: new BehaviorSubject<string>('1'),
currentWorkspace$: new BehaviorSubject<WorkspaceObject | null>({
id: '1',
name: 'Workspace 1',
}),
workspaceList$: new BehaviorSubject<WorkspaceObject[]>([
{ id: '1', name: 'Workspace 1' },
{ id: '2', name: 'Workspace 2', libraryReadonly: false },
{ id: '3', name: 'Workspace 3', libraryReadonly: true },
]),
initialized$: new BehaviorSubject<boolean>(true),
};
const optionContainCurrent: WorkspaceOption[] = getTargetWorkspacesOptions(workspaces, '1');
expect(optionContainCurrent.length).toBe(1);
expect(optionContainCurrent[0].key).toBe('2');
expect(optionContainCurrent[0].label).toBe('Workspace 2');

const workspaceOption: WorkspaceOption[] = getTargetWorkspacesOptions(workspaces);
expect(workspaceOption.length).toBe(2);
});

it('should capitalize first letter', () => {
const workspaceName: string = 'workspace';
const capitalizedName: string = capitalizeFirstLetter(workspaceName);
expect(capitalizedName).toBe('Workspace');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { WorkspaceAttribute, WorkspacesStart } from 'opensearch-dashboards/publi

export type WorkspaceOption = EuiComboBoxOptionOption<WorkspaceAttribute>;

// Convert workspace to option which can be displayed in the drop-down box.
export function workspaceToOption(
workspace: WorkspaceAttribute,
currentWorkspaceId?: string
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/workspace/public/workspace_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ export class WorkspaceClient {
libraryReadonly: !workspaceIdsWithWritePermission.includes(workspace.id),
}));
this.workspaces.workspaceList$.next(workspaces);
} else {
this.workspaces.workspaceList$.next([]);
}
}
}
Expand Down

0 comments on commit 49c6134

Please sign in to comment.