Skip to content

Commit

Permalink
Forced the back links on the draft and template pages to point back t…
Browse files Browse the repository at this point in the history
…o their respective indexes
  • Loading branch information
crismali committed Jan 29, 2025
1 parent d41585b commit 95dcc1a
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 16 deletions.
3 changes: 0 additions & 3 deletions src/pages/__tests__/dashboard.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import React from 'react'
import { render } from '@testing-library/react'
import { faker } from '@faker-js/faker'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { randomUUID } from 'crypto'
import DashboardPage from '../dashboard'
import {
EmailTemplateIndex,
Expand All @@ -17,7 +15,6 @@ import {
urlFor,
} from 'src/testHelpers'
import { SIDEBAR_NAVIGATION_TEST_ID } from 'src/ui'
import { group } from 'console'

jest.mock('src/network/emailTemplates', () => {
return {
Expand Down
1 change: 1 addition & 0 deletions src/pages/email-templates/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const EmailTemplateShowPage: FC<Props> = ({ params }) => {
>
<div ref={beforeLayoutRef as any} />
<EmailEditorSidebar
linkBackTo="/my-drafts"
emailTranslation={currentTranslation}
heading={
<>
Expand Down
1 change: 1 addition & 0 deletions src/templates/EmailEditorPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const EmailEditorPage: FC<Props> = ({ pageContext, location }) => {
>
<div ref={beforeLayoutRef as any} />
<EmailEditorSidebar
linkBackTo="/library"
emailTranslation={currentTranslation}
heading={
<>
Expand Down
8 changes: 6 additions & 2 deletions src/templates/EmailEditorSidebar/BackLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import React, { FC } from 'react'
import { Link, navigate } from 'gatsby'
import { BackArrowIcon } from 'src/ui'

export const BackLink: FC = () => {
interface Props {
to: string
}

export const BackLink: FC<Props> = ({ to }) => {
return (
<Link to="#" onClick={() => navigate(-1)} className="back-link">
<Link to={to} className="back-link">
<BackArrowIcon />
<span className="back-link-text">Back</span>
</Link>
Expand Down
14 changes: 6 additions & 8 deletions src/templates/EmailEditorSidebar/__tests__/BackLink.test.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import React from 'react'
import { render } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { navigate } from 'gatsby'
import { BackLink } from '../BackLink'
import { faker } from '@faker-js/faker'
import { urlFor } from 'src/testHelpers'

describe('BackLink', () => {
it('is a link that goes back when clicked', async () => {
const user = userEvent.setup()
const { baseElement } = render(<BackLink />)
const link = baseElement.querySelector('a')
const path = `/${faker.lorem.word()}`
const { baseElement } = render(<BackLink to={path} />)
const link: HTMLAnchorElement = baseElement.querySelector('a') as any
expect(link).toHaveTextContent('Back')
expect(navigate).not.toHaveBeenCalled()
await user.click(link!)
expect(navigate).toHaveBeenCalledWith(-1)
expect(link.href).toEqual(urlFor(path))
})
})
9 changes: 8 additions & 1 deletion src/templates/EmailEditorSidebar/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('EmailEditorSidebar', () => {
<EmailEditorSidebar
heading={<h1>{faker.lorem.words(3)}</h1>}
emailTranslation={emailTranslation}
linkBackTo={`/${faker.lorem.word()}`}
/>,
)
const link: HTMLAnchorElement = baseElement.querySelector('.back-link') as any
Expand All @@ -30,7 +31,11 @@ describe('EmailEditorSidebar', () => {
it('displays the given heading', () => {
const title = faker.lorem.words(3)
const { baseElement } = render(
<EmailEditorSidebar emailTranslation={emailTranslation} heading={<h1>{title}</h1>} />,
<EmailEditorSidebar
emailTranslation={emailTranslation}
heading={<h1>{title}</h1>}
linkBackTo={`/${faker.lorem.word()}`}
/>,
)
expect(baseElement).toContainHTML(`<h1>${title}</h1>`)
})
Expand All @@ -52,6 +57,7 @@ describe('EmailEditorSidebar', () => {
<EmailEditorSidebar
emailTranslation={emailTranslation}
heading={<h1>{faker.lorem.words(3)}</h1>}
linkBackTo={`/${faker.lorem.word()}`}
/>,
)

Expand All @@ -70,6 +76,7 @@ describe('EmailEditorSidebar', () => {
<EmailEditorSidebar
emailTranslation={buildEmailTranslation({ language: 'not-set' })}
heading={<h1>{faker.lorem.words(3)}</h1>}
linkBackTo={`/${faker.lorem.word()}`}
/>,
)

Expand Down
5 changes: 3 additions & 2 deletions src/templates/EmailEditorSidebar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import { EmailEditorSidebarAccordion } from './EmailEditorSidebarAccordion'
interface Props {
heading: ReactElement
emailTranslation: EmailTranslation.Unique
linkBackTo: string
}

export const EmailEditorSidebar: FC<Props> = ({ heading, emailTranslation }) => {
export const EmailEditorSidebar: FC<Props> = ({ heading, emailTranslation, linkBackTo }) => {
const components = emailTranslation.components

return (
<Sidebar id="sidebar-container" className="email-editor-sidebar">
<SpacedSidebarContainer>
<BackLink />
<BackLink to={linkBackTo} />
<SkipNavContent />
{heading}
</SpacedSidebarContainer>
Expand Down

0 comments on commit 95dcc1a

Please sign in to comment.