Skip to content

Commit

Permalink
fix markdown tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
bgarropy-atlassian committed Jan 30, 2022
1 parent 1b5f882 commit 3e1d8bd
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 42 deletions.
79 changes: 59 additions & 20 deletions src/hooks/useMarkdown/useMarkdown.test.tsx
Expand Up @@ -2,32 +2,34 @@ import {render, screen} from "@testing-library/react"
import {renderHook} from "@testing-library/react-hooks"
import {useMarkdown} from "hooks"

test("renders local links", () => {
const name = "blog"
describe("renders links", () => {
test("local links", () => {
const name = "blog"

const {result} = renderHook(() =>
useMarkdown(`<a href="/${name}">${name}</a>`),
)
const {result} = renderHook(() =>
useMarkdown(`<a href="/${name}">${name}</a>`),
)

render(result.current)
render(result.current)

const link = screen.getByText(name)
expect(link).toHaveAttribute("href", `/${name}`)
})
const link = screen.getByText(name)
expect(link).toHaveAttribute("href", `/${name}`)
})

test("renders external links", () => {
const name = "example"
test("external links", () => {
const name = "example"

const {result} = renderHook(() =>
useMarkdown(`<a href="https://${name}.com">${name}</a>`),
)
const {result} = renderHook(() =>
useMarkdown(`<a href="https://${name}.com">${name}</a>`),
)

render(result.current)
const link = screen.getByText(name)
render(result.current)
const link = screen.getByText(name)

expect(link).toHaveAttribute("href", `https://${name}.com`)
expect(link).toHaveAttribute("rel", "noopener noreferrer")
expect(link).toHaveAttribute("target", "_blank")
expect(link).toHaveAttribute("href", `https://${name}.com`)
expect(link).toHaveAttribute("rel", "noopener noreferrer")
expect(link).toHaveAttribute("target", "_blank")
})
})

test("renders images", () => {
Expand All @@ -42,6 +44,43 @@ test("renders images", () => {
render(result.current)

const image = screen.getByAltText(name)
expect(image)
expect(image.parentElement.parentElement).toHaveClass("figure")
})

describe("renders headers", () => {
test("level one", () => {
const {result} = renderHook(() => useMarkdown("<h1>Heading one</h1>"))
render(result.current)

const icon = screen.getByLabelText("link")
const link = icon.parentElement
const heading = link.parentElement

expect(link).toHaveAttribute("href", "#heading-one")
expect(heading).toHaveAttribute("id", "heading-one")
})

test("level two", () => {
const {result} = renderHook(() => useMarkdown("<h2>Heading two</h2>"))
render(result.current)

const icon = screen.getByLabelText("link")
const link = icon.parentElement
const heading = link.parentElement

expect(link).toHaveAttribute("href", "#heading-two")
expect(heading).toHaveAttribute("id", "heading-two")
})

test("level three", () => {
const {result} = renderHook(() => useMarkdown("<h3>Heading three</h3>"))
render(result.current)

const icon = screen.getByLabelText("link")
const link = icon.parentElement
const heading = link.parentElement

expect(link).toHaveAttribute("href", "#heading-three")
expect(heading).toHaveAttribute("id", "heading-three")
})
})
22 changes: 0 additions & 22 deletions src/utils/markdown.test.ts
Expand Up @@ -122,28 +122,6 @@ describe("transforms markdown", () => {
)
})

test.skip("syntax highlights", async () => {
expect(true).toBeTruthy()
})

test("slugifies headings", async () => {
const html = await transformMarkdown("# Heading one")

// eslint-disable-next-line quotes
expect(html).toEqual(expect.stringContaining('<h1 id="heading-one">'))
})

test("links headings", async () => {
const html = await transformMarkdown("# Heading one")

expect(html).toEqual(
expect.stringContaining(
// eslint-disable-next-line quotes
'<a aria-hidden="true" tabindex="-1" href="#heading-one">',
),
)
})

test("includes image sizes", async () => {
const html = await transformMarkdown("![brad garropy](/profile.jpg)")

Expand Down

1 comment on commit 3e1d8bd

@vercel
Copy link

@vercel vercel bot commented on 3e1d8bd Jan 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.