From 2dd74127e2586ed020ff5db8a4374bc3b9f2c3c5 Mon Sep 17 00:00:00 2001 From: Dinika Saxena Date: Mon, 16 Sep 2024 11:37:21 +0200 Subject: [PATCH] 4969 // Open resources with tag or revision in self in studios Signed-off-by: Dinika Saxena --- .../containers/DataTableContainer.spec.tsx | 167 ++++++++++++++++++ src/shared/containers/DataTableContainer.tsx | 4 +- 2 files changed, 170 insertions(+), 1 deletion(-) diff --git a/src/shared/containers/DataTableContainer.spec.tsx b/src/shared/containers/DataTableContainer.spec.tsx index d6ae7b3f9..676e71ad3 100644 --- a/src/shared/containers/DataTableContainer.spec.tsx +++ b/src/shared/containers/DataTableContainer.spec.tsx @@ -32,6 +32,7 @@ import configureStore from '../store'; import { cleanup, render, screen, waitFor } from '../../utils/testUtil'; import DataTableContainer from './DataTableContainer'; import { notification } from 'antd'; +import { getMockResource } from '__mocks__/handlers/DataExplorer/handlers'; describe('DataTableContainer.spec.tsx', () => { const queryClient = new QueryClient(); @@ -421,6 +422,172 @@ describe('DataTableContainer - Selection', () => { }); }); +describe('DataTableContainer - Row Click', () => { + const queryClient = new QueryClient(); + let dataTableContainer: JSX.Element; + let container: HTMLElement; + let rerender: (ui: React.ReactElement) => void; + let user: UserEvent; + let server: ReturnType; + let component: RenderResult; + let nexus: ReturnType; + let nexusSpy: jest.SpyInstance; + + beforeAll(() => { + server = setupServer( + dashboardResource, + dashboardVocabulary, + fetchResourceForDownload + ); + + server.listen(); + }); + + beforeEach(async () => { + const history = createMemoryHistory({}); + + nexus = createNexusClient({ + fetch, + uri: deltaPath(), + }); + const store = configureStore(history, { nexus }, {}); + dataTableContainer = ( + + + + + + + + + + ); + + component = render(dataTableContainer); + + container = component.container; + rerender = component.rerender; + user = userEvent.setup(); + + nexusSpy = jest + .spyOn(nexus, 'httpGet') + .mockImplementation(request => + request.path.toString().includes('format') + ? Promise.resolve([getMockResource('doesnt-matter', {}, 'agents')]) + : Promise.resolve(getMockResource('doesnt-matter', {}, 'agents')) + ); + }); + + // reset any request handlers that are declared as a part of our tests + // (i.e. for testing one-time error scenarios) + afterEach(() => { + cleanup(); + queryClient.clear(); + localStorage.clear(); + nexusSpy.mockClear(); + }); + + afterAll(() => { + server.resetHandlers(); + server.close(); + }); + + const visibleTableRows = () => { + return container.querySelectorAll('table tbody tr.data-table-row'); + }; + + const waitForTableRows = async (expectedRowsCount: number) => { + return await waitFor(() => { + const rows = visibleTableRows(); + expect(rows.length).toEqual(expectedRowsCount); + return rows; + }); + }; + + it('requests correct resource from delta when user clicks on row with revision in self', async () => { + const selfWithRevision = + 'https://localhost:3000/resources/bbp/agents/_/persons%2Fc3358e61-7650-4954-99b7-f7572cbf5d5g?rev=30'; + + const resources = [getMockStudioResource('Malory', `${selfWithRevision}`)]; + + server.use(sparqlViewResultHandler(resources)); + component.unmount(); + rerender(dataTableContainer); + await waitForTableRows(1); + + await user.click(screen.getByText('Malory')); + expect(nexusSpy).toHaveBeenLastCalledWith({ + path: `${selfWithRevision}&format=expanded`, + headers: { Accept: 'application/json' }, + }); + }); + + it('requests correct resource from delta when user clicks on row with tag in self', async () => { + const selfWithTag = + 'https://localhost:3000/resources/bbp/agents/_/persons%2Fc3358e61-7650-4954-99b7-f7572cbf5d5g?tag=30'; + + const resources = [getMockStudioResource('Malory', `${selfWithTag}`)]; + + server.use(sparqlViewResultHandler(resources)); + component.unmount(); + rerender(dataTableContainer); + await waitForTableRows(1); + + await user.click(screen.getByText('Malory')); + expect(nexusSpy).toHaveBeenLastCalledWith({ + path: `${selfWithTag}&format=expanded`, + headers: { Accept: 'application/json' }, + }); + }); + + it('requests correct resource from delta when user clicks on row with tag and revision in self', async () => { + const selfWithTagAndRev = + 'https://localhost:3000/resources/bbp/agents/_/persons%2Fc3358e61-7650-4954-99b7-f7572cbf5d5g?tag=30&rev=2-'; + + const resources = [getMockStudioResource('Malory', `${selfWithTagAndRev}`)]; + + server.use(sparqlViewResultHandler(resources)); + component.unmount(); + rerender(dataTableContainer); + await waitForTableRows(1); + + await user.click(screen.getByText('Malory')); + expect(nexusSpy).toHaveBeenLastCalledWith({ + path: `${selfWithTagAndRev}&format=expanded`, + headers: { Accept: 'application/json' }, + }); + }); + + it('requests correct resource from delta when user clicks on row with no tag or revision in self', async () => { + const selfWithoutTagOrRev = + 'https://localhost:3000/resources/bbp/agents/_/persons%2Fc3358e61-7650-4954-99b7-f7572cbf5d5g'; + + const resources = [ + getMockStudioResource('Malory', `${selfWithoutTagOrRev}`), + ]; + + server.use(sparqlViewResultHandler(resources)); + component.unmount(); + rerender(dataTableContainer); + await waitForTableRows(1); + + await user.click(screen.getByText('Malory')); + expect(nexusSpy).toHaveBeenLastCalledWith({ + path: `${selfWithoutTagOrRev}?format=expanded`, + headers: { Accept: 'application/json' }, + }); + }); +}); + const dashboardErrorResponse = { '@context': 'https://bluebrain.github.io/nexus/contexts/error.json', '@type': 'ResourceNotFound', diff --git a/src/shared/containers/DataTableContainer.tsx b/src/shared/containers/DataTableContainer.tsx index 69a85f2a4..64bea81a9 100644 --- a/src/shared/containers/DataTableContainer.tsx +++ b/src/shared/containers/DataTableContainer.tsx @@ -219,9 +219,11 @@ const DataTableContainer: React.FC = ({ if (resource['@type'] === 'Project') { return; } + const url = new URL(selfUrl); + url.searchParams.set('format', 'expanded'); nexus .httpGet({ - path: `${selfUrl}?format=expanded`, + path: `${url.toString()}`, headers: { Accept: 'application/json' }, }) .then((fullIdResponse: Resource) => {