Skip to content

Commit

Permalink
feat: show back buttons below 800px
Browse files Browse the repository at this point in the history
  • Loading branch information
lemald committed Nov 28, 2023
1 parent b5b5819 commit a7d5dbd
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 20 deletions.
5 changes: 4 additions & 1 deletion assets/src/components/viewHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ const ViewHeader: ViewHeaderType = ({
}): JSX.Element => {
const deviceType = useScreenSize()

const screenSizeAllowsBackButton =
deviceType === "mobile" || deviceType === "mobile_landscape_tablet_portrait"

return (
<div className="c-view-header">
{backlinkToView &&
backlinkToView !== OpenView.None &&
deviceType === "mobile" ? (
screenSizeAllowsBackButton ? (
<button
className="c-view-header__backlink"
onClick={followBacklink}
Expand Down
66 changes: 47 additions & 19 deletions assets/tests/components/viewHeader.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { jest, describe, test, expect } from "@jest/globals"
import "@testing-library/jest-dom/jest-globals"
import React from "react"
import { render } from "@testing-library/react"
import ViewHeader from "../../src/components/viewHeader"
import userEvent from "@testing-library/user-event"
import useScreenSize from "../../src/hooks/useScreenSize"
import { OpenView } from "../../src/state/pagePanelState"
import { DeviceType } from "../../src/skate"

jest.mock("../../src/hooks/useScreenSize", () => ({
__esModule: true,
Expand All @@ -23,24 +25,50 @@ describe("ViewHeader", () => {
expect(close).toHaveBeenCalled()
})

test("backlink doesn't render on non-mobile breakpoints", () => {
const close = jest.fn()
const followBacklink = jest.fn()

const result = render(
<ViewHeader
title="My View"
closeView={close}
backlinkToView={OpenView.Swings}
followBacklink={followBacklink}
/>
)

expect(result.queryByTitle("Swings")).toBeNull()
})
test.each<DeviceType>(["mobile", "mobile_landscape_tablet_portrait"])(
"backlink renders on %s breakpoint",
(breakpoint) => {
jest.mocked(useScreenSize).mockReturnValueOnce(breakpoint)

const close = jest.fn()
const followBacklink = jest.fn()

const result = render(
<ViewHeader
title="My View"
closeView={close}
backlinkToView={OpenView.Swings}
followBacklink={followBacklink}
/>
)

expect(result.getByTitle("Swings")).toBeInTheDocument()
}
)

test.each<DeviceType>(["tablet", "desktop"])(
"backlink doesn't render on %s breakpoint",
(breakpoint) => {
jest.mocked(useScreenSize).mockReturnValueOnce(breakpoint)

const close = jest.fn()
const followBacklink = jest.fn()

const result = render(
<ViewHeader
title="My View"
closeView={close}
backlinkToView={OpenView.Swings}
followBacklink={followBacklink}
/>
)

expect(result.queryByTitle("Swings")).toBeNull()
}
)

test("backlink doesn't render when there's no view to return to", () => {
;(useScreenSize as jest.Mock).mockImplementationOnce(() => "mobile")
jest.mocked(useScreenSize).mockImplementationOnce(() => "mobile")

const close = jest.fn()
const followBacklink = jest.fn()
Expand All @@ -58,7 +86,7 @@ describe("ViewHeader", () => {
})

test("backlink button invokes callback", async () => {
;(useScreenSize as jest.Mock).mockImplementationOnce(() => "mobile")
jest.mocked(useScreenSize).mockImplementationOnce(() => "mobile")

const close = jest.fn()
const followBacklink = jest.fn()
Expand All @@ -79,7 +107,7 @@ describe("ViewHeader", () => {
})

test("backlink button renders for Late View", async () => {
;(useScreenSize as jest.Mock).mockImplementationOnce(() => "mobile")
jest.mocked(useScreenSize).mockImplementationOnce(() => "mobile")

const close = jest.fn()
const followBacklink = jest.fn()
Expand All @@ -97,7 +125,7 @@ describe("ViewHeader", () => {
})

test("backlink button render for notifications drawer", async () => {
;(useScreenSize as jest.Mock).mockImplementationOnce(() => "mobile")
jest.mocked(useScreenSize).mockImplementationOnce(() => "mobile")

const close = jest.fn()
const followBacklink = jest.fn()
Expand Down

0 comments on commit a7d5dbd

Please sign in to comment.