From 6e3daf12c6826c2e33b90a6d91b6946b1c8da4b0 Mon Sep 17 00:00:00 2001 From: asasmith Date: Mon, 25 Sep 2023 14:22:27 -0400 Subject: [PATCH] wip tests --- src/handlers/__tests__/getBoxscore.test.ts | 111 ++++++++++++++++++--- src/handlers/__tests__/health.test.ts | 4 +- src/models/__mocks__/boxscore.ts | 7 ++ src/utils/__mocks__/mongoInstance.ts | 4 + 4 files changed, 109 insertions(+), 17 deletions(-) create mode 100644 src/models/__mocks__/boxscore.ts create mode 100644 src/utils/__mocks__/mongoInstance.ts diff --git a/src/handlers/__tests__/getBoxscore.test.ts b/src/handlers/__tests__/getBoxscore.test.ts index 7a077bc..317b0cd 100644 --- a/src/handlers/__tests__/getBoxscore.test.ts +++ b/src/handlers/__tests__/getBoxscore.test.ts @@ -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; 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; + + // 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; + + 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; + + // 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).mockResolvedValue({ - data: mockData, - } as AxiosResponse); + // (axios.get as jest.MockedFunction).mockResolvedValue({ + // data: mockData, + // } as AxiosResponse); - 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' }}); + // }); }); diff --git a/src/handlers/__tests__/health.test.ts b/src/handlers/__tests__/health.test.ts index 1ac3ad4..d339925 100644 --- a/src/handlers/__tests__/health.test.ts +++ b/src/handlers/__tests__/health.test.ts @@ -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'); diff --git a/src/models/__mocks__/boxscore.ts b/src/models/__mocks__/boxscore.ts new file mode 100644 index 0000000..c2a3d92 --- /dev/null +++ b/src/models/__mocks__/boxscore.ts @@ -0,0 +1,7 @@ +import { jest } from '@jest/globals'; + +export const BoxscoreModel = { + create: jest.fn(), + findOne: jest.fn(), + updateOne: jest.fn(), +}; diff --git a/src/utils/__mocks__/mongoInstance.ts b/src/utils/__mocks__/mongoInstance.ts new file mode 100644 index 0000000..fe83367 --- /dev/null +++ b/src/utils/__mocks__/mongoInstance.ts @@ -0,0 +1,4 @@ +export async function mongoInstance() { + console.log('Mocked mongoInstance: Attempted to connect but mocked.'); + return Promise.resolve(); +}