Skip to content

Commit

Permalink
Merge pull request #2120 from alphagov/view-template-with-default-layout
Browse files Browse the repository at this point in the history
View templates with default layout
  • Loading branch information
BenSurgisonGDS committed Apr 14, 2023
2 parents deb86fc + 3fb8851 commit 50378a5
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

### Fixes

- [#2120: View templates with default layout](https://github.com/alphagov/govuk-prototype-kit/pull/2120)
Viewing templates in Manage Prototype now works even if app/views/layouts/main.html is missing

- [#2100: Make sure exact versions of plugins are installed from the kit](https://github.com/alphagov/govuk-prototype-kit/pull/2100)

## 13.6.0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// core dependencies
const path = require('path')

// local dependencies
const { waitForApplication, installPlugin } = require('../../utils')
const { manageTemplatesPagePath, getTemplateLink } = require('../plugin-utils')

const plugin = '@govuk-prototype-kit/step-by-step'
const pluginPageTemplate = '/templates/step-by-step-navigation.html'
const pluginPageTitle = 'Step by step navigation'

const defaultLayoutFilePath = path.join('app', 'views', 'layouts', 'main.html')
const backupLayoutComment = '<!-- could not find layouts/main.html or layouts/main.njk in prototype, using backup default template -->'

const comments = el => cy.wrap(
[...el.childNodes]
.filter(node => node.nodeName === '#comment')
.map(commentNode => '<!--' + commentNode.data + '-->')
)

describe('view template with default layout', () => {
before(() => {
cy.task('copyFromStarterFiles', { filename: defaultLayoutFilePath })
installPlugin(plugin, 'latest')
})

after(() => {
cy.task('copyFromStarterFiles', { filename: defaultLayoutFilePath })
})

it('deleting default layout does not cause viewing a template to fail to render', () => {
cy.task('log', 'Visit the manage prototype plugins page')

waitForApplication(manageTemplatesPagePath)
cy.visit(manageTemplatesPagePath)

cy.task('log', `Preview the ${pluginPageTitle} template`)

cy.get(`a[href="${getTemplateLink('view', '@govuk-prototype-kit/step-by-step', pluginPageTemplate)}"]`).click()

cy.document().then(doc =>
comments(doc.head).should('not.contain', backupLayoutComment)
)

cy.task('deleteFile', { filename: path.join(Cypress.env('projectFolder'), defaultLayoutFilePath) })

waitForApplication(manageTemplatesPagePath)
cy.visit(manageTemplatesPagePath)

cy.task('log', `Preview the ${pluginPageTitle} template`)

cy.get(`a[href="${getTemplateLink('view', '@govuk-prototype-kit/step-by-step', pluginPageTemplate)}"]`).click()

cy.visit('/', { failOnStatusCode: false })
cy.get('body').should('not.contains.text', 'Error: template not found')

cy.document().then(doc => {
cy.log('head content', doc.head.innerHTML)
comments(doc.head).should('contain', backupLayoutComment)
})
})
})
3 changes: 2 additions & 1 deletion lib/manage-prototype-handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ const contextPath = '/manage-prototype'

const appViews = plugins.getAppViews([
path.join(projectDir, 'node_modules'),
path.join(projectDir, 'app/views/')
path.join(projectDir, 'app/views/'),
path.join(packageDir, 'lib/final-backup-nunjucks')
])

const pkgPath = path.join(projectDir, 'package.json')
Expand Down

0 comments on commit 50378a5

Please sign in to comment.