Skip to content

Commit

Permalink
Load resource with tag/revision if it exists in self
Browse files Browse the repository at this point in the history
Signed-off-by: Dinika Saxena <[email protected]>
  • Loading branch information
Dinika committed Sep 17, 2024
1 parent 7efccbf commit 7ac2d3e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
14 changes: 13 additions & 1 deletion src/shared/containers/DataTableContainer.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ describe('DataTableContainer - Row Click', () => {
let component: RenderResult;
let nexus: ReturnType<typeof createNexusClient>;
let nexusSpy: jest.SpyInstance;
let historySpy: jest.SpyInstance;

beforeAll(() => {
server = setupServer(
Expand Down Expand Up @@ -485,6 +486,7 @@ describe('DataTableContainer - Row Click', () => {
? Promise.resolve([getMockResource('doesnt-matter', {}, 'agents')])
: Promise.resolve(getMockResource('doesnt-matter', {}, 'agents'))
);
historySpy = jest.spyOn(history, 'push');
});

// reset any request handlers that are declared as a part of our tests
Expand All @@ -494,6 +496,7 @@ describe('DataTableContainer - Row Click', () => {
queryClient.clear();
localStorage.clear();
nexusSpy.mockClear();
historySpy.mockClear();
});

afterAll(() => {
Expand Down Expand Up @@ -529,6 +532,8 @@ describe('DataTableContainer - Row Click', () => {
path: `${selfWithRevision}&format=expanded`,
headers: { Accept: 'application/json' },
});
const navigateTo = historySpy.mock.calls[0][0];
expect(navigateTo).toContain('rev=30');
});

it('requests correct resource from delta when user clicks on row with tag in self', async () => {
Expand All @@ -547,11 +552,13 @@ describe('DataTableContainer - Row Click', () => {
path: `${selfWithTag}&format=expanded`,
headers: { Accept: 'application/json' },
});
const navigateTo = historySpy.mock.calls[0][0];
expect(navigateTo).toContain('tag=30');
});

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-';
'https://localhost:3000/resources/bbp/agents/_/persons%2Fc3358e61-7650-4954-99b7-f7572cbf5d5g?tag=30&rev=20';

const resources = [getMockStudioResource('Malory', `${selfWithTagAndRev}`)];

Expand All @@ -565,6 +572,8 @@ describe('DataTableContainer - Row Click', () => {
path: `${selfWithTagAndRev}&format=expanded`,
headers: { Accept: 'application/json' },
});
const navigateTo = historySpy.mock.calls[0][0];
expect(navigateTo).toContain('rev=20');
});

it('requests correct resource from delta when user clicks on row with no tag or revision in self', async () => {
Expand All @@ -585,6 +594,9 @@ describe('DataTableContainer - Row Click', () => {
path: `${selfWithoutTagOrRev}?format=expanded`,
headers: { Accept: 'application/json' },
});
const navigateTo = historySpy.mock.calls[0][0];
expect(navigateTo).not.toContain('rev');
expect(navigateTo).not.toContain('tag');
});
});

Expand Down
23 changes: 18 additions & 5 deletions src/shared/containers/DataTableContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -219,19 +219,32 @@ const DataTableContainer: React.FC<DataTableProps> = ({
if (resource['@type'] === 'Project') {
return;
}
const url = new URL(selfUrl);
url.searchParams.set('format', 'expanded');
const resourceUrl = new URL(selfUrl);
resourceUrl.searchParams.set('format', 'expanded');
nexus
.httpGet({
path: `${url.toString()}`,
path: `${resourceUrl.toString()}`,
headers: { Accept: 'application/json' },
})
.then((fullIdResponse: Resource) => {
const [orgLabel, projectLabel] = parseProjectUrl(resource._project);
const hist = `/${orgLabel}/${projectLabel}/resources/${encodeURIComponent(

let fullResourceId = `/${orgLabel}/${projectLabel}/resources/${encodeURIComponent(
fullIdResponse[0]['@id']
)}`;
history.push(hist, { background: location });

const revision = resourceUrl.searchParams.get('rev');
const tag = resourceUrl.searchParams.get('tag');

if (revision) {
fullResourceId = `${fullResourceId}?rev=${revision}`;
} else if (tag) {
fullResourceId = `${fullResourceId}?tag=${tag}`;
}

history.push(`${fullResourceId.toString()}`, {
background: location,
});
});
})
.catch(error => {
Expand Down

0 comments on commit 7ac2d3e

Please sign in to comment.