Skip to content

Commit

Permalink
Merge pull request #1235 from alphagov/add-tests-imports
Browse files Browse the repository at this point in the history
Test that parts of GOV.UK Frontend can be imported individually
  • Loading branch information
36degrees authored Mar 5, 2019
2 parents 415baac + df54ea9 commit f9b2e11
Show file tree
Hide file tree
Showing 20 changed files with 132 additions and 3 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,23 @@

([PR #N](https://github.com/alphagov/govuk-frontend/pull/N))

- Ensure that files within the core, objects and overrides layers can be
imported individually

Unlike components, the files within these layers did not previously import
their dependencies (for example, most of them require the govuk-exports mixin
but did not import it).

We've also added tests to ensure that files within these layers can be
imported and rendered to CSS without erroring, which should catch this in the
future.

Thanks to [Alasdair McLeay](https://github.com/penx) for originally raising a
PR to fix this.

([PR #1235](https://github.com/alphagov/govuk-frontend/pull/1235))


- Ensure inset component does not misalign nested components

Thanks to [Paul Hayes](https://github.com/fofr) for raising this issue.
Expand Down
24 changes: 21 additions & 3 deletions lib/jest-helpers.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
'use strict'

const path = require('path')
const fs = require('fs')
const nunjucks = require('nunjucks')
const path = require('path')
const util = require('util')

const cheerio = require('cheerio')
const nunjucks = require('nunjucks')
const yaml = require('js-yaml')

const sass = require('node-sass')
const sassRender = util.promisify(sass.render)

const configPaths = require('../config/paths.json')
const { componentNameToMacroName } = require('./helper-functions.js')

Expand Down Expand Up @@ -65,6 +70,19 @@ function getExamples (componentName) {
return examples
}

/**
* Render Sass
*
* @param {object} options Options to pass to sass.render
* @return {promise} Result of calling sass.render
*/
function renderSass (options) {
return sassRender({
includePaths: [ configPaths.src ],
...options
})
}

/**
* Get the raw HTML representation of a component, and remove any other
* child elements that do not match the component.
Expand All @@ -83,4 +101,4 @@ function htmlWithClassName ($, className) {
return $.html($component)
}

module.exports = { render, getExamples, htmlWithClassName }
module.exports = { render, getExamples, htmlWithClassName, renderSass }
7 changes: 7 additions & 0 deletions src/components/all.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-env jest */

const { allComponents } = require('../../lib/file-helper')
const { renderSass } = require('../../lib/jest-helpers')

const configPaths = require('../../config/paths.json')

Expand All @@ -21,3 +22,9 @@ describe('When nunjucks is configured with a different base path', () => {
}).not.toThrow()
})
})

it.each(allComponents)('%s.scss renders to CSS without errors', (component) => {
return renderSass({
file: `${configPaths.src}/components/${component}/_${component}.scss`
})
})
4 changes: 4 additions & 0 deletions src/core/_global-styles.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
@import "../settings/all";
@import "../tools/all";
@import "../helpers/all";

@import "links";
@import "typography";

Expand Down
4 changes: 4 additions & 0 deletions src/core/_links.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
@import "../settings/all";
@import "../tools/all";
@import "../helpers/all";

@include govuk-exports("govuk/core/links") {

%govuk-link {
Expand Down
4 changes: 4 additions & 0 deletions src/core/_lists.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
@import "../settings/all";
@import "../tools/all";
@import "../helpers/all";

@include govuk-exports("govuk/core/lists") {

%govuk-list {
Expand Down
4 changes: 4 additions & 0 deletions src/core/_section-break.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
@import "../settings/all";
@import "../tools/all";
@import "../helpers/all";

@include govuk-exports("govuk/core/section-break") {

%govuk-section-break {
Expand Down
2 changes: 2 additions & 0 deletions src/core/_template.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
@import "../settings/all";
@import "../tools/all";
@import "../helpers/all";

@include govuk-exports("govuk/core/template") {

Expand Down
4 changes: 4 additions & 0 deletions src/core/_typography.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
@import "../settings/all";
@import "../tools/all";
@import "../helpers/all";

@include govuk-exports("govuk/core/typography") {

// Headings
Expand Down
11 changes: 11 additions & 0 deletions src/core/core.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* eslint-env jest */

const glob = require('glob')
const { renderSass } = require('../../lib/jest-helpers')
const configPaths = require('../../config/paths.json')

const sassFiles = glob.sync(`${configPaths.src}/core/**/*.scss`)

it.each(sassFiles)('%s renders to CSS without errors', (file) => {
return renderSass({ file: file })
})
4 changes: 4 additions & 0 deletions src/objects/_form-group.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
@import "../settings/all";
@import "../tools/all";
@import "../helpers/all";

@include govuk-exports("govuk/objects/form-group") {

.govuk-form-group {
Expand Down
4 changes: 4 additions & 0 deletions src/objects/_grid.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
@import "../settings/all";
@import "../tools/all";
@import "../helpers/all";

@include govuk-exports("govuk/objects/grid") {

.govuk-grid-row {
Expand Down
4 changes: 4 additions & 0 deletions src/objects/_main-wrapper.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
@import "../settings/all";
@import "../tools/all";
@import "../helpers/all";

// Example usage with Breadcrumbs, phase banners, back links:
// <div class="govuk-width-container">
// <!-- Breadcrumbs, phase banners, back links are placed in here. -->
Expand Down
4 changes: 4 additions & 0 deletions src/objects/_width-container.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
@import "../settings/all";
@import "../tools/all";
@import "../helpers/all";

@mixin govuk-width-container {
// Limit the width of the container to the page width
max-width: $govuk-page-width;
Expand Down
11 changes: 11 additions & 0 deletions src/objects/objects.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* eslint-env jest */

const glob = require('glob')
const { renderSass } = require('../../lib/jest-helpers')
const configPaths = require('../../config/paths.json')

const sassFiles = glob.sync(`${configPaths.src}/objects/**/*.scss`)

it.each(sassFiles)('%s renders to CSS without errors', (file) => {
return renderSass({ file: file })
})
4 changes: 4 additions & 0 deletions src/overrides/_display.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
@import "../settings/all";
@import "../tools/all";
@import "../helpers/all";

@include govuk-exports("govuk/overrides/display") {

.govuk-\!-display-inline {
Expand Down
4 changes: 4 additions & 0 deletions src/overrides/_spacing.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
@import "../settings/all";
@import "../tools/all";
@import "../helpers/all";

////
/// @group overrides
////
Expand Down
4 changes: 4 additions & 0 deletions src/overrides/_typography.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
@import "../settings/all";
@import "../tools/all";
@import "../helpers/all";

@include govuk-exports("govuk/overrides/typography") {
// Font size and line height

Expand Down
4 changes: 4 additions & 0 deletions src/overrides/_width.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
@import "../settings/all";
@import "../tools/all";
@import "../helpers/all";

@include govuk-exports("govuk/overrides/width") {
.govuk-\!-width-full {
width: 100% !important;
Expand Down
11 changes: 11 additions & 0 deletions src/overrides/overrides.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* eslint-env jest */

const glob = require('glob')
const { renderSass } = require('../../lib/jest-helpers')
const configPaths = require('../../config/paths.json')

const sassFiles = glob.sync(`${configPaths.src}/overrides/**/*.scss`)

it.each(sassFiles)('%s renders to CSS without errors', (file) => {
return renderSass({ file: file })
})

0 comments on commit f9b2e11

Please sign in to comment.