non-empty-string.spec.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { describe, expect, it } from 'vitest';
  2. import { isNonEmptyString, toNonEmptyStringOrUndefined } from './non-empty-string';
  3. describe('isNonEmptyString', () => {
  4. /* eslint-disable indent */
  5. it.each`
  6. input | expected | description
  7. ${'hello'} | ${true} | ${'non-empty string'}
  8. ${'world'} | ${true} | ${'non-empty string'}
  9. ${'a'} | ${true} | ${'single character'}
  10. ${'1'} | ${true} | ${'numeric string'}
  11. ${' '} | ${true} | ${'space character'}
  12. ${' '} | ${true} | ${'multiple spaces'}
  13. ${''} | ${false} | ${'empty string'}
  14. ${null} | ${false} | ${'null'}
  15. ${undefined} | ${false} | ${'undefined'}
  16. `('should return $expected for $description: $input', ({ input, expected }) => {
  17. /* eslint-enable indent */
  18. expect(isNonEmptyString(input)).toBe(expected);
  19. });
  20. });
  21. describe('toNonEmptyStringOrUndefined', () => {
  22. /* eslint-disable indent */
  23. it.each`
  24. input | expected | description
  25. ${'hello'} | ${'hello'} | ${'non-empty string'}
  26. ${'world'} | ${'world'} | ${'non-empty string'}
  27. ${'a'} | ${'a'} | ${'single character'}
  28. ${'1'} | ${'1'} | ${'numeric string'}
  29. ${' '} | ${' '} | ${'space character'}
  30. ${' '} | ${' '} | ${'multiple spaces'}
  31. ${''} | ${undefined} | ${'empty string'}
  32. ${null} | ${undefined} | ${'null'}
  33. ${undefined} | ${undefined} | ${'undefined'}
  34. `('should return $expected for $description: $input', ({ input, expected }) => {
  35. /* eslint-enable indent */
  36. expect(toNonEmptyStringOrUndefined(input)).toBe(expected);
  37. });
  38. });
  39. describe('toNonEmptyStringOrUndefined type safety', () => {
  40. it('should maintain type safety with NonEmptyString brand', () => {
  41. const validString = 'test';
  42. const result = toNonEmptyStringOrUndefined(validString);
  43. expect(result).toBe(validString);
  44. if (result !== undefined) {
  45. const _typedResult: typeof result = validString as typeof result;
  46. expect(_typedResult).toBe(validString);
  47. }
  48. });
  49. });