diff --git a/x-pack/plugins/triggers_actions_ui/public/application/components/update_api_key_modal_confirmation.test.tsx b/x-pack/plugins/triggers_actions_ui/public/application/components/update_api_key_modal_confirmation.test.tsx new file mode 100644 index 0000000000000..2c1b10fca0c6c --- /dev/null +++ b/x-pack/plugins/triggers_actions_ui/public/application/components/update_api_key_modal_confirmation.test.tsx @@ -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 }) => ( + {children} +); + +const renderWithProviders = (ui: any) => { + return render(ui, { wrapper: Providers }); +}; + +jest.mock('../../common/lib/kibana'); +const useKibanaMock = useKibana as jest.Mocked; + +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( + + ); + + expect( + await screen.findByText('You will not be able to recover the old API key') + ).toBeInTheDocument(); + }); + + it('Cancel modal updates Api Key', async () => { + renderWithProviders( + + ); + + fireEvent.click(await screen.findByText('Cancel')); + expect(onCancel).toHaveBeenCalled(); + }); + + it('Update an Api Key', async () => { + apiUpdateApiKeyCall.mockResolvedValue({}); + renderWithProviders( + + ); + + 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( + + ); + + 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", + }, + ] + `); + }); + }); +}); diff --git a/x-pack/plugins/triggers_actions_ui/public/application/sections/rules_list/components/rules_list.test.tsx b/x-pack/plugins/triggers_actions_ui/public/application/sections/rules_list/components/rules_list.test.tsx index f70437b33c451..2775b200cb6c6 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/sections/rules_list/components/rules_list.test.tsx +++ b/x-pack/plugins/triggers_actions_ui/public/application/sections/rules_list/components/rules_list.test.tsx @@ -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(); @@ -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(); @@ -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.')); - 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(); - - 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' }) - ); - 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(); }); });