Skip to content

Commit

Permalink
Linting
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronbrethorst committed Jan 17, 2025
1 parent f9d84b3 commit b7a8d22
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 239 deletions.
20 changes: 10 additions & 10 deletions src/lib/obaSdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
241 changes: 117 additions & 124 deletions src/tests/lib/obaSdk.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
const oba = new onebusaway({

Check failure on line 36 in src/tests/lib/obaSdk.test.js

View workflow job for this annotation

GitHub Actions / lint

'oba' is assigned a value but never used
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.');
});
});
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.');
});
});
Loading

0 comments on commit b7a8d22

Please sign in to comment.