|
| 1 | +/* |
| 2 | + * Copyright (C) 2018-present Arctic Ice Studio <[email protected]> |
| 3 | + * Copyright (C) 2018-present Sven Greb <[email protected]> |
| 4 | + * |
| 5 | + * Project: Nord Docs |
| 6 | + * Repository: https://github.com/arcticicestudio/nord-docs |
| 7 | + * License: MIT |
| 8 | + */ |
| 9 | + |
| 10 | +import React, { Fragment } from "react"; |
| 11 | +import { stripUnit } from "polished"; |
| 12 | + |
| 13 | +import { renderWithTheme } from "nord-docs-test-utils"; |
| 14 | +import { H1, H2, H3, H4, H5, H6 } from "atoms/core/HTMLElements"; |
| 15 | + |
| 16 | +describe("theme styles", () => { |
| 17 | + test("matches the snapshot", () => { |
| 18 | + const { container } = renderWithTheme( |
| 19 | + <Fragment> |
| 20 | + <H1>Heading Level 1</H1> |
| 21 | + <H2>Heading Level 2</H2> |
| 22 | + <H3>Heading Level 3</H3> |
| 23 | + <H4>Heading Level 4</H4> |
| 24 | + <H5>Heading Level 5</H5> |
| 25 | + <H6>Heading Level 6</H6> |
| 26 | + </Fragment> |
| 27 | + ); |
| 28 | + expect(container).toMatchSnapshot(); |
| 29 | + }); |
| 30 | + |
| 31 | + test("matches the snapshot with adjusted margin", () => { |
| 32 | + const { container } = renderWithTheme( |
| 33 | + <Fragment> |
| 34 | + <H1 noMargin>Heading Level 1</H1> |
| 35 | + <H2 noMargin>Heading Level 2</H2> |
| 36 | + <H3 noMargin>Heading Level 3</H3> |
| 37 | + <H4 noMargin>Heading Level 4</H4> |
| 38 | + <H5 noMargin>Heading Level 5</H5> |
| 39 | + <H6 noMargin>Heading Level 6</H6> |
| 40 | + </Fragment> |
| 41 | + ); |
| 42 | + expect(container).toMatchSnapshot(); |
| 43 | + }); |
| 44 | + |
| 45 | + test("has explicit font size definitions", () => { |
| 46 | + const { container } = renderWithTheme( |
| 47 | + <Fragment> |
| 48 | + <H1 noMargin>Heading Level 1</H1> |
| 49 | + <H2 noMargin>Heading Level 2</H2> |
| 50 | + <H3 noMargin>Heading Level 3</H3> |
| 51 | + <H4 noMargin>Heading Level 4</H4> |
| 52 | + <H5 noMargin>Heading Level 5</H5> |
| 53 | + <H6 noMargin>Heading Level 6</H6> |
| 54 | + </Fragment> |
| 55 | + ); |
| 56 | + expect( |
| 57 | + Object.values(container.children) |
| 58 | + .map(headingElement => getComputedStyle(headingElement).fontSize) |
| 59 | + .filter(Boolean).length |
| 60 | + ).toEqual(container.children.length); |
| 61 | + }); |
| 62 | + |
| 63 | + test("has no top margin", () => { |
| 64 | + const { container } = renderWithTheme(<H1>Nord</H1>); |
| 65 | + expect(container.firstChild).toHaveStyleRule("margin-top", "0"); |
| 66 | + }); |
| 67 | + |
| 68 | + test("adjusts bottom margin based on passed props", () => { |
| 69 | + const { container } = renderWithTheme(<H1 noMargin>Nord</H1>); |
| 70 | + expect(container.firstChild).toHaveStyleRule("margin-bottom", "0"); |
| 71 | + }); |
| 72 | + |
| 73 | + test("Ensure descending font sizes between all heading levels", () => { |
| 74 | + const { container } = renderWithTheme( |
| 75 | + <Fragment> |
| 76 | + <H1 noMargin>Heading Level 1</H1> |
| 77 | + <H2 noMargin>Heading Level 2</H2> |
| 78 | + <H3 noMargin>Heading Level 3</H3> |
| 79 | + <H4 noMargin>Heading Level 4</H4> |
| 80 | + <H5 noMargin>Heading Level 5</H5> |
| 81 | + <H6 noMargin>Heading Level 6</H6> |
| 82 | + </Fragment> |
| 83 | + ); |
| 84 | + |
| 85 | + expect( |
| 86 | + /* Get the font sizes as numbers of all heading components in rendered order. */ |
| 87 | + Object.values(container.children) |
| 88 | + .map(headingElement => stripUnit(getComputedStyle(headingElement).fontSize)) |
| 89 | + /* Ensure descending font sizes by comparing a higher level heading with the next lower one. */ |
| 90 | + .reduce((acc, cur) => (acc > cur ? cur : 0)) |
| 91 | + ).toBeGreaterThan(0); |
| 92 | + }); |
| 93 | +}); |
0 commit comments