Skip to content

Commit 945f8dd

Browse files
Add unit tests for grid mixins from GOV.UK Frontend
See: alphagov/govuk-frontend#1090 Co-authored-by: Oliver Byford <oliver.byford@digital.cabinet-office.gov.uk>
1 parent f97f5dc commit 945f8dd

File tree

1 file changed

+299
-0
lines changed

1 file changed

+299
-0
lines changed
Lines changed: 299 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,299 @@
1+
import { outdent } from 'outdent'
2+
import { compileStringAsync } from 'sass-embedded'
3+
4+
describe('Grid system', () => {
5+
const sassImports = `
6+
@import "core/settings/breakpoints";
7+
@import "core/settings/globals";
8+
@import "core/settings/spacing";
9+
10+
@import "core/tools/grid";
11+
@import "core/tools/exports";
12+
@import "core/tools/sass-mq";
13+
`
14+
15+
describe('@function nhsuk-grid-width', () => {
16+
it('outputs the specified key value from the map of widths', async () => {
17+
const sass = `
18+
${sassImports}
19+
20+
.foo {
21+
content: nhsuk-grid-width(one-quarter);
22+
}
23+
`
24+
25+
const results = await compileStringAsync(sass, {
26+
loadPaths: ['packages']
27+
})
28+
29+
expect(results.css).toBe(
30+
outdent`
31+
.foo {
32+
content: 25%;
33+
}
34+
`
35+
)
36+
})
37+
38+
it('throws an error that the specified key does not exist in the map of widths', async () => {
39+
const sass = `
40+
${sassImports}
41+
42+
$value: nhsuk-grid-width(seven-fifths);
43+
`
44+
45+
const results = compileStringAsync(sass, {
46+
loadPaths: ['packages']
47+
})
48+
49+
await expect(results).rejects.toThrow('Unknown grid width `seven-fifths`')
50+
})
51+
})
52+
53+
describe('@mixin nhsuk-grid-row', () => {
54+
it('outputs default defined styles for .nhsuk-grid-row class', async () => {
55+
const sass = `
56+
${sassImports}
57+
58+
@import "core/tools/mixins";
59+
60+
@include nhsuk-grid-row;
61+
`
62+
63+
const results = await compileStringAsync(sass, {
64+
loadPaths: ['packages']
65+
})
66+
67+
expect(results.css).toBe(outdent`
68+
.nhsuk-grid-row {
69+
margin-right: -16px;
70+
margin-left: -16px;
71+
}
72+
.nhsuk-grid-row::after {
73+
clear: both;
74+
content: \"\";
75+
display: block;
76+
}
77+
`)
78+
})
79+
80+
it('outputs styles for the specified class', async () => {
81+
const sass = `
82+
${sassImports}
83+
84+
@import "core/tools/mixins";
85+
86+
@include nhsuk-grid-row("app-grid-row");
87+
`
88+
89+
const results = await compileStringAsync(sass, {
90+
loadPaths: ['packages']
91+
})
92+
93+
expect(results.css).toBe(outdent`
94+
.app-grid-row {
95+
margin-right: -16px;
96+
margin-left: -16px;
97+
}
98+
.app-grid-row::after {
99+
clear: both;
100+
content: \"\";
101+
display: block;
102+
}
103+
`)
104+
})
105+
})
106+
107+
describe('@mixin nhsuk-grid-column', () => {
108+
it('outputs the CSS required for a column in the grid', async () => {
109+
const sass = `
110+
${sassImports}
111+
112+
.nhsuk-grid-column-full {
113+
@include nhsuk-grid-column($class: false);
114+
}
115+
`
116+
117+
const results = await compileStringAsync(sass, {
118+
loadPaths: ['packages']
119+
})
120+
121+
expect(results.css).toBe(outdent`
122+
.nhsuk-grid-column-full {
123+
box-sizing: border-box;
124+
width: 100%;
125+
padding: 0 16px;
126+
}
127+
@media (min-width: 40.0625em) {
128+
.nhsuk-grid-column-full {
129+
width: 100%;
130+
float: left;
131+
}
132+
}
133+
`)
134+
})
135+
136+
it('allows different widths to be specified using $width', async () => {
137+
const sass = `
138+
${sassImports}
139+
140+
.nhsuk-grid-column-two-thirds {
141+
@include nhsuk-grid-column(two-thirds, $class: false);
142+
}
143+
`
144+
145+
const results = await compileStringAsync(sass, {
146+
loadPaths: ['packages']
147+
})
148+
149+
expect(results.css).toBe(outdent`
150+
.nhsuk-grid-column-two-thirds {
151+
box-sizing: border-box;
152+
width: 100%;
153+
padding: 0 16px;
154+
}
155+
@media (min-width: 40.0625em) {
156+
.nhsuk-grid-column-two-thirds {
157+
width: 66.6666666667%;
158+
float: left;
159+
}
160+
}
161+
`)
162+
})
163+
164+
it('allows predefined breakpoints to be specified using $at', async () => {
165+
const sass = `
166+
${sassImports}
167+
168+
.nhsuk-grid-column-one-quarter-at-desktop {
169+
@include nhsuk-grid-column(one-quarter, $at: desktop, $class: false);
170+
}
171+
`
172+
173+
const results = await compileStringAsync(sass, {
174+
loadPaths: ['packages']
175+
})
176+
177+
expect(results.css).toBe(outdent`
178+
.nhsuk-grid-column-one-quarter-at-desktop {
179+
box-sizing: border-box;
180+
padding: 0 16px;
181+
}
182+
@media (min-width: 48.0625em) {
183+
.nhsuk-grid-column-one-quarter-at-desktop {
184+
width: 25%;
185+
float: left;
186+
}
187+
}
188+
`)
189+
})
190+
191+
it('allows custom breakpoints to be specified using $at', async () => {
192+
const sass = `
193+
${sassImports}
194+
195+
.nhsuk-grid-column-one-quarter-at-500px {
196+
@include nhsuk-grid-column(one-quarter, $at: 500px, $class: false);
197+
}
198+
`
199+
200+
const results = await compileStringAsync(sass, {
201+
loadPaths: ['packages']
202+
})
203+
204+
expect(results.css).toBe(outdent`
205+
.nhsuk-grid-column-one-quarter-at-500px {
206+
box-sizing: border-box;
207+
width: 100%;
208+
padding: 0 16px;
209+
}
210+
@media (min-width: 31.25em) {
211+
.nhsuk-grid-column-one-quarter-at-500px {
212+
width: 25%;
213+
float: left;
214+
}
215+
}
216+
`)
217+
})
218+
219+
it('allows columns to float right using $float: right', async () => {
220+
const sass = `
221+
${sassImports}
222+
223+
.nhsuk-grid-column-one-half-right {
224+
@include nhsuk-grid-column(one-half, $float: right, $class: false);
225+
}
226+
`
227+
228+
const results = await compileStringAsync(sass, {
229+
loadPaths: ['packages']
230+
})
231+
232+
expect(results.css).toBe(outdent`
233+
.nhsuk-grid-column-one-half-right {
234+
box-sizing: border-box;
235+
width: 100%;
236+
padding: 0 16px;
237+
}
238+
@media (min-width: 40.0625em) {
239+
.nhsuk-grid-column-one-half-right {
240+
width: 50%;
241+
float: right;
242+
}
243+
}
244+
`)
245+
})
246+
247+
it('includes the class name by default (deprecated)', async () => {
248+
const sass = `
249+
${sassImports}
250+
251+
@include nhsuk-grid-column();
252+
`
253+
254+
const results = await compileStringAsync(sass, {
255+
loadPaths: ['packages']
256+
})
257+
258+
expect(results.css).toBe(outdent`
259+
.nhsuk-grid-column-full {
260+
box-sizing: border-box;
261+
width: 100%;
262+
padding: 0 16px;
263+
}
264+
@media (min-width: 40.0625em) {
265+
.nhsuk-grid-column-full {
266+
width: 100%;
267+
float: left;
268+
}
269+
}
270+
`)
271+
})
272+
273+
it('allows the class name to be overridden (deprecated)', async () => {
274+
const sass = `
275+
${sassImports}
276+
277+
@include nhsuk-grid-column(three-quarters, $class: "large-column");
278+
`
279+
280+
const results = await compileStringAsync(sass, {
281+
loadPaths: ['packages']
282+
})
283+
284+
expect(results.css).toBe(outdent`
285+
.large-column-three-quarters {
286+
box-sizing: border-box;
287+
width: 100%;
288+
padding: 0 16px;
289+
}
290+
@media (min-width: 40.0625em) {
291+
.large-column-three-quarters {
292+
width: 75%;
293+
float: left;
294+
}
295+
}
296+
`)
297+
})
298+
})
299+
})

0 commit comments

Comments
 (0)