utils.spec.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { describe, expect, it } from 'vitest';
  2. import { convertRelationPaths, isEmailAddressLike, normalizeEmailAddress } from './utils';
  3. describe('convertRelationPaths()', () => {
  4. it('undefined', () => {
  5. const result = convertRelationPaths<any>(undefined);
  6. expect(result).toEqual(undefined);
  7. });
  8. it('null', () => {
  9. const result = convertRelationPaths<any>(null);
  10. expect(result).toEqual(undefined);
  11. });
  12. it('single relation', () => {
  13. const result = convertRelationPaths<any>(['a']);
  14. expect(result).toEqual({
  15. a: true,
  16. });
  17. });
  18. it('flat list', () => {
  19. const result = convertRelationPaths<any>(['a', 'b', 'c']);
  20. expect(result).toEqual({
  21. a: true,
  22. b: true,
  23. c: true,
  24. });
  25. });
  26. it('three-level nested', () => {
  27. const result = convertRelationPaths<any>(['a', 'b.c', 'd.e.f']);
  28. expect(result).toEqual({
  29. a: true,
  30. b: {
  31. c: true,
  32. },
  33. d: {
  34. e: {
  35. f: true,
  36. },
  37. },
  38. });
  39. });
  40. });
  41. describe('normalizeEmailAddress()', () => {
  42. it('should trim whitespace', () => {
  43. expect(normalizeEmailAddress(' test@test.com ')).toBe('test@test.com');
  44. });
  45. it('should lowercase email addresses', async () => {
  46. expect(normalizeEmailAddress('JoeSmith@test.com')).toBe('joesmith@test.com');
  47. expect(normalizeEmailAddress('TEST@TEST.COM')).toBe('test@test.com');
  48. expect(normalizeEmailAddress('test.person@TEST.COM')).toBe('test.person@test.com');
  49. expect(normalizeEmailAddress('test.person+Extra@TEST.COM')).toBe('test.person+extra@test.com');
  50. expect(normalizeEmailAddress('TEST-person+Extra@TEST.COM')).toBe('test-person+extra@test.com');
  51. expect(normalizeEmailAddress('我買@屋企.香港')).toBe('我買@屋企.香港');
  52. });
  53. it('ignores surrounding whitespace', async () => {
  54. expect(normalizeEmailAddress(' JoeSmith@test.com')).toBe('joesmith@test.com');
  55. expect(normalizeEmailAddress('TEST@TEST.COM ')).toBe('test@test.com');
  56. expect(normalizeEmailAddress(' test.person@TEST.COM ')).toBe('test.person@test.com');
  57. });
  58. it('should not lowercase non-email address identifiers', async () => {
  59. expect(normalizeEmailAddress('Test')).toBe('Test');
  60. expect(normalizeEmailAddress('Ucj30Da2.!3rAA')).toBe('Ucj30Da2.!3rAA');
  61. });
  62. });
  63. describe('isEmailAddressLike()', () => {
  64. it('returns true for valid email addresses', () => {
  65. expect(isEmailAddressLike('simple@example.com')).toBe(true);
  66. expect(isEmailAddressLike('very.common@example.com')).toBe(true);
  67. expect(isEmailAddressLike('abc@example.co.uk')).toBe(true);
  68. expect(isEmailAddressLike('disposable.style.email.with+symbol@example.com')).toBe(true);
  69. expect(isEmailAddressLike('other.email-with-hyphen@example.com')).toBe(true);
  70. expect(isEmailAddressLike('fully-qualified-domain@example.com')).toBe(true);
  71. expect(isEmailAddressLike('user.name+tag+sorting@example.com')).toBe(true);
  72. expect(isEmailAddressLike('example-indeed@strange-example.com')).toBe(true);
  73. expect(isEmailAddressLike('example-indeed@strange-example.inininini')).toBe(true);
  74. });
  75. it('ignores surrounding whitespace', () => {
  76. expect(isEmailAddressLike(' simple@example.com')).toBe(true);
  77. expect(isEmailAddressLike('very.common@example.com ')).toBe(true);
  78. expect(isEmailAddressLike(' abc@example.co.uk ')).toBe(true);
  79. });
  80. it('returns false for invalid email addresses', () => {
  81. expect(isEmailAddressLike('username')).toBe(false);
  82. expect(isEmailAddressLike('823@ee28qje')).toBe(false);
  83. expect(isEmailAddressLike('Abc.example.com')).toBe(false);
  84. expect(isEmailAddressLike('A@b@')).toBe(false);
  85. });
  86. });