Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,35 @@ jobs:
test:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18.x, 20.x]
permissions:
contents: read
pull-requests: write

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup Node.js ${{ matrix.node-version }}
- name: Setup Node.js 14.x
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
node-version: 14.x

- name: Install dependencies
run: npm ci
run: npm install

- name: Run tests
run: npm test

- name: Run tests with coverage
run: npm run test:coverage

- name: Coverage Report
uses: ArtiomTr/jest-coverage-report-action@v2
if: matrix.node-version == '20.x' && github.event_name == 'pull_request'
- name: Jest Coverage Comment
if: github.event_name == 'pull_request'
uses: MishaKav/jest-coverage-comment@main
with:
coverage-file: ./coverage/coverage-summary.json
base-coverage-file: ./coverage/coverage-summary.json
annotations: all
test-script: npm run test:coverage
coverage-summary-path: ./coverage/coverage-summary.json
title: Test Coverage
badge-title: Coverage
hide-comment: false
create-new-comment: false
hide-summary: false
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Dependencies
node_modules/
package-lock.json

# Test coverage
coverage/
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Template for creating and testing ImageKit URL Endpoint Functions with unit tests and GitHub Actions CI.

Uses Node 14.

## Quick Start

```bash
Expand Down Expand Up @@ -115,4 +117,4 @@ npm run test:coverage # With coverage

## CI/CD

GitHub Actions automatically runs tests on push/PR to `main` or `develop` branches. Tests run on Node.js 18.x and 20.x.
GitHub Actions automatically runs tests on push/PR to `main` or `develop` branches. Tests run on Node.js 14.x.
133 changes: 8 additions & 125 deletions handler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,130 +14,13 @@ describe('URL Endpoint Function Handler', () => {
}
};

describe('Default behavior (no-op)', () => {
it('should return the original URL unchanged', () => {
const url = 'https://ik.imagekit.io/demo/image.jpg';
const urlPrefix = 'demo';

const result = handler(url, urlPrefix, mockContext);

expect(result).toHaveProperty('url');
expect(result.url).toBe(url);
});

it('should set signURL to false by default', () => {
const url = 'https://ik.imagekit.io/demo/image.jpg';
const urlPrefix = 'demo';

const result = handler(url, urlPrefix, mockContext);

expect(result).toHaveProperty('signURL');
expect(result.signURL).toBe(false);
});

it('should handle URLs with query parameters', () => {
const url = 'https://ik.imagekit.io/demo/image.jpg?quality=80&format=webp';
const urlPrefix = 'demo';

const result = handler(url, urlPrefix, mockContext);

expect(result.url).toBe(url);
expect(result.signURL).toBe(false);
});

it('should handle URLs with transformation parameters', () => {
const url = 'https://ik.imagekit.io/demo/w_100/h_200/image.jpg';
const urlPrefix = 'demo';

const result = handler(url, urlPrefix, mockContext);

expect(result.url).toBe(url);
expect(result.signURL).toBe(false);
});

it('should work with different urlPrefix values', () => {
const url = 'https://ik.imagekit.io/demo/image.jpg';
const prefixes = ['img', 's3-bucket', 'web-folder', 'custom-prefix'];

prefixes.forEach(prefix => {
const result = handler(url, prefix, mockContext);
expect(result.url).toBe(url);
expect(result.signURL).toBe(false);
});
});

it('should work with different context values', () => {
const url = 'https://ik.imagekit.io/demo/image.jpg';
const urlPrefix = 'demo';

const contexts = [
{ ...mockContext, isDebug: true },
{ ...mockContext, host: 'different-host.com' },
{ ...mockContext, clientNumber: 'client-456' }
];

contexts.forEach(ctx => {
const result = handler(url, urlPrefix, ctx);
expect(result.url).toBe(url);
expect(result.signURL).toBe(false);
});
});
});

describe('Return value structure', () => {
it('should return an object with url property', () => {
const url = 'https://ik.imagekit.io/demo/image.jpg';
const result = handler(url, 'demo', mockContext);

expect(typeof result).toBe('object');
expect(result).toHaveProperty('url');
expect(typeof result.url).toBe('string');
});

it('should return an object with signURL property', () => {
const url = 'https://ik.imagekit.io/demo/image.jpg';
const result = handler(url, 'demo', mockContext);

expect(result).toHaveProperty('signURL');
expect(typeof result.signURL).toBe('boolean');
});

it('should not return status property (normal result type)', () => {
const url = 'https://ik.imagekit.io/demo/image.jpg';
const result = handler(url, 'demo', mockContext);

expect(result).not.toHaveProperty('status');
expect(result).not.toHaveProperty('body');
});
});

describe('Edge cases', () => {
it('should handle empty query strings', () => {
const url = 'https://ik.imagekit.io/demo/image.jpg?';
const result = handler(url, 'demo', mockContext);

expect(result.url).toBe(url);
});

it('should handle URLs with fragments', () => {
const url = 'https://ik.imagekit.io/demo/image.jpg#section';
const result = handler(url, 'demo', mockContext);

expect(result.url).toBe(url);
});

it('should handle complex paths', () => {
const url = 'https://ik.imagekit.io/demo/folder/subfolder/image.jpg';
const result = handler(url, 'demo', mockContext);

expect(result.url).toBe(url);
});

it('should handle URLs with special characters', () => {
const url = 'https://ik.imagekit.io/demo/image%20with%20spaces.jpg';
const result = handler(url, 'demo', mockContext);

expect(result.url).toBe(url);
});
it('should return the original URL unchanged', () => {
const url = 'https://ik.imagekit.io/demo/image.jpg';
const urlPrefix = 'demo';

const result = handler(url, urlPrefix, mockContext);

expect(result.url).toBe(url);
expect(result.signURL).toBe(false);
});
});
Loading