join-path.spec.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import {expect} from 'chai';
  2. import {joinPath} from './join-path.js';
  3. describe('joinPath', function () {
  4. it('should join multiple segments with a forward slash', function () {
  5. const result = joinPath('api', 'v1', 'users');
  6. expect(result).to.be.eq('/api/v1/users');
  7. });
  8. it('should always return a path starting with a single forward slash', function () {
  9. expect(joinPath('users')).to.be.eq('/users');
  10. expect(joinPath('/users')).to.be.eq('/users');
  11. });
  12. it('should remove leading slashes from segments', function () {
  13. const result = joinPath('/api', '/v1', '/users');
  14. expect(result).to.be.eq('/api/v1/users');
  15. });
  16. it('should remove trailing slashes from segments', function () {
  17. const result = joinPath('api/', 'v1/', 'users/');
  18. expect(result).to.be.eq('/api/v1/users');
  19. });
  20. it('should handle segments with both leading and trailing slashes', function () {
  21. const result = joinPath('/api/', '/v1/', '/users/');
  22. expect(result).to.be.eq('/api/v1/users');
  23. });
  24. it('should ignore empty string segments', function () {
  25. const result = joinPath('api', '', 'v1', '', 'users');
  26. expect(result).to.be.eq('/api/v1/users');
  27. });
  28. it('should ignore null and undefined segments', function () {
  29. const result = joinPath('api', null, 'v1', undefined, 'users');
  30. expect(result).to.be.eq('/api/v1/users');
  31. });
  32. it('should handle a single segment correctly', function () {
  33. expect(joinPath('users')).to.be.eq('/users');
  34. expect(joinPath('/users/')).to.be.eq('/users');
  35. });
  36. it('should return a single slash when no segments are provided', function () {
  37. const result = joinPath();
  38. expect(result).to.be.eq('/');
  39. });
  40. it('should return a single slash if all segments are empty or falsy', function () {
  41. const result = joinPath('', null, '', undefined);
  42. expect(result).to.be.eq('/');
  43. });
  44. it('should treat a single slash segment as an empty segment', function () {
  45. const result = joinPath('api', '/', 'users');
  46. expect(result).to.be.eq('/api/users');
  47. });
  48. it('should not add extra slashes if segments are already joined', function () {
  49. const result = joinPath('api/v1', 'users');
  50. expect(result).to.be.eq('/api/v1/users');
  51. });
  52. it('should convert number arguments to string segments', function () {
  53. const result = joinPath(-10, 0, 10);
  54. expect(result).to.be.eq('/-10/0/10');
  55. });
  56. });