Skip to content

Commit

Permalink
Add more test cases
Browse files Browse the repository at this point in the history
Signed-off-by: Lin Wang <[email protected]>
  • Loading branch information
wanglam committed Aug 14, 2024
1 parent d63b019 commit 6330a33
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
*/

import React from 'react';
import { render } from '@testing-library/react';
import { WorkspaceForm } from './workspace_form';
import { fireEvent, render } from '@testing-library/react';
import { coreMock } from '../../../../../core/public/mocks';
import { DataSourceManagementPluginSetup } from '../../../../../plugins/data_source_management/public';
import { createMockedRegisteredUseCases } from '../../mocks';
import { WorkspaceOperationType } from './constants';
import { WorkspaceForm } from './workspace_form';

const mockCoreStart = coreMock.createStart();

Expand Down Expand Up @@ -61,9 +61,30 @@ describe('WorkspaceForm', () => {

expect(queryByText('Associate data source')).not.toBeInTheDocument();
});

it('should not display data source panel when data source is disabled', () => {
const { queryByText } = setup(true, undefined);

expect(queryByText('Associate data source')).not.toBeInTheDocument();
});

it('should automatic update workspace name after use case changed', () => {
const { getByTestId } = setup(false, mockDataSourceManagementSetup);

const nameInput = getByTestId('workspaceForm-workspaceDetails-nameInputText');
expect(nameInput).toHaveValue('');
fireEvent.click(getByTestId('workspaceUseCase-observability'));
expect(nameInput).toHaveValue('Observability');
});

it('should not automatic update workspace name after manual input', () => {
const { getByTestId } = setup(false, mockDataSourceManagementSetup);

const nameInput = getByTestId('workspaceForm-workspaceDetails-nameInputText');
fireEvent.input(nameInput, {
target: { value: 'test workspace name' },
});
fireEvent.click(getByTestId('workspaceUseCase-observability'));
expect(nameInput).toHaveValue('test workspace name');
});
});
40 changes: 38 additions & 2 deletions src/plugins/workspace/public/services/use_case_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,26 @@
import { BehaviorSubject } from 'rxjs';
import { first } from 'rxjs/operators';
import { chromeServiceMock } from '../../../../core/public/mocks';
import { DEFAULT_NAV_GROUPS, NavGroupType } from '../../../../core/public';
import {
ALL_USE_CASE_ID,
DEFAULT_NAV_GROUPS,
NavGroupItemInMap,
NavGroupType,
} from '../../../../core/public';
import { UseCaseService } from './use_case_service';

const mockNavGroupsMap = {
system: {
id: 'system',
title: 'System',
description: 'System use case',
navLinks: [],
type: NavGroupType.SYSTEM,
},
search: {
id: 'search',
title: 'Search',
description: 'Search use case',
navLinks: [{ id: 'searchRelevance', title: 'Search Relevance' }],
order: 2000,
},
Expand All @@ -35,7 +42,7 @@ const setupUseCaseStart = (options?: { navGroupEnabled?: boolean }) => {
const workspaceConfigurableApps$ = new BehaviorSubject([
{ id: 'searchRelevance', title: 'Search Relevance' },
]);
const navGroupsMap$ = new BehaviorSubject(mockNavGroupsMap);
const navGroupsMap$ = new BehaviorSubject<Record<string, NavGroupItemInMap>>(mockNavGroupsMap);
const useCase = new UseCaseService();

chrome.navGroup.getNavGroupEnabled.mockImplementation(() => options?.navGroupEnabled ?? true);
Expand Down Expand Up @@ -123,5 +130,34 @@ describe('UseCaseService', () => {
});
expect(fn).toHaveBeenCalledTimes(2);
});
it('should move all use case to the last one', async () => {
const { useCaseStart, navGroupsMap$ } = setupUseCaseStart();

navGroupsMap$.next({
...mockNavGroupsMap,
[ALL_USE_CASE_ID]: { ...DEFAULT_NAV_GROUPS.all, navLinks: [], order: -1 },
});
let useCases = await useCaseStart.getRegisteredUseCases$().pipe(first()).toPromise();

expect(useCases[useCases.length - 1]).toEqual(
expect.objectContaining({
id: ALL_USE_CASE_ID,
systematic: true,
})
);

navGroupsMap$.next({
[ALL_USE_CASE_ID]: { ...DEFAULT_NAV_GROUPS.all, navLinks: [], order: 1500 },
...mockNavGroupsMap,
});
useCases = await useCaseStart.getRegisteredUseCases$().pipe(first()).toPromise();

expect(useCases[useCases.length - 1]).toEqual(
expect.objectContaining({
id: ALL_USE_CASE_ID,
systematic: true,
})
);
});
});
});

0 comments on commit 6330a33

Please sign in to comment.