|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | + |
| 3 | +import { appendCacheTag } from './appendCacheTag.js' |
| 4 | + |
| 5 | +describe('appendCacheTag', () => { |
| 6 | + it('should return the url unchanged when cacheTag is undefined', () => { |
| 7 | + expect(appendCacheTag('https://example.com/image.jpg', undefined)).toBe( |
| 8 | + 'https://example.com/image.jpg', |
| 9 | + ) |
| 10 | + }) |
| 11 | + |
| 12 | + it('should return the url unchanged when cacheTag is false', () => { |
| 13 | + expect(appendCacheTag('https://example.com/image.jpg', false)).toBe( |
| 14 | + 'https://example.com/image.jpg', |
| 15 | + ) |
| 16 | + }) |
| 17 | + |
| 18 | + it('should return the url unchanged when cacheTag is empty string', () => { |
| 19 | + expect(appendCacheTag('https://example.com/image.jpg', '')).toBe( |
| 20 | + 'https://example.com/image.jpg', |
| 21 | + ) |
| 22 | + }) |
| 23 | + |
| 24 | + it('should append the cache tag with ? when the url has no query string', () => { |
| 25 | + const result = appendCacheTag('https://example.com/image.jpg', '2024-01-01T00:00:00.000Z') |
| 26 | + expect(result).toBe('https://example.com/image.jpg?2024-01-01T00%3A00%3A00.000Z') |
| 27 | + }) |
| 28 | + |
| 29 | + it('should append the cache tag with & when the url already has a query string', () => { |
| 30 | + const result = appendCacheTag( |
| 31 | + 'https://example.com/image.jpg?w=800&q=75', |
| 32 | + '2024-01-01T00:00:00.000Z', |
| 33 | + ) |
| 34 | + expect(result).toBe('https://example.com/image.jpg?w=800&q=75&2024-01-01T00%3A00%3A00.000Z') |
| 35 | + }) |
| 36 | + |
| 37 | + it('should URI-encode the cache tag value', () => { |
| 38 | + const result = appendCacheTag('/image.jpg', '2024-06-15T12:30:00.000Z') |
| 39 | + expect(result).toBe('/image.jpg?2024-06-15T12%3A30%3A00.000Z') |
| 40 | + }) |
| 41 | + |
| 42 | + it('should work with relative urls', () => { |
| 43 | + const result = appendCacheTag('/uploads/photo.png', '2024-01-01T00:00:00.000Z') |
| 44 | + expect(result).toBe('/uploads/photo.png?2024-01-01T00%3A00%3A00.000Z') |
| 45 | + }) |
| 46 | + |
| 47 | + it('should work with relative urls that already have a query string', () => { |
| 48 | + const result = appendCacheTag('/uploads/photo.png?size=large', '2024-01-01T00:00:00.000Z') |
| 49 | + expect(result).toBe('/uploads/photo.png?size=large&2024-01-01T00%3A00%3A00.000Z') |
| 50 | + }) |
| 51 | +}) |
0 commit comments