Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

use-breakpoints.spec.ts 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { act, renderHook } from '@testing-library/react'
  2. import useBreakpoints, { MediaType } from './use-breakpoints'
  3. describe('useBreakpoints', () => {
  4. const originalInnerWidth = window.innerWidth
  5. // Mock the window resize event
  6. const fireResize = (width: number) => {
  7. window.innerWidth = width
  8. act(() => {
  9. window.dispatchEvent(new Event('resize'))
  10. })
  11. }
  12. // Restore the original innerWidth after tests
  13. afterAll(() => {
  14. window.innerWidth = originalInnerWidth
  15. })
  16. it('should return mobile for width <= 640px', () => {
  17. // Mock window.innerWidth for mobile
  18. Object.defineProperty(window, 'innerWidth', {
  19. writable: true,
  20. configurable: true,
  21. value: 640,
  22. })
  23. const { result } = renderHook(() => useBreakpoints())
  24. expect(result.current).toBe(MediaType.mobile)
  25. })
  26. it('should return tablet for width > 640px and <= 768px', () => {
  27. // Mock window.innerWidth for tablet
  28. Object.defineProperty(window, 'innerWidth', {
  29. writable: true,
  30. configurable: true,
  31. value: 768,
  32. })
  33. const { result } = renderHook(() => useBreakpoints())
  34. expect(result.current).toBe(MediaType.tablet)
  35. })
  36. it('should return pc for width > 768px', () => {
  37. // Mock window.innerWidth for pc
  38. Object.defineProperty(window, 'innerWidth', {
  39. writable: true,
  40. configurable: true,
  41. value: 1024,
  42. })
  43. const { result } = renderHook(() => useBreakpoints())
  44. expect(result.current).toBe(MediaType.pc)
  45. })
  46. it('should update media type when window resizes', () => {
  47. // Start with desktop
  48. Object.defineProperty(window, 'innerWidth', {
  49. writable: true,
  50. configurable: true,
  51. value: 1024,
  52. })
  53. const { result } = renderHook(() => useBreakpoints())
  54. expect(result.current).toBe(MediaType.pc)
  55. // Resize to tablet
  56. fireResize(768)
  57. expect(result.current).toBe(MediaType.tablet)
  58. // Resize to mobile
  59. fireResize(600)
  60. expect(result.current).toBe(MediaType.mobile)
  61. })
  62. it('should clean up event listeners on unmount', () => {
  63. // Spy on addEventListener and removeEventListener
  64. const addEventListenerSpy = jest.spyOn(window, 'addEventListener')
  65. const removeEventListenerSpy = jest.spyOn(window, 'removeEventListener')
  66. const { unmount } = renderHook(() => useBreakpoints())
  67. // Unmount should trigger cleanup
  68. unmount()
  69. expect(addEventListenerSpy).toHaveBeenCalledWith('resize', expect.any(Function))
  70. expect(removeEventListenerSpy).toHaveBeenCalledWith('resize', expect.any(Function))
  71. // Clean up spies
  72. addEventListenerSpy.mockRestore()
  73. removeEventListenerSpy.mockRestore()
  74. })
  75. })