element.test.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
  3. * SPDX-License-Identifier: MIT
  4. */
  5. import { vi, describe, expect, it } from 'vitest';
  6. import { isHidden, isRectInit } from '../../src/utils/element';
  7. describe('test isHidden', () => {
  8. it('isHidden true', () => {
  9. vi.stubGlobal('getComputedStyle', () => ({
  10. display: 'none',
  11. }));
  12. const mockElement = {
  13. offsetParent: null,
  14. };
  15. const res = isHidden(mockElement as unknown as HTMLElement);
  16. expect(res).toEqual(true);
  17. });
  18. it('isHidden false', () => {
  19. vi.stubGlobal('getComputedStyle', () => ({
  20. display: 'block',
  21. }));
  22. const mockElement1 = {
  23. offsetParent: true,
  24. };
  25. const res = isHidden(mockElement1 as unknown as HTMLElement);
  26. expect(res).toEqual(false);
  27. });
  28. });
  29. describe('isRectInit', () => {
  30. it('should return false when input is undefined', () => {
  31. expect(isRectInit(undefined)).toBe(false);
  32. });
  33. it('should return false when all properties are 0', () => {
  34. const emptyRect: DOMRect = {
  35. bottom: 0,
  36. height: 0,
  37. left: 0,
  38. right: 0,
  39. top: 0,
  40. width: 0,
  41. x: 0,
  42. y: 0,
  43. toJSON: () => ({}),
  44. };
  45. expect(isRectInit(emptyRect)).toBe(false);
  46. });
  47. it('should return true when any property is not 0', () => {
  48. const validRect: DOMRect = {
  49. bottom: 100,
  50. height: 100,
  51. left: 0,
  52. right: 100,
  53. top: 0,
  54. width: 100,
  55. x: 0,
  56. y: 0,
  57. toJSON: () => ({}),
  58. };
  59. expect(isRectInit(validRect)).toBe(true);
  60. });
  61. });