From 1a6cc97842c1622b3663ad67c17ed5eb498a3967 Mon Sep 17 00:00:00 2001 From: mateusz-winiarczyk <mateusz.winiarczyk@appunite.com> Date: Mon, 11 Mar 2024 11:35:58 +0100 Subject: [PATCH] fix(plugin): set plugin active tab in drawer as loaded plugin from url (MIN-295) --- .../LoadPluginFromUrl.component.test.tsx | 33 ++++++++++++++++++- .../hooks/useLoadPluginFromUrl.ts | 9 +++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/components/Map/Drawer/AvailablePluginsDrawer/LoadPluginFromUrl/LoadPluginFromUrl.component.test.tsx b/src/components/Map/Drawer/AvailablePluginsDrawer/LoadPluginFromUrl/LoadPluginFromUrl.component.test.tsx index b74995ed..e6a8094d 100644 --- a/src/components/Map/Drawer/AvailablePluginsDrawer/LoadPluginFromUrl/LoadPluginFromUrl.component.test.tsx +++ b/src/components/Map/Drawer/AvailablePluginsDrawer/LoadPluginFromUrl/LoadPluginFromUrl.component.test.tsx @@ -9,7 +9,7 @@ import { StoreType } from '@/redux/store'; import { mockNetworkResponse } from '@/utils/mockNetworkResponse'; import { InitialStoreState } from '@/utils/testing/getReduxStoreActionsListener'; import { getReduxWrapperWithStore } from '@/utils/testing/getReduxWrapperWithStore'; -import { fireEvent, render, screen } from '@testing-library/react'; +import { fireEvent, render, screen, waitFor } from '@testing-library/react'; import axios, { HttpStatusCode } from 'axios'; import MockAdapter from 'axios-mock-adapter'; import { act } from 'react-dom/test-utils'; @@ -163,5 +163,36 @@ describe('LoadPluginFromUrl - component', () => { const button = screen.getByTestId('load-plugin-button'); expect(button).toBeDisabled(); }); + it('should set plugin active tab in drawer as loaded plugin', async () => { + const pluginUrl = 'http://example.com/plugin.js'; + const pluginScript = `function init() {} init()`; + mockedAxiosClient.onGet(pluginUrl).reply(HttpStatusCode.Ok, pluginScript); + + global.URL.canParse = jest.fn().mockReturnValue(true); + + const { store } = renderComponent(); + + const dispatchSpy = jest.spyOn(store, 'dispatch'); + + const input = screen.getByTestId('load-plugin-input-url'); + expect(input).toBeVisible(); + + act(() => { + fireEvent.change(input, { target: { value: pluginUrl } }); + }); + + const button = screen.getByTestId('load-plugin-button'); + + act(() => { + button.click(); + }); + + await waitFor(() => { + expect(dispatchSpy).toHaveBeenCalledWith({ + payload: 'e008fb2ceb97e3d6139ffe38a1b39d5d', + type: 'plugins/setCurrentDrawerPluginHash', + }); + }); + }); }); }); diff --git a/src/components/Map/Drawer/AvailablePluginsDrawer/LoadPluginFromUrl/hooks/useLoadPluginFromUrl.ts b/src/components/Map/Drawer/AvailablePluginsDrawer/LoadPluginFromUrl/hooks/useLoadPluginFromUrl.ts index 123e220c..93d0f014 100644 --- a/src/components/Map/Drawer/AvailablePluginsDrawer/LoadPluginFromUrl/hooks/useLoadPluginFromUrl.ts +++ b/src/components/Map/Drawer/AvailablePluginsDrawer/LoadPluginFromUrl/hooks/useLoadPluginFromUrl.ts @@ -1,5 +1,7 @@ +import { useAppDispatch } from '@/redux/hooks/useAppDispatch'; import { useAppSelector } from '@/redux/hooks/useAppSelector'; import { activePluginsDataSelector } from '@/redux/plugins/plugins.selectors'; +import { setCurrentDrawerPluginHash } from '@/redux/plugins/plugins.slice'; import { PluginsManager } from '@/services/pluginsManager'; import axios from 'axios'; import { ChangeEvent, useMemo, useState } from 'react'; @@ -15,12 +17,17 @@ export const useLoadPluginFromUrl = (): UseLoadPluginReturnType => { const [pluginUrl, setPluginUrl] = useState(''); const [isLoading, setIsLoading] = useState(false); const activePlugins = useAppSelector(activePluginsDataSelector); + const dispatch = useAppDispatch(); const isPending = useMemo( () => !pluginUrl || isLoading || !URL.canParse(pluginUrl), [pluginUrl, isLoading], ); + const handleSetCurrentDrawerPluginHash = (hash: string): void => { + dispatch(setCurrentDrawerPluginHash(hash)); + }; + const handleLoadPlugin = async (): Promise<void> => { try { setIsLoading(true); @@ -40,6 +47,8 @@ export const useLoadPluginFromUrl = (): UseLoadPluginReturnType => { } setPluginUrl(''); + + handleSetCurrentDrawerPluginHash(hash); } finally { setIsLoading(false); } -- GitLab