inference.test.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { describe, expect, it } from 'vitest';
  2. import {
  3. isEmpty,
  4. isHttpUrl,
  5. isObject,
  6. isUndefined,
  7. isWindow,
  8. } from './inference';
  9. describe('isHttpUrl', () => {
  10. it("should return true when given 'http://example.com'", () => {
  11. expect(isHttpUrl('http://example.com')).toBe(true);
  12. });
  13. it("should return true when given 'https://example.com'", () => {
  14. expect(isHttpUrl('https://example.com')).toBe(true);
  15. });
  16. it("should return false when given 'ftp://example.com'", () => {
  17. expect(isHttpUrl('ftp://example.com')).toBe(false);
  18. });
  19. it("should return false when given 'example.com'", () => {
  20. expect(isHttpUrl('example.com')).toBe(false);
  21. });
  22. });
  23. describe('isUndefined', () => {
  24. it('isUndefined should return true for undefined values', () => {
  25. expect(isUndefined()).toBe(true);
  26. });
  27. it('isUndefined should return false for null values', () => {
  28. expect(isUndefined(null)).toBe(false);
  29. });
  30. it('isUndefined should return false for defined values', () => {
  31. expect(isUndefined(0)).toBe(false);
  32. expect(isUndefined('')).toBe(false);
  33. expect(isUndefined(false)).toBe(false);
  34. });
  35. it('isUndefined should return false for objects and arrays', () => {
  36. expect(isUndefined({})).toBe(false);
  37. expect(isUndefined([])).toBe(false);
  38. });
  39. });
  40. describe('isEmpty', () => {
  41. it('should return true for empty string', () => {
  42. expect(isEmpty('')).toBe(true);
  43. });
  44. it('should return true for empty array', () => {
  45. expect(isEmpty([])).toBe(true);
  46. });
  47. it('should return true for empty object', () => {
  48. expect(isEmpty({})).toBe(true);
  49. });
  50. it('should return false for non-empty string', () => {
  51. expect(isEmpty('hello')).toBe(false);
  52. });
  53. it('should return false for non-empty array', () => {
  54. expect(isEmpty([1, 2, 3])).toBe(false);
  55. });
  56. it('should return false for non-empty object', () => {
  57. expect(isEmpty({ a: 1 })).toBe(false);
  58. });
  59. it('should return true for null or undefined', () => {
  60. expect(isEmpty(null)).toBe(true);
  61. expect(isEmpty()).toBe(true);
  62. });
  63. it('should return false for number or boolean', () => {
  64. expect(isEmpty(0)).toBe(false);
  65. expect(isEmpty(true)).toBe(false);
  66. });
  67. });
  68. describe('isWindow', () => {
  69. it('should return true for the window object', () => {
  70. expect(isWindow(window)).toBe(true);
  71. });
  72. it('should return false for other objects', () => {
  73. expect(isWindow({})).toBe(false);
  74. expect(isWindow([])).toBe(false);
  75. expect(isWindow(null)).toBe(false);
  76. });
  77. });
  78. describe('isObject', () => {
  79. it('should return true for objects', () => {
  80. expect(isObject({})).toBe(true);
  81. expect(isObject({ a: 1 })).toBe(true);
  82. });
  83. it('should return false for non-objects', () => {
  84. expect(isObject(null)).toBe(false);
  85. expect(isObject()).toBe(false);
  86. expect(isObject(42)).toBe(false);
  87. expect(isObject('string')).toBe(false);
  88. expect(isObject(true)).toBe(false);
  89. expect(isObject([1, 2, 3])).toBe(true);
  90. expect(isObject(new Date())).toBe(true);
  91. expect(isObject(/regex/)).toBe(true);
  92. });
  93. });