Skip to content

Commit

Permalink
Add unit tests for stripDomainPrefixes function
Browse files Browse the repository at this point in the history
  • Loading branch information
amitraj2203 committed May 30, 2024
1 parent 2802679 commit 2860362
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions packages/url/src/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
prependHTTPS,
removeQueryArgs,
safeDecodeURI,
stripDomainPrefixes,
} from '../';
import wptData from './fixtures/wpt-data';

Expand Down Expand Up @@ -127,6 +128,68 @@ describe( 'isValidProtocol', () => {
} );
} );

describe( 'stripDomainPrefixes', () => {
it( 'should remove the protocol (http, https, file, tel, etc.) from the URL', () => {
expect( stripDomainPrefixes( 'http://wordpress.org' ) ).toBe(
'wordpress.org'
);
expect( stripDomainPrefixes( 'https://wordpress.org' ) ).toBe(
'wordpress.org'
);
expect( stripDomainPrefixes( 'https://localhost:8080' ) ).toBe(
'localhost:8080'
);
expect( stripDomainPrefixes( 'file:///folder/file.txt' ) ).toBe(
'/folder/file.txt'
);
expect( stripDomainPrefixes( 'ftp://wordpress.org' ) ).toBe(
'wordpress.org'
);
expect( stripDomainPrefixes( 'tel:0123456789' ) ).toBe( '0123456789' );
expect( stripDomainPrefixes( 'blob:data' ) ).toBe( 'data' );
} );

it( 'should remove both the protocol and www prefix if both are present', () => {
expect( stripDomainPrefixes( 'http://www.google.com' ) ).toBe(
'google.com'
);
expect( stripDomainPrefixes( 'https://www.google.com' ) ).toBe(
'google.com'
);
expect( stripDomainPrefixes( 'ftp://www.google.com' ) ).toBe(
'google.com'
);
} );

it( 'should handle URLs without a protocol', () => {
expect( stripDomainPrefixes( 'www.example.com' ) ).toBe(
'example.com'
);
expect( stripDomainPrefixes( 'example.com' ) ).toBe( 'example.com' );
expect( stripDomainPrefixes( 'example.com/path' ) ).toBe(
'example.com/path'
);
} );

it( 'should handle URLs with both parameters and fragments correctly', () => {
expect(
stripDomainPrefixes(
'https://user:[email protected]:1020/test-path/file.extension#anchor?query=params&more'
)
).toBe(
'user:[email protected]:1020/test-path/file.extension#anchor?query=params&more'
);
expect( stripDomainPrefixes( 'https://www.example.com#section' ) ).toBe(
'example.com#section'
);
expect(
stripDomainPrefixes(
'https://www.example.com?param1=value1&param2=value2'
)
).toBe( 'example.com?param1=value1&param2=value2' );
} );
} );

describe( 'getAuthority', () => {
it( 'returns the authority part of a URL', () => {
expect(
Expand Down

0 comments on commit 2860362

Please sign in to comment.