Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import * as React from 'react';
import { __IntlProvider as IntlProvider } from '@kbn/i18n-react';
import { IToasts } from '@kbn/core/public';
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import { UpdateApiKeyModalConfirmation } from './update_api_key_modal_confirmation';
import { useKibana } from '../../common/lib/kibana';

const Providers = ({ children }: { children: any }) => (
<IntlProvider locale="en">{children}</IntlProvider>
);

const renderWithProviders = (ui: any) => {
return render(ui, { wrapper: Providers });
};

jest.mock('../../common/lib/kibana');
const useKibanaMock = useKibana as jest.Mocked<typeof useKibana>;

describe('Update Api Key', () => {
const onCancel = jest.fn();
const apiUpdateApiKeyCall = jest.fn();
const setIsLoadingState = jest.fn();
const onUpdated = jest.fn();
const onSearchPopulate = jest.fn();

const addSuccess = jest.fn();
const addError = jest.fn();

beforeAll(() => {
useKibanaMock().services.notifications.toasts = {
addSuccess,
addError,
} as unknown as IToasts;
});

afterEach(() => {
jest.clearAllMocks();
});

it('Render modal updates Api Key', async () => {
renderWithProviders(
<UpdateApiKeyModalConfirmation
onCancel={onCancel}
idsToUpdate={['2']}
apiUpdateApiKeyCall={apiUpdateApiKeyCall}
setIsLoadingState={setIsLoadingState}
onUpdated={onUpdated}
onSearchPopulate={onSearchPopulate}
/>
);

expect(
await screen.findByText('You will not be able to recover the old API key')
).toBeInTheDocument();
});

it('Cancel modal updates Api Key', async () => {
renderWithProviders(
<UpdateApiKeyModalConfirmation
onCancel={onCancel}
idsToUpdate={['2']}
apiUpdateApiKeyCall={apiUpdateApiKeyCall}
setIsLoadingState={setIsLoadingState}
onUpdated={onUpdated}
onSearchPopulate={onSearchPopulate}
/>
);

fireEvent.click(await screen.findByText('Cancel'));
expect(onCancel).toHaveBeenCalled();
});

it('Update an Api Key', async () => {
apiUpdateApiKeyCall.mockResolvedValue({});
renderWithProviders(
<UpdateApiKeyModalConfirmation
onCancel={onCancel}
idsToUpdate={['2']}
apiUpdateApiKeyCall={apiUpdateApiKeyCall}
setIsLoadingState={setIsLoadingState}
onUpdated={onUpdated}
onSearchPopulate={onSearchPopulate}
/>
);

fireEvent.click(await screen.findByText('Update'));
expect(setIsLoadingState).toBeCalledTimes(1);
expect(apiUpdateApiKeyCall).toHaveBeenLastCalledWith(expect.objectContaining({ ids: ['2'] }));
await waitFor(() => {
expect(setIsLoadingState).toBeCalledTimes(2);
expect(onUpdated).toHaveBeenCalled();
});
});

it('Failed to update an Api Key', async () => {
apiUpdateApiKeyCall.mockRejectedValue(500);
renderWithProviders(
<UpdateApiKeyModalConfirmation
onCancel={onCancel}
idsToUpdate={['2']}
apiUpdateApiKeyCall={apiUpdateApiKeyCall}
setIsLoadingState={setIsLoadingState}
onUpdated={onUpdated}
onSearchPopulate={onSearchPopulate}
/>
);

fireEvent.click(await screen.findByText('Update'));
expect(setIsLoadingState).toBeCalledTimes(1);
expect(apiUpdateApiKeyCall).toHaveBeenLastCalledWith(expect.objectContaining({ ids: ['2'] }));
await waitFor(() => {
expect(setIsLoadingState).toBeCalledTimes(2);
expect(addError).toHaveBeenCalled();
expect(addError.mock.calls[0]).toMatchInlineSnapshot(`
Array [
500,
Object {
"title": "Failed to update the API key",
},
]
`);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,7 @@ const renderWithProviders = (ui: any) => {
return render(ui, { wrapper: AllTheProviders });
};

// FLAKY: https://github.com/elastic/kibana/issues/134922
// FLAKY: https://github.com/elastic/kibana/issues/134923

describe.skip('Update Api Key', () => {
describe('Update Api Key', () => {
const addSuccess = jest.fn();
const addError = jest.fn();

Expand Down Expand Up @@ -177,7 +174,7 @@ describe.skip('Update Api Key', () => {
cleanup();
});

it('Updates the Api Key successfully', async () => {
it('Have the option to update API key', async () => {
bulkUpdateAPIKey.mockResolvedValueOnce({ errors: [], total: 1, rules: [], skipped: [] });
renderWithProviders(<RulesList />);

Expand All @@ -186,45 +183,7 @@ describe.skip('Update Api Key', () => {
fireEvent.click((await screen.findAllByTestId('selectActionButton'))[1]);
expect(screen.getByTestId('collapsedActionPanel')).toBeInTheDocument();

fireEvent.click(await screen.findByText('Update API key'));
expect(screen.getByText('You will not be able to recover the old API key')).toBeInTheDocument();

fireEvent.click(await screen.findByText('Cancel'));
expect(
screen.queryByText('You will not be able to recover the old API key')
).not.toBeInTheDocument();

fireEvent.click((await screen.findAllByTestId('selectActionButton'))[1]);
expect(screen.getByTestId('collapsedActionPanel')).toBeInTheDocument();

fireEvent.click(await screen.findByText('Update API key'));

fireEvent.click(await screen.findByTestId('confirmModalConfirmButton'));
await waitFor(() => expect(addSuccess).toHaveBeenCalledWith('Updated API key for 1 rule.'));
Comment thread
XavierM marked this conversation as resolved.
expect(bulkUpdateAPIKey).toHaveBeenCalledWith(expect.objectContaining({ ids: ['2'] }));
expect(screen.queryByText("You can't recover the old API key")).not.toBeInTheDocument();
});

it('Update API key fails', async () => {
bulkUpdateAPIKey.mockRejectedValueOnce(500);
renderWithProviders(<RulesList />);

expect(await screen.findByText('test rule ok')).toBeInTheDocument();

fireEvent.click((await screen.findAllByTestId('selectActionButton'))[1]);
expect(screen.getByTestId('collapsedActionPanel')).toBeInTheDocument();

fireEvent.click(await screen.findByText('Update API key'));
expect(screen.getByText('You will not be able to recover the old API key')).toBeInTheDocument();

fireEvent.click(await screen.findByText('Update'));
await waitFor(() =>
expect(addError).toHaveBeenCalledWith(500, { title: 'Failed to update the API key' })
Comment thread
XavierM marked this conversation as resolved.
);
expect(bulkUpdateAPIKey).toHaveBeenCalledWith(expect.objectContaining({ ids: ['2'] }));
expect(
screen.queryByText('You will not be able to recover the old API key')
).not.toBeInTheDocument();
expect(screen.queryByText('Update API key')).toBeInTheDocument();
});
});

Expand Down