Skip to content
Draft
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
111 changes: 95 additions & 16 deletions src/handlers/__tests__/getBoxscore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,109 @@ import { beforeEach, describe, expect, it, jest } from '@jest/globals';
import axios, { AxiosResponse } from 'axios';
import { app } from '../../server';
import request from 'supertest';
import { BoxscoreModel } from '../../models/boxscore';

jest.mock('axios');
jest.mock('../../utils/mongoInstance');
// jest.mock('../../models/boxscore');

jest.mock('../../models/boxscore', () => {
return {
BoxscoreModel: {
create: jest.fn(),
findOne: jest.fn(),
updateOne: jest.fn(),
},
};
});

const mockAxios = axios as jest.Mocked<typeof axios>;

describe('getBoxsore route handler', () => {

beforeEach(() => {
jest.clearAllMocks();
});
beforeEach(() => {
jest.clearAllMocks();
});

// it('should return the cached boxscore if not stale', async function () {
// const mockedCachedBoxscore = {
// game_id: '123',
// updated_at: Date.now(),
// };

// const mockBoxscoreModel = BoxscoreModel as jest.Mocked<typeof BoxscoreModel>;

// mockBoxscoreModel.findOne.mockResolvedValueOnce(mockedCachedBoxscore);

// const response = await request(app).get('/v1/boxscore/123');

// expect(response.status).toBe(200);
// });

it('should fetch and return a new boxscore if cached data is stale', async () => {
// Mock a stale cached boxscore
const mockCachedBoxscore = {
game_id: '123',
updated_at: new Date(Date.now() - 16000).toISOString(), // Stale data
// Add other relevant properties here
};

// Mock the Axios GET request response
const mockApiResponse = {
data: {
league: 'NBA',
away_team: {
team_id: 'away',
},
home_team: {
team_id: 'home',
},
// Add other relevant properties here
},
};

const mockUpdateOneResponse = {
acknowledged: false,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 1,
upsertedId: null,
};

const mockBoxscoreModel = BoxscoreModel as jest.Mocked<typeof BoxscoreModel>;

mockBoxscoreModel.findOne.mockResolvedValue(mockCachedBoxscore);
mockBoxscoreModel.updateOne.mockResolvedValue(mockUpdateOneResponse);
mockAxios.get.mockResolvedValue(mockApiResponse);

const response = await request(app).get('/v1/boxscore/123');

expect(response.status).toBe(200);
expect(response.body.data).toEqual(expect.objectContaining(mockApiResponse.data));
});

// it('should handle errors gracefully and return a 500 status code', async () => {
// console.log('here')
// const mockBoxscoreModel = BoxscoreModel as jest.Mocked<typeof BoxscoreModel>;

// mockBoxscoreModel.findOne.mockRejectedValueOnce(new Error('wut error'));

// const response = await request(app).get('/v1/boxscore/123');

it('handles axios error', async () => {
await request(app).get('/v1/boxscore/no').expect(500);
});
// expect(response.status).toBe(500);
// });

it('fetches successfully data from an API', async () => {
const mockData = {
foo: 'bar',
};
// it('fetches successfully data from an API', async () => {
// const mockData = {
// foo: 'bar',
// };

(axios.get as jest.MockedFunction<typeof axios.get>).mockResolvedValue({
data: mockData,
} as AxiosResponse<typeof mockData>);
// (axios.get as jest.MockedFunction<typeof axios.get>).mockResolvedValue({
// data: mockData,
// } as AxiosResponse<typeof mockData>);

const response = await request(app).get('/v1/boxscore/123');
expect(response.body).toEqual({ data: { foo: 'bar' }});
});
// const response = await request(app).get('/v1/boxscore/123');
// expect(response.body).toEqual({ data: { foo: 'bar' }});
// });
});

4 changes: 3 additions & 1 deletion src/handlers/__tests__/health.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { describe, expect, it } from '@jest/globals';
import { describe, expect, it, jest } from '@jest/globals';
import { app } from '../../server';
import request from 'supertest';

jest.mock('../../utils/mongoInstance');

describe('/health', function () {
it('should return 200 status when server is running', async function () {
const response = await request(app).get('/health');
Expand Down
7 changes: 7 additions & 0 deletions src/models/__mocks__/boxscore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { jest } from '@jest/globals';

export const BoxscoreModel = {
create: jest.fn(),
findOne: jest.fn(),
updateOne: jest.fn(),
};
4 changes: 4 additions & 0 deletions src/utils/__mocks__/mongoInstance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export async function mongoInstance() {
console.log('Mocked mongoInstance: Attempted to connect but mocked.');
return Promise.resolve();
}