diff --git a/cypress/e2e/plugins/plugin-utils.js b/cypress/e2e/plugins/plugin-utils.js index 45c90b2604..0ab6d16406 100644 --- a/cypress/e2e/plugins/plugin-utils.js +++ b/cypress/e2e/plugins/plugin-utils.js @@ -56,6 +56,11 @@ function initiatePluginAction (action, plugin, pluginName, options = {}) { .find('a') .click() + if (action === 'update') { + cy.get('a') + .contains('Latest version:').click() + } + cy.get('button') .contains(capitalize(action)) .click() @@ -110,6 +115,9 @@ function provePluginUpdated (plugin, pluginName) { .find('a') .click() + cy.get('a') + .contains('Latest version:').should('not.exist') + cy.get('button') .contains(capitalize('update')).should('not.exist') } diff --git a/lib/manage-prototype-handlers.js b/lib/manage-prototype-handlers.js index 9f7a8503be..f6d7997944 100644 --- a/lib/manage-prototype-handlers.js +++ b/lib/manage-prototype-handlers.js @@ -404,7 +404,7 @@ function getTemplatesPostInstallHandler (req, res) { })) } -function buildPluginData (pluginData) { +function buildPluginData (pluginData, isLatest) { if (pluginData === undefined) { return } @@ -417,12 +417,14 @@ function buildPluginData (pluginData) { installedVersion, required, localVersion, - pluginConfig = {} + pluginConfig = {}, + latestPluginConfig } = pluginData + const meta = isLatest ? latestPluginConfig?.meta || pluginConfig?.meta : pluginConfig?.meta const preparedPackageNameForDisplay = plugins.preparePackageNameForDisplay(packageName) return { ...preparedPackageNameForDisplay, - ...pluginConfig.meta, + ...meta, packageName, latestVersion, installedLocally, @@ -433,7 +435,7 @@ function buildPluginData (pluginData) { uninstallLink: installed && !required ? `${contextPath}/plugins/uninstall?package=${encodeURIComponent(packageName)}${installedLocally ? `&version=${encodeURIComponent(localVersion)}` : ''}` : undefined, uninstallCommand: `npm uninstall ${packageName}`, installedVersion, - inThisPlugin: getInThisPluginDetails(pluginConfig) + inThisPlugin: getInThisPluginDetails(isLatest ? latestPluginConfig || pluginConfig : pluginConfig) } } @@ -764,15 +766,20 @@ async function postPluginsModeHandler (req, res) { async function getPluginDetailsHandler (req, res) { const packageName = req.query.package + const isLatest = req.route.path.split('/').pop() === 'latest' + const latestLink = isLatest ? '' : req.originalUrl.replace('?', '/latest?') + const installedLink = isLatest ? req.originalUrl.replace('/latest?', '?') : '' const plugin = await lookupPackageInfo(packageName) - const { name, scope, installedVersion, latestVersion, ...pluginData } = buildPluginData(plugin) + const { name, scope, installedVersion, latestVersion, ...pluginData } = buildPluginData(plugin, isLatest) const viewData = { ...pluginData, installedVersion, latestVersion, - plugin: { name, scope, version: installedVersion || latestVersion } + latestLink, + installedLink, + plugin: { name, scope, version: latestLink ? installedVersion || latestVersion : latestVersion } } - res.render(getManagementView('plugin-details.njk'), viewData) + res.send(nunjucksManagementEnv.render(getManagementView('plugin-details.njk'), viewData)) } module.exports = { diff --git a/lib/manage-prototype-handlers.test.js b/lib/manage-prototype-handlers.test.js index c0906036fe..cf5832c86a 100644 --- a/lib/manage-prototype-handlers.test.js +++ b/lib/manage-prototype-handlers.test.js @@ -40,6 +40,7 @@ const { postTemplatesInstallHandler, getTemplatesPostInstallHandler, getPluginsHandler, + getPluginDetailsHandler, postPluginsStatusHandler, postPluginsModeMiddleware, getPluginsModeHandler, @@ -502,6 +503,32 @@ describe('manage-prototype-handlers', () => { expect(res.redirect).toHaveBeenCalledWith(fullPath + '?search=' + search) }) + describe('getPluginDetailsHandler', () => { + it('plugins installed', async () => { + fse.readJsonSync.mockReturnValue(undefined) + req.route.path = 'plugins-installed' + await getPluginDetailsHandler(req, res) + expect(mockNunjucksRender).toHaveBeenCalledWith( + 'views/manage-prototype/plugin-details.njk', + expect.objectContaining({ + packageName: 'test-package', + installLink: '/manage-prototype/plugins/install?package=test-package', + installCommand: 'npm install test-package', + updateCommand: 'npm install test-package@2.0.0', + uninstallCommand: 'npm uninstall test-package', + inThisPlugin: [], + latestVersion: '2.0.0', + latestLink: '/current-url', + installedLink: '', + plugin: { + name: 'Test Package', + version: '2.0.0' + } + }) + ) + }) + }) + it('getPluginsModeHandler', async () => { req.params.mode = 'install' req.query.package = packageName diff --git a/lib/manage-prototype-routes.js b/lib/manage-prototype-routes.js index e3147d1880..5841933aee 100644 --- a/lib/manage-prototype-routes.js +++ b/lib/manage-prototype-routes.js @@ -80,6 +80,7 @@ router.post('/plugins/:mode', postPluginsModeMiddleware) router.post('/plugins/:mode', csrfProtection, postPluginsModeHandler) +router.get('/plugin-details/latest', getPluginDetailsHandler) router.get('/plugin-details', getPluginDetailsHandler) // Find GOV.UK Frontend (via internal package, project fallback) diff --git a/lib/nunjucks/views/manage-prototype/plugin-details.njk b/lib/nunjucks/views/manage-prototype/plugin-details.njk index 5445adc355..c9cad76965 100644 --- a/lib/nunjucks/views/manage-prototype/plugin-details.njk +++ b/lib/nunjucks/views/manage-prototype/plugin-details.njk @@ -14,7 +14,12 @@
Latest version: {{ latestVersion }}
+ {% if latestLink %} + + {% endif %} + {% if installedLink %} + + {% endif %} {% endif %}