Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Lens] rewrite some tests to testing library #187637

Merged
merged 5 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,24 @@
import React from 'react';

import { CoreStart } from '@kbn/core/public';
import { RedirectAppLinks } from '@kbn/shared-ux-link-redirect-app';
import { mountWithIntl } from '@kbn/test-jest-helpers';
import { shallow } from 'enzyme';
import { Visualization } from '..';
import { DataViewsState } from '../state_management';
import { Datasource, UserMessage } from '../types';
import {
UserMessageGetterProps,
filterAndSortUserMessages,
getApplicationUserMessages,
} from './get_application_user_messages';
import { cleanup, render, screen } from '@testing-library/react';
import { I18nProvider } from '@kbn/i18n-react';

jest.mock('@kbn/shared-ux-link-redirect-app', () => {
const original = jest.requireActual('@kbn/shared-ux-link-redirect-app');
return {
...original,
RedirectAppLinks: () => <a>RedirectAppLinks</a>,
};
});

describe('application-level user messages', () => {
it('should generate error if vis type is not provided', () => {
Expand Down Expand Up @@ -139,70 +147,57 @@ describe('application-level user messages', () => {
return core;
}

const irrelevantProps = {
dataViews: {} as DataViewsState,
visualization: {} as Visualization,
visualizationState: { activeId: 'foo', state: {} },
const renderApplicationUserMessages = (propsOverrides?: Partial<UserMessageGetterProps>) => {
const props = {
visualizationType: '123',
activeDatasource: {
checkIntegrity: jest.fn(() => ['missing_pattern']),
} as unknown as Datasource,
activeDatasourceState: { isLoading: false, state: {} },
// user can go to management, but indexPatterns management is not accessible
core: createCoreStartWithPermissions({
navLinks: { management: true },
management: { kibana: { indexPatterns: false } },
}),
// irrelevantProps
dataViews: {} as DataViewsState,
visualization: {} as Visualization,
visualizationState: { activeId: 'foo', state: {} },
};
const rtlRender = render(
<I18nProvider>
{
getApplicationUserMessages({
...props,
...propsOverrides,
})[0].longMessage
}
</I18nProvider>
);
return rtlRender;
};

it('generates error if missing an index pattern', () => {
expect(
getApplicationUserMessages({
visualizationType: '123',
activeDatasource: {
checkIntegrity: jest.fn(() => ['missing_pattern']),
} as unknown as Datasource,
activeDatasourceState: { isLoading: false, state: {} },
core: createCoreStartWithPermissions(),
...irrelevantProps,
})
).toMatchSnapshot();
renderApplicationUserMessages({
core: createCoreStartWithPermissions(),
});

expect(screen.queryByText('RedirectAppLinks')).toBeInTheDocument();
expect(screen.getByTestId('missing-refs-failure')).toHaveTextContent('Data view not found');
});

it('doesnt show a recreate link if user has no access', () => {
expect(
mountWithIntl(
<div>
{
getApplicationUserMessages({
visualizationType: '123',
activeDatasource: {
checkIntegrity: jest.fn(() => ['missing_pattern']),
} as unknown as Datasource,
activeDatasourceState: { isLoading: false, state: {} },
// user can go to management, but indexPatterns management is not accessible
core: createCoreStartWithPermissions({
navLinks: { management: true },
management: { kibana: { indexPatterns: false } },
}),
...irrelevantProps,
})[0].longMessage
}
</div>
).exists(RedirectAppLinks)
).toBeFalsy();
renderApplicationUserMessages();
expect(screen.queryByText('RedirectAppLinks')).not.toBeInTheDocument();
cleanup();
renderApplicationUserMessages({
core: createCoreStartWithPermissions({
navLinks: { management: false },
management: { kibana: { indexPatterns: true } },
}),
});

expect(
shallow(
<div>
{
getApplicationUserMessages({
visualizationType: '123',
activeDatasource: {
checkIntegrity: jest.fn(() => ['missing_pattern']),
} as unknown as Datasource,
activeDatasourceState: { isLoading: false, state: {} },
// user can't go to management at all
core: createCoreStartWithPermissions({
navLinks: { management: false },
management: { kibana: { indexPatterns: true } },
}),
...irrelevantProps,
})[0].longMessage
}
</div>
).exists(RedirectAppLinks)
).toBeFalsy();
expect(screen.queryByText('RedirectAppLinks')).not.toBeInTheDocument();
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ import {
EDITOR_UNKNOWN_VIS_TYPE,
} from '../user_messages_ids';

export interface UserMessageGetterProps {
visualizationType: string | null | undefined;
visualization: Visualization | undefined;
visualizationState: VisualizationState | undefined;
activeDatasource: Datasource | null | undefined;
activeDatasourceState: { isLoading: boolean; state: unknown } | null;
dataViews: DataViewsState;
core: CoreStart;
}

/**
* Provides a place to register general user messages that don't belong in the datasource or visualization objects
*/
Expand All @@ -47,15 +57,7 @@ export const getApplicationUserMessages = ({
activeDatasourceState,
dataViews,
core,
}: {
visualizationType: string | null | undefined;
visualization: Visualization | undefined;
visualizationState: VisualizationState | undefined;
activeDatasource: Datasource | null | undefined;
activeDatasourceState: { isLoading: boolean; state: unknown } | null;
dataViews: DataViewsState;
core: CoreStart;
}): UserMessage[] => {
}: UserMessageGetterProps): UserMessage[] => {
const messages: UserMessage[] = [];

if (!visualizationType) {
Expand Down
Loading