Skip to content

Commit

Permalink
Rename renderComponent() to render() to match helper
Browse files Browse the repository at this point in the history
Test helpers and lib functions now match, except one returns a Cheerio object and the other a rendered string
  • Loading branch information
colinrotherham committed Oct 16, 2023
1 parent 677f71b commit aef20a1
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 21 deletions.
4 changes: 2 additions & 2 deletions packages/govuk-frontend-review/src/app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getComponentsFixtures,
getComponentNames,
getComponentNamesFiltered,
renderComponent,
render,
renderPreview
} from '@govuk-frontend/lib/components'
import { filterPath, hasPath } from '@govuk-frontend/lib/files'
Expand Down Expand Up @@ -159,7 +159,7 @@ export default async () => {
}

// Construct and evaluate the component with the data for this example
res.locals.componentView = renderComponent(componentName, {
res.locals.componentView = render(componentName, {
context: fixture.options,
exampleName,
env
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { renderComponent } from '@govuk-frontend/lib/components'
import { render } from '@govuk-frontend/lib/components'
import beautify from 'js-beautify'

/**
Expand All @@ -10,7 +10,7 @@ import beautify from 'js-beautify'
* @returns {string} HTML rendered by the component
*/
export function getHTMLCode(componentName, options) {
const html = renderComponent(componentName, { ...options, env: this.env })
const html = render(componentName, { ...options, env: this.env })

// Default beautify options
const beautifyOptions = beautify.html.defaultOptions()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getExamples, renderComponent } from '@govuk-frontend/lib/components'
import { getExamples, render } from '@govuk-frontend/lib/components'

import { CharacterCount } from './character-count.mjs'

Expand All @@ -7,10 +7,7 @@ describe('CharacterCount', () => {

beforeAll(async () => {
const examples = await getExamples('character-count')
html = renderComponent(
'character-count',
examples['to configure in JavaScript']
)
html = render('character-count', examples['to configure in JavaScript'])
})

beforeEach(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const {
getComponentsFixtures,
getComponentNames,
nunjucksEnv,
renderComponent
render
} = require('@govuk-frontend/lib/components')
const { HtmlValidate } = require('html-validate')

Expand Down Expand Up @@ -136,7 +136,7 @@ describe('Components', () => {
for (const { component: componentName, fixtures } of componentsFixtures) {
const fixtureTasks = fixtures.map(
async ({ name: exampleName, options: context }) => {
const html = renderComponent(componentName, { context })
const html = render(componentName, { context })

// Validate HTML
return expect({
Expand Down
2 changes: 1 addition & 1 deletion shared/helpers/nunjucks.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const cheerio = require('cheerio')
* @returns {import('cheerio').CheerioAPI} HTML rendered by the macro
*/
function render(componentName, options) {
return cheerio.load(components.renderComponent(componentName, options))
return cheerio.load(components.render(componentName, options))
}

/**
Expand Down
4 changes: 2 additions & 2 deletions shared/helpers/puppeteer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { AxePuppeteer } = require('@axe-core/puppeteer')
const { ports } = require('@govuk-frontend/config')
const { renderComponent } = require('@govuk-frontend/lib/components')
const { render } = require('@govuk-frontend/lib/components')
const { componentNameToClassName } = require('@govuk-frontend/lib/names')
const slug = require('slug')

Expand Down Expand Up @@ -90,7 +90,7 @@ async function renderAndInitialise(
await page.$eval(
'#slot', // See boilerplate.njk `<div id="slot">`
(slot, html) => (slot.innerHTML = html),
renderComponent(componentName, renderOptions)
render(componentName, renderOptions)
)

// Call `beforeInitialisation` in a separate `$eval` call
Expand Down
8 changes: 3 additions & 5 deletions shared/lib/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ async function getExamples(componentName, packageOptions) {
* @param {MacroRenderOptions} [options] - Nunjucks macro render options
* @returns {string} HTML rendered by the component
*/
function renderComponent(componentName, options) {
function render(componentName, options) {
const macroName = componentNameToMacroName(componentName)
const macroPath = `govuk/components/${componentName}/macro.njk`

Expand Down Expand Up @@ -182,9 +182,7 @@ function renderPreview(componentName, options) {
const scriptsPath = '/javascripts/govuk-frontend.min.js'

// Render component (optional)
const componentHtml = componentName
? renderComponent(componentName, options)
: ''
const componentHtml = componentName ? render(componentName, options) : ''

// Render page template
return renderTemplate('govuk/template.njk', {
Expand Down Expand Up @@ -266,7 +264,7 @@ module.exports = {
getComponentNamesFiltered,
getExamples,
nunjucksEnv,
renderComponent,
render,
renderMacro,
renderPreview,
renderString,
Expand Down
4 changes: 2 additions & 2 deletions shared/tasks/components.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { basename, dirname, join } from 'path'

import { nunjucksEnv, renderComponent } from '@govuk-frontend/lib/components'
import { nunjucksEnv, render } from '@govuk-frontend/lib/components'
import { getListing, getYaml } from '@govuk-frontend/lib/files'

import { files } from './index.mjs'
Expand Down Expand Up @@ -112,7 +112,7 @@ async function generateFixture(componentDataPath, options) {
previewLayoutModifiers: example.previewLayoutModifiers ?? [],

// Render Nunjucks example
html: renderComponent(componentName, {
html: render(componentName, {
exampleName: example.name,
context: example.options,
env
Expand Down

0 comments on commit aef20a1

Please sign in to comment.