1 import React from "react"
2 import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"
3 import { cleanup, render, waitFor } from "@testing-library/react"
4 import { MobileProvider, useMobile } from "../index"
5
6 const TestComponent = () => {
7 const { isMobile } = useMobile()
8 return <div data-testid="mobile">{isMobile ? "mobile" : "desktop"}</div>
9 }
10
11 beforeEach(() => {
12 vi.clearAllMocks()
13 // Reset window.innerWidth
14 Object.defineProperty(window, "innerWidth", {
15 writable: true,
16 configurable: true,
17 value: 1024,
18 })
19 })
20
21 afterEach(() => {
22 cleanup()
23 })
24
25 describe("rendering", () => {
26 test("renders children", () => {
27 const { container } = render(
28 <MobileProvider>
29 <div>Test</div>
30 </MobileProvider>
31 )
32 expect(container).toHaveTextContent("Test")
33 })
34 })
35
36 describe("useMobile hook", () => {
37 test("detects desktop when window width >= 1024", async () => {
38 Object.defineProperty(window, "innerWidth", {
39 writable: true,
40 configurable: true,
41 value: 1024,
42 })
43
44 const { getByTestId } = render(
45 <MobileProvider>
46 <TestComponent />
47 </MobileProvider>
48 )
49
50 await waitFor(() => {
51 expect(getByTestId("mobile")).toHaveTextContent("desktop")
52 })
53 })
54
55 test("detects mobile when window width < 1024", async () => {
56 Object.defineProperty(window, "innerWidth", {
57 writable: true,
58 configurable: true,
59 value: 768,
60 })
61
62 const { getByTestId } = render(
63 <MobileProvider>
64 <TestComponent />
65 </MobileProvider>
66 )
67
68 await waitFor(() => {
69 expect(getByTestId("mobile")).toHaveTextContent("mobile")
70 })
71 })
72
73 test("updates when window is resized to mobile", async () => {
74 Object.defineProperty(window, "innerWidth", {
75 writable: true,
76 configurable: true,
77 value: 1024,
78 })
79
80 const { getByTestId } = render(
81 <MobileProvider>
82 <TestComponent />
83 </MobileProvider>
84 )
85
86 await waitFor(() => {
87 expect(getByTestId("mobile")).toHaveTextContent("desktop")
88 })
89
90 // Simulate resize to mobile
91 Object.defineProperty(window, "innerWidth", {
92 writable: true,
93 configurable: true,
94 value: 768,
95 })
96
97 window.dispatchEvent(new Event("resize"))
98
99 await waitFor(() => {
100 expect(getByTestId("mobile")).toHaveTextContent("mobile")
101 })
102 })
103
104 test("updates when window is resized to desktop", async () => {
105 Object.defineProperty(window, "innerWidth", {
106 writable: true,
107 configurable: true,
108 value: 768,
109 })
110
111 const { getByTestId } = render(
112 <MobileProvider>
113 <TestComponent />
114 </MobileProvider>
115 )
116
117 await waitFor(() => {
118 expect(getByTestId("mobile")).toHaveTextContent("mobile")
119 })
120
121 // Simulate resize to desktop
122 Object.defineProperty(window, "innerWidth", {
123 writable: true,
124 configurable: true,
125 value: 1024,
126 })
127
128 window.dispatchEvent(new Event("resize"))
129
130 await waitFor(() => {
131 expect(getByTestId("mobile")).toHaveTextContent("desktop")
132 })
133 })
134
135 test("throws error when used outside provider", () => {
136 const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {})
137
138 expect(() => {
139 render(<TestComponent />)
140 }).toThrow("useMobile must be used within a MobileProvider")
141
142 consoleSpy.mockRestore()
143 })
144 })