From 193a898bc5ee783a02e9073569861c236bbf53ae Mon Sep 17 00:00:00 2001 From: Aaron Brethorst Date: Thu, 16 Jan 2025 22:06:08 -0800 Subject: [PATCH] Linting --- eslint.config.js | 2 +- src/lib/obaSdk.js | 20 +-- src/tests/lib/obaSdk.test.js | 241 +++++++++++++++++------------------ src/tests/lib/urls.test.js | 163 +++++++++-------------- 4 files changed, 186 insertions(+), 240 deletions(-) diff --git a/eslint.config.js b/eslint.config.js index 9e95a0a..d422cb0 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -21,6 +21,6 @@ export default [ } }, { - ignores: ['build/', '.svelte-kit/', 'dist/', 'src/lib/googleMaps.js'] + ignores: ['build/', '.svelte-kit/', 'dist/', 'src/lib/googleMaps.js', 'coverage'] } ]; diff --git a/src/lib/obaSdk.js b/src/lib/obaSdk.js index 7ee4f48..05de54a 100644 --- a/src/lib/obaSdk.js +++ b/src/lib/obaSdk.js @@ -9,19 +9,19 @@ const oba = new onebusaway({ }); export function handleOBAResponse(response, entityName) { - if (!response) { - throw error(500, `Unable to fetch ${entityName}.`); - } + if (!response) { + throw error(500, `Unable to fetch ${entityName}.`); + } - if (typeof response.code === 'undefined') { - throw error(500, `Unable to fetch ${entityName}.`); - } + if (typeof response.code === 'undefined') { + throw error(500, `Unable to fetch ${entityName}.`); + } - if (response.code !== 200) { - throw error(500, `Unable to fetch ${entityName}.`); - } + if (response.code !== 200) { + throw error(500, `Unable to fetch ${entityName}.`); + } - return json(response); + return json(response); } export default oba; diff --git a/src/tests/lib/obaSdk.test.js b/src/tests/lib/obaSdk.test.js index c2a3ac9..9474cde 100644 --- a/src/tests/lib/obaSdk.test.js +++ b/src/tests/lib/obaSdk.test.js @@ -8,140 +8,133 @@ vi.mock('onebusaway-sdk'); // Mock @sveltejs/kit error and json functions vi.mock('@sveltejs/kit', () => ({ - error: vi.fn((status, message) => { - throw new Error(message); - }), - json: vi.fn((data) => ({ - status: 200, - body: data - })) + error: vi.fn((status, message) => { + throw new Error(message); + }), + json: vi.fn((data) => ({ + status: 200, + body: data + })) })); // Mock environment variables vi.mock('$env/static/public', () => ({ - PUBLIC_OBA_SERVER_URL: 'https://test-api.example.com' + PUBLIC_OBA_SERVER_URL: 'https://test-api.example.com' })); vi.mock('$env/static/private', () => ({ - PRIVATE_OBA_API_KEY: 'test-api-key' + PRIVATE_OBA_API_KEY: 'test-api-key' })); describe('OneBusAway Client', () => { - beforeEach(() => { - vi.clearAllMocks(); - }); - - describe('OBA Client Initialization', () => { - it('should initialize with correct configuration', () => { - const oba = new onebusaway({ - baseURL: 'https://test-api.example.com', - apiKey: 'test-api-key' - }); - - expect(onebusaway).toHaveBeenCalledWith({ - baseURL: 'https://test-api.example.com', - apiKey: 'test-api-key' - }); - }); - }); - - describe('handleOBAResponse', () => { - it('should return JSON response when status code is 200', () => { - const mockResponse = { - code: 200, - data: { - stops: [] - } - }; - - const result = handleOBAResponse(mockResponse, 'stops'); - expect(json).toHaveBeenCalledWith(mockResponse); - expect(result.status).toBe(200); - expect(result.body).toEqual(mockResponse); - }); - - it('should throw error when status code is not 200', () => { - const mockResponse = { - code: 404, - data: null - }; - - expect(() => handleOBAResponse(mockResponse, 'stops')) - .toThrow(/Unable to fetch stops/); - expect(error).toHaveBeenCalledWith(500, 'Unable to fetch stops.'); - }); - - it('should handle undefined response gracefully', () => { - expect(() => handleOBAResponse(undefined, 'stops')) - .toThrow(/Unable to fetch stops/); - expect(error).toHaveBeenCalledWith(500, 'Unable to fetch stops.'); - }); - - it('should handle null response gracefully', () => { - expect(() => handleOBAResponse(null, 'stops')) - .toThrow(/Unable to fetch stops/); - expect(error).toHaveBeenCalledWith(500, 'Unable to fetch stops.'); - }); - - it('should handle response with missing code gracefully', () => { - const mockResponse = { - data: { - stops: [] - } - }; - - expect(() => handleOBAResponse(mockResponse, 'stops')) - .toThrow(/Unable to fetch stops/); - expect(error).toHaveBeenCalledWith(500, 'Unable to fetch stops.'); - }); - }); + beforeEach(() => { + vi.clearAllMocks(); + }); + + describe('OBA Client Initialization', () => { + it('should initialize with correct configuration', () => { + new onebusaway({ + baseURL: 'https://test-api.example.com', + apiKey: 'test-api-key' + }); + + expect(onebusaway).toHaveBeenCalledWith({ + baseURL: 'https://test-api.example.com', + apiKey: 'test-api-key' + }); + }); + }); + + describe('handleOBAResponse', () => { + it('should return JSON response when status code is 200', () => { + const mockResponse = { + code: 200, + data: { + stops: [] + } + }; + + const result = handleOBAResponse(mockResponse, 'stops'); + expect(json).toHaveBeenCalledWith(mockResponse); + expect(result.status).toBe(200); + expect(result.body).toEqual(mockResponse); + }); + + it('should throw error when status code is not 200', () => { + const mockResponse = { + code: 404, + data: null + }; + + expect(() => handleOBAResponse(mockResponse, 'stops')).toThrow(/Unable to fetch stops/); + expect(error).toHaveBeenCalledWith(500, 'Unable to fetch stops.'); + }); + + it('should handle undefined response gracefully', () => { + expect(() => handleOBAResponse(undefined, 'stops')).toThrow(/Unable to fetch stops/); + expect(error).toHaveBeenCalledWith(500, 'Unable to fetch stops.'); + }); + + it('should handle null response gracefully', () => { + expect(() => handleOBAResponse(null, 'stops')).toThrow(/Unable to fetch stops/); + expect(error).toHaveBeenCalledWith(500, 'Unable to fetch stops.'); + }); + + it('should handle response with missing code gracefully', () => { + const mockResponse = { + data: { + stops: [] + } + }; + + expect(() => handleOBAResponse(mockResponse, 'stops')).toThrow(/Unable to fetch stops/); + expect(error).toHaveBeenCalledWith(500, 'Unable to fetch stops.'); + }); + }); }); describe('OBA Client Integration', () => { - let oba; - - beforeEach(() => { - vi.clearAllMocks(); - oba = new onebusaway({ - baseURL: 'https://test-api.example.com', - apiKey: 'test-api-key' - }); - }); - - it('should handle successful API responses', async () => { - const mockApiResponse = { - code: 200, - data: { - stops: [ - { id: 1, name: 'Test Stop' } - ] - } - }; - - // Mock a successful API call - oba.stops = vi.fn().mockResolvedValue(mockApiResponse); - - const response = await oba.stops(); - const result = handleOBAResponse(response, 'stops'); - - expect(json).toHaveBeenCalledWith(mockApiResponse); - expect(result.status).toBe(200); - expect(result.body).toEqual(mockApiResponse); - }); - - it('should handle failed API responses', async () => { - const mockApiResponse = { - code: 500, - data: null - }; - - // Mock a failed API call - oba.stops = vi.fn().mockResolvedValue(mockApiResponse); - - const response = await oba.stops(); - - expect(() => handleOBAResponse(response, 'stops')) - .toThrow(/Unable to fetch stops/); - expect(error).toHaveBeenCalledWith(500, 'Unable to fetch stops.'); - }); -}); \ No newline at end of file + let oba; + + beforeEach(() => { + vi.clearAllMocks(); + oba = new onebusaway({ + baseURL: 'https://test-api.example.com', + apiKey: 'test-api-key' + }); + }); + + it('should handle successful API responses', async () => { + const mockApiResponse = { + code: 200, + data: { + stops: [{ id: 1, name: 'Test Stop' }] + } + }; + + // Mock a successful API call + oba.stops = vi.fn().mockResolvedValue(mockApiResponse); + + const response = await oba.stops(); + const result = handleOBAResponse(response, 'stops'); + + expect(json).toHaveBeenCalledWith(mockApiResponse); + expect(result.status).toBe(200); + expect(result.body).toEqual(mockApiResponse); + }); + + it('should handle failed API responses', async () => { + const mockApiResponse = { + code: 500, + data: null + }; + + // Mock a failed API call + oba.stops = vi.fn().mockResolvedValue(mockApiResponse); + + const response = await oba.stops(); + + expect(() => handleOBAResponse(response, 'stops')).toThrow(/Unable to fetch stops/); + expect(error).toHaveBeenCalledWith(500, 'Unable to fetch stops.'); + }); +}); diff --git a/src/tests/lib/urls.test.js b/src/tests/lib/urls.test.js index 49d6012..ae71f6c 100644 --- a/src/tests/lib/urls.test.js +++ b/src/tests/lib/urls.test.js @@ -2,120 +2,73 @@ import { describe, it, expect } from 'vitest'; import { buildURL } from '$lib/urls'; describe('buildURL', () => { - it('should build a basic URL with query parameters', () => { - const result = buildURL( - 'http://example.com', - 'api/data', - { key: 'value', page: '1' } - ); - expect(result).toBe('http://example.com/api/data?key=value&page=1'); - }); + it('should build a basic URL with query parameters', () => { + const result = buildURL('http://example.com', 'api/data', { key: 'value', page: '1' }); + expect(result).toBe('http://example.com/api/data?key=value&page=1'); + }); - it('should handle trailing slashes in baseURL', () => { - const result = buildURL( - 'http://example.com/', - 'api/data', - { key: 'value' } - ); - expect(result).toBe('http://example.com/api/data?key=value'); - }); + it('should handle trailing slashes in baseURL', () => { + const result = buildURL('http://example.com/', 'api/data', { key: 'value' }); + expect(result).toBe('http://example.com/api/data?key=value'); + }); - it('should handle leading slashes in path', () => { - const result = buildURL( - 'http://example.com', - '/api/data', - { key: 'value' } - ); - expect(result).toBe('http://example.com/api/data?key=value'); - }); + it('should handle leading slashes in path', () => { + const result = buildURL('http://example.com', '/api/data', { key: 'value' }); + expect(result).toBe('http://example.com/api/data?key=value'); + }); - it('should handle both trailing and leading slashes', () => { - const result = buildURL( - 'http://example.com/', - '/api/data', - { key: 'value' } - ); - expect(result).toBe('http://example.com/api/data?key=value'); - }); + it('should handle both trailing and leading slashes', () => { + const result = buildURL('http://example.com/', '/api/data', { key: 'value' }); + expect(result).toBe('http://example.com/api/data?key=value'); + }); - it('should handle multiple trailing slashes in baseURL', () => { - const result = buildURL( - 'http://example.com///', - 'api/data', - { key: 'value' } - ); - expect(result).toBe('http://example.com/api/data?key=value'); - }); + it('should handle multiple trailing slashes in baseURL', () => { + const result = buildURL('http://example.com///', 'api/data', { key: 'value' }); + expect(result).toBe('http://example.com/api/data?key=value'); + }); - it('should handle multiple leading slashes in path', () => { - const result = buildURL( - 'http://example.com', - '///api/data', - { key: 'value' } - ); - expect(result).toBe('http://example.com/api/data?key=value'); - }); + it('should handle multiple leading slashes in path', () => { + const result = buildURL('http://example.com', '///api/data', { key: 'value' }); + expect(result).toBe('http://example.com/api/data?key=value'); + }); - it('should handle empty query parameters', () => { - const result = buildURL( - 'http://example.com', - 'api/data', - {} - ); - expect(result).toBe('http://example.com/api/data?'); - }); + it('should handle empty query parameters', () => { + const result = buildURL('http://example.com', 'api/data', {}); + expect(result).toBe('http://example.com/api/data?'); + }); - it('should encode query parameter values', () => { - const result = buildURL( - 'http://example.com', - 'api/data', - { key: 'value with spaces', special: '!@#$%' } - ); - expect(result).toBe('http://example.com/api/data?key=value+with+spaces&special=%21%40%23%24%25'); - }); + it('should encode query parameter values', () => { + const result = buildURL('http://example.com', 'api/data', { + key: 'value with spaces', + special: '!@#$%' + }); + expect(result).toBe( + 'http://example.com/api/data?key=value+with+spaces&special=%21%40%23%24%25' + ); + }); - it('should convert undefined values to "undefined" string', () => { - const result = buildURL( - 'http://example.com', - 'api/data', - { key: undefined, value: 'test' } - ); - expect(result).toBe('http://example.com/api/data?key=undefined&value=test'); - }); + it('should convert undefined values to "undefined" string', () => { + const result = buildURL('http://example.com', 'api/data', { key: undefined, value: 'test' }); + expect(result).toBe('http://example.com/api/data?key=undefined&value=test'); + }); - it('should convert null values to "null" string', () => { - const result = buildURL( - 'http://example.com', - 'api/data', - { key: null, value: 'test' } - ); - expect(result).toBe('http://example.com/api/data?key=null&value=test'); - }); + it('should convert null values to "null" string', () => { + const result = buildURL('http://example.com', 'api/data', { key: null, value: 'test' }); + expect(result).toBe('http://example.com/api/data?key=null&value=test'); + }); - it('should convert array to comma-separated string', () => { - const result = buildURL( - 'http://example.com', - 'api/data', - { items: ['a', 'b', 'c'] } - ); - expect(result).toBe('http://example.com/api/data?items=a%2Cb%2Cc'); - }); + it('should convert array to comma-separated string', () => { + const result = buildURL('http://example.com', 'api/data', { items: ['a', 'b', 'c'] }); + expect(result).toBe('http://example.com/api/data?items=a%2Cb%2Cc'); + }); - it('should handle boolean values', () => { - const result = buildURL( - 'http://example.com', - 'api/data', - { isActive: true, isDeleted: false } - ); - expect(result).toBe('http://example.com/api/data?isActive=true&isDeleted=false'); - }); + it('should handle boolean values', () => { + const result = buildURL('http://example.com', 'api/data', { isActive: true, isDeleted: false }); + expect(result).toBe('http://example.com/api/data?isActive=true&isDeleted=false'); + }); - it('should handle number values', () => { - const result = buildURL( - 'http://example.com', - 'api/data', - { count: 42, price: 19.99 } - ); - expect(result).toBe('http://example.com/api/data?count=42&price=19.99'); - }); -}); \ No newline at end of file + it('should handle number values', () => { + const result = buildURL('http://example.com', 'api/data', { count: 42, price: 19.99 }); + expect(result).toBe('http://example.com/api/data?count=42&price=19.99'); + }); +});