From 9b11df04b3fa7382379e2695ef08b66a6a30b713 Mon Sep 17 00:00:00 2001 From: Alex Corey Date: Thu, 20 Feb 2020 15:16:49 -0500 Subject: [PATCH] Fixes navigation bug --- .../TemplateList/TemplateListItem.jsx | 8 +- .../TemplateList/TemplateListItem.test.jsx | 87 ++++++++++++++++++- 2 files changed, 93 insertions(+), 2 deletions(-) diff --git a/awx/ui_next/src/screens/Template/TemplateList/TemplateListItem.jsx b/awx/ui_next/src/screens/Template/TemplateList/TemplateListItem.jsx index b13eb92298..f08a0ccb06 100644 --- a/awx/ui_next/src/screens/Template/TemplateList/TemplateListItem.jsx +++ b/awx/ui_next/src/screens/Template/TemplateList/TemplateListItem.jsx @@ -1,5 +1,5 @@ import React from 'react'; -import { Link } from 'react-router-dom'; +import { Link, useLocation } from 'react-router-dom'; import { Button, DataListAction as _DataListAction, @@ -40,6 +40,12 @@ function TemplateListItem({ i18n, template, isSelected, onSelect, detailUrl }) { (!template.summary_fields.inventory && !template.ask_inventory_on_launch)); + const location = useLocation(); + + if (location.pathname.startsWith('/projects')) { + detailUrl = `/templates/job_template/${template.id}/details`; + } + return ( diff --git a/awx/ui_next/src/screens/Template/TemplateList/TemplateListItem.test.jsx b/awx/ui_next/src/screens/Template/TemplateList/TemplateListItem.test.jsx index 8fa48e08d6..d834478668 100644 --- a/awx/ui_next/src/screens/Template/TemplateList/TemplateListItem.test.jsx +++ b/awx/ui_next/src/screens/Template/TemplateList/TemplateListItem.test.jsx @@ -1,7 +1,8 @@ import React from 'react'; +import { Route } from 'react-router-dom'; import { mountWithContexts } from '@testUtils/enzymeHelpers'; - +import { createMemoryHistory } from 'history'; import TemplateListItem from './TemplateListItem'; describe('', () => { @@ -161,4 +162,88 @@ describe('', () => { ); expect(wrapper.find('ExclamationTriangleIcon').exists()).toBe(false); }); + test('clicking on template from templates list navigates properly', () => { + const history = createMemoryHistory({ + initialEntries: ['/templates'], + }); + const wrapper = mountWithContexts( + ( + + )} + />, + { + context: { + router: { + history, + route: { + location: history.location, + match: { + params: { id: 1 }, + }, + }, + }, + }, + } + ); + wrapper.find('Link').simulate('click', { button: 0 }); + expect(history.location.pathname).toEqual( + '/templates/job_template/1/details' + ); + }); + test('clicking on template from project templates list navigates properly', () => { + const history = createMemoryHistory({ + initialEntries: ['/projects/1/job_templates'], + }); + const wrapper = mountWithContexts( + ( + + )} + />, + { + context: { + router: { + history, + route: { + location: history.location, + match: { + params: { id: 1 }, + }, + }, + }, + }, + } + ); + wrapper.find('Link').simulate('click', { button: 0 }); + expect(history.location.pathname).toEqual( + '/templates/job_template/2/details' + ); + }); });