| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- import {expect} from 'chai';
- import {joinPath} from './join-path.js';
- describe('joinPath', function () {
- it('should join multiple segments with a forward slash', function () {
- const result = joinPath('api', 'v1', 'users');
- expect(result).to.be.eq('/api/v1/users');
- });
- it('should always return a path starting with a single forward slash', function () {
- expect(joinPath('users')).to.be.eq('/users');
- expect(joinPath('/users')).to.be.eq('/users');
- });
- it('should remove leading slashes from segments', function () {
- const result = joinPath('/api', '/v1', '/users');
- expect(result).to.be.eq('/api/v1/users');
- });
- it('should remove trailing slashes from segments', function () {
- const result = joinPath('api/', 'v1/', 'users/');
- expect(result).to.be.eq('/api/v1/users');
- });
- it('should handle segments with both leading and trailing slashes', function () {
- const result = joinPath('/api/', '/v1/', '/users/');
- expect(result).to.be.eq('/api/v1/users');
- });
- it('should ignore empty string segments', function () {
- const result = joinPath('api', '', 'v1', '', 'users');
- expect(result).to.be.eq('/api/v1/users');
- });
- it('should ignore null and undefined segments', function () {
- const result = joinPath('api', null, 'v1', undefined, 'users');
- expect(result).to.be.eq('/api/v1/users');
- });
- it('should handle a single segment correctly', function () {
- expect(joinPath('users')).to.be.eq('/users');
- expect(joinPath('/users/')).to.be.eq('/users');
- });
- it('should return a single slash when no segments are provided', function () {
- const result = joinPath();
- expect(result).to.be.eq('/');
- });
- it('should return a single slash if all segments are empty or falsy', function () {
- const result = joinPath('', null, '', undefined);
- expect(result).to.be.eq('/');
- });
- it('should treat a single slash segment as an empty segment', function () {
- const result = joinPath('api', '/', 'users');
- expect(result).to.be.eq('/api/users');
- });
- it('should not add extra slashes if segments are already joined', function () {
- const result = joinPath('api/v1', 'users');
- expect(result).to.be.eq('/api/v1/users');
- });
- it('should convert number arguments to string segments', function () {
- const result = joinPath(-10, 0, 10);
- expect(result).to.be.eq('/-10/0/10');
- });
- });
|