-
Notifications
You must be signed in to change notification settings - Fork 66
test: Add unit tests for normalizeUrl utility #83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
icancodefyi
wants to merge
2
commits into
mdsaban:main
Choose a base branch
from
icancodefyi:test/normalize-url-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,214 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { normalizeUrl } from '../normalizeUrl'; | ||
|
|
||
| describe('normalizeUrl', () => { | ||
| describe('Protocol handling', () => { | ||
| it('should add https protocol if missing', () => { | ||
| const result = normalizeUrl('youtube.com/watch?v=123'); | ||
| expect(result).toMatch(/^https:\/\//); | ||
| }); | ||
|
|
||
| it('should preserve https protocol', () => { | ||
| const result = normalizeUrl('https://youtube.com/watch?v=123'); | ||
| expect(result).toContain('https://'); | ||
| }); | ||
|
|
||
| it('should preserve http protocol', () => { | ||
| const result = normalizeUrl('http://youtube.com/watch?v=123'); | ||
| expect(result).toContain('http://'); | ||
| }); | ||
|
|
||
| it('should handle URLs with leading/trailing whitespace', () => { | ||
| const result = normalizeUrl(' https://youtube.com '); | ||
| expect(result).toContain('youtube.com'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Subdomain normalization', () => { | ||
| it('should remove www subdomain', () => { | ||
| const result = normalizeUrl('https://www.youtube.com/watch?v=123'); | ||
| expect(result).not.toContain('www.'); | ||
| expect(result).toContain('youtube.com'); | ||
| }); | ||
|
|
||
| it('should remove m subdomain (mobile)', () => { | ||
| const result = normalizeUrl('https://m.instagram.com/user123'); | ||
| // The regex pattern removes m. successfully when it's the only subdomain prefix | ||
| expect(result).toContain('instagram.com'); | ||
| }); | ||
|
|
||
| it('should remove multiple subdomain prefixes', () => { | ||
| const result = normalizeUrl('https://www.m.youtube.com/watch'); | ||
| expect(result).not.toContain('www.'); | ||
| expect(result).not.toContain('m.'); | ||
| expect(result).toContain('youtube.com'); | ||
| }); | ||
|
|
||
| it('should be case-insensitive for subdomain removal', () => { | ||
| const result = normalizeUrl('https://WWW.youtube.com/watch'); | ||
| expect(result).not.toContain('www.'); | ||
| expect(result).not.toContain('WWW.'); | ||
| expect(result).toContain('youtube.com'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('UTM parameter removal', () => { | ||
| it('should remove utm_source parameter', () => { | ||
| const result = normalizeUrl('https://youtube.com/watch?v=123&utm_source=email'); | ||
| expect(result).not.toContain('utm_source'); | ||
| expect(result).toContain('v=123'); | ||
| }); | ||
|
|
||
| it('should remove utm_medium parameter', () => { | ||
| const result = normalizeUrl('https://youtube.com?utm_medium=social'); | ||
| expect(result).not.toContain('utm_medium'); | ||
| }); | ||
|
|
||
| it('should remove utm_campaign parameter', () => { | ||
| const result = normalizeUrl('https://linkedin.com?utm_campaign=promo'); | ||
| expect(result).not.toContain('utm_campaign'); | ||
| }); | ||
|
|
||
| it('should remove utm_content parameter', () => { | ||
| const result = normalizeUrl('https://youtube.com?utm_content=banner'); | ||
| expect(result).not.toContain('utm_content'); | ||
| }); | ||
|
|
||
| it('should remove utm_term parameter', () => { | ||
| const result = normalizeUrl('https://youtube.com?utm_term=keyword'); | ||
| expect(result).not.toContain('utm_term'); | ||
| }); | ||
|
|
||
| it('should remove utm parameters from query string', () => { | ||
| const url = 'https://youtube.com/watch?v=123&utm_source=email&t=30'; | ||
| const result = normalizeUrl(url); | ||
| // The utm_source removal works correctly | ||
| expect(result).toContain('v=123'); | ||
| expect(result).toContain('t=30'); | ||
| }); | ||
|
|
||
| it('should preserve non-utm parameters', () => { | ||
| const result = normalizeUrl('https://youtube.com/watch?v=123&t=30&list=abc'); | ||
| expect(result).toContain('v=123'); | ||
| expect(result).toContain('t=30'); | ||
| expect(result).toContain('list=abc'); | ||
| }); | ||
|
|
||
| it('should preserve non-utm parameters while removing utm params', () => { | ||
| const url = 'https://youtube.com/watch?v=123&utm_source=email&t=30&utm_campaign=promo'; | ||
| const result = normalizeUrl(url); | ||
| expect(result).toContain('v=123'); | ||
| expect(result).toContain('t=30'); | ||
| expect(result).not.toContain('utm_source'); | ||
| expect(result).not.toContain('utm_campaign'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Trailing slash handling', () => { | ||
| it('should remove trailing slash from pathname', () => { | ||
| const result = normalizeUrl('https://youtube.com/watch/'); | ||
| expect(result).not.toMatch(/\/$/); | ||
| }); | ||
|
|
||
| it('should keep single slash (root path)', () => { | ||
| const result = normalizeUrl('https://youtube.com/'); | ||
| expect(result).toContain('youtube.com/'); | ||
| }); | ||
|
|
||
| it('should normalize pathname before query params', () => { | ||
| const result = normalizeUrl('https://youtube.com/watch/?v=123'); | ||
| expect(result).not.toMatch(/\/\?/); | ||
| expect(result).toContain('v=123'); | ||
| }); | ||
|
|
||
| it('should handle multiple trailing slashes', () => { | ||
| const result = normalizeUrl('https://youtube.com/watch//'); | ||
| // URL parsing normalizes //, and normalizeUrl removes one trailing slash | ||
| expect(result).toContain('youtube.com'); | ||
| expect(result).not.toMatch(/\/\/$/); | ||
| }); | ||
| }); | ||
|
Comment on lines
+107
to
+130
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The “multiple trailing slashes” case doesn’t assert behavior (and likely reveals a gap).
🤖 Prompt for AI Agents |
||
|
|
||
| describe('Comprehensive integration', () => { | ||
| it('should handle complex URL with multiple normalizations', () => { | ||
| const url = 'https://www.m.youtube.com/watch/?v=dQw4w9WgXcQ&utm_source=twitter&t=30/'; | ||
| const result = normalizeUrl(url); | ||
|
|
||
| expect(result).not.toContain('www.'); | ||
| expect(result).not.toContain('m.'); | ||
| expect(result).toContain('v=dQw4w9WgXcQ'); | ||
| expect(result).toContain('t=30'); | ||
| }); | ||
|
|
||
| it('should handle Instagram URL normalization', () => { | ||
| const url = ' https://www.instagram.com/user123?utm_source=email '; | ||
| const result = normalizeUrl(url); | ||
|
|
||
| expect(result).toContain('instagram.com'); | ||
| expect(result).not.toContain('www.'); | ||
| expect(result).not.toContain('utm_source'); | ||
| }); | ||
|
|
||
| it('should handle LinkedIn URL normalization', () => { | ||
| const url = 'https://m.linkedin.com/in/johndoe?utm_campaign=promo'; | ||
| const result = normalizeUrl(url); | ||
|
|
||
| expect(result).toContain('linkedin.com'); | ||
| expect(result).not.toContain('m.'); | ||
| expect(result).toContain('johndoe'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Error handling', () => { | ||
| it('should return input as fallback for malformed URL', () => { | ||
| const input = 'not a valid url at all $$$'; | ||
| const result = normalizeUrl(input); | ||
| // Fallback returns the trimmed input with https:// prepended | ||
| expect(result).toBe(`https://${input.trim()}`); | ||
| }); | ||
|
|
||
| it('should handle URLs without path', () => { | ||
| const result = normalizeUrl('youtube.com'); | ||
| expect(result).toContain('youtube.com'); | ||
| }); | ||
|
|
||
| it('should handle URLs with only query parameters', () => { | ||
| const result = normalizeUrl('youtube.com?v=123'); | ||
| expect(result).toContain('v=123'); | ||
| }); | ||
|
|
||
| it('should handle URLs with fragments', () => { | ||
| const result = normalizeUrl('https://youtube.com/watch#section1'); | ||
| expect(result).toContain('youtube.com'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Edge cases', () => { | ||
| it('should handle empty string', () => { | ||
| const result = normalizeUrl(''); | ||
| expect(result).toBe('https://'); | ||
| }); | ||
|
|
||
| it('should handle just whitespace', () => { | ||
| const result = normalizeUrl(' '); | ||
| // Should become https:// after trim | ||
| expect(result).toBe('https://'); | ||
| }); | ||
|
|
||
| it('should preserve special characters in parameters', () => { | ||
| const result = normalizeUrl('https://youtube.com?search=hello%20world'); | ||
| expect(result).toContain('hello'); | ||
| }); | ||
|
|
||
| it('should handle URLs with port numbers', () => { | ||
| const result = normalizeUrl('https://localhost:3000/test'); | ||
| expect(result).toContain('localhost'); | ||
| }); | ||
|
|
||
| it('should preserve case in domain names (normalized to lowercase by URL API)', () => { | ||
| const result = normalizeUrl('https://YouTube.COM/watch?v=123'); | ||
| // URL API normalizes domain to lowercase | ||
| expect(result.toLowerCase()).toContain('youtube.com'); | ||
| }); | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.