-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
66cad26
commit 58b4cdc
Showing
10 changed files
with
769 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { dfdaGET, dfdaPOST } from './route'; | ||
import { createMocks } from 'node-mocks-http'; | ||
import { mockDeep } from 'jest-mock-extended'; | ||
import { prisma } from '@/lib/prisma'; | ||
|
||
jest.mock('@/lib/prisma'); | ||
|
||
describe('DFDA API Route', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('dfdaGET', () => { | ||
it('should return DFDA data for a valid user', async () => { | ||
const { req, res } = createMocks({ | ||
method: 'GET', | ||
query: { dfdaPath: 'validUser' }, | ||
}); | ||
|
||
const mockDfdaData = { id: 1, name: 'Test DFDA' }; | ||
(prisma.dFDA.findUnique as jest.Mock).mockResolvedValueOnce(mockDfdaData); | ||
|
||
await dfdaGET(req, res); | ||
|
||
expect(res._getStatusCode()).toBe(200); | ||
expect(JSON.parse(res._getData())).toEqual(mockDfdaData); | ||
}); | ||
|
||
it('should return 404 for an invalid user', async () => { | ||
const { req, res } = createMocks({ | ||
method: 'GET', | ||
query: { dfdaPath: 'invalidUser' }, | ||
}); | ||
|
||
(prisma.dFDA.findUnique as jest.Mock).mockResolvedValueOnce(null); | ||
|
||
await dfdaGET(req, res); | ||
|
||
expect(res._getStatusCode()).toBe(404); | ||
}); | ||
}); | ||
|
||
describe('dfdaPOST', () => { | ||
it('should create a new DFDA entry for a valid request', async () => { | ||
const { req, res } = createMocks({ | ||
method: 'POST', | ||
query: { dfdaPath: 'newEntry' }, | ||
body: { name: 'New DFDA Entry' }, | ||
}); | ||
req.session = { user: { id: 1 } } as any; | ||
|
||
const mockCreatedEntry = { id: 2, name: 'New DFDA Entry' }; | ||
(prisma.dFDA.create as jest.Mock).mockResolvedValueOnce(mockCreatedEntry); | ||
|
||
await dfdaPOST(req, res); | ||
|
||
expect(res._getStatusCode()).toBe(201); | ||
expect(JSON.parse(res._getData())).toEqual(mockCreatedEntry); | ||
}); | ||
|
||
it('should return 401 for an unauthenticated request', async () => { | ||
const { req, res } = createMocks({ | ||
method: 'POST', | ||
query: { dfdaPath: 'newEntry' }, | ||
body: { name: 'New DFDA Entry' }, | ||
}); | ||
req.session = {} as any; | ||
|
||
await dfdaPOST(req, res); | ||
|
||
expect(res._getStatusCode()).toBe(401); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { createMocks } from 'node-mocks-http'; | ||
import { mockDeep } from 'jest-mock-extended'; | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
import { POST } from './route'; | ||
|
||
jest.mock('openai', () => ({ | ||
Configuration: jest.fn(), | ||
OpenAIApi: jest.fn(() => ({ | ||
createCompletion: jest.fn(), | ||
})), | ||
})); | ||
|
||
describe('Image to Measurements API Route', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should return a successful response for a valid request', async () => { | ||
const { req, res } = createMocks<NextApiRequest, NextApiResponse>({ | ||
method: 'POST', | ||
body: { | ||
file: 'validBase64Image', | ||
prompt: 'Test prompt', | ||
}, | ||
}); | ||
|
||
const mockOpenAIResponse = { | ||
data: { | ||
choices: [ | ||
{ | ||
text: 'Mock analysis result', | ||
}, | ||
], | ||
}, | ||
}; | ||
jest.spyOn(require('openai'), 'OpenAIApi').mockImplementation(() => ({ | ||
createCompletion: jest.fn().mockResolvedValueOnce(mockOpenAIResponse), | ||
})); | ||
|
||
await POST(req, res); | ||
|
||
expect(res._getStatusCode()).toBe(200); | ||
expect(JSON.parse(res._getData())).toEqual({ | ||
success: true, | ||
analysis: 'Mock analysis result', | ||
}); | ||
}); | ||
|
||
it('should return an error response for an invalid request', async () => { | ||
const { req, res } = createMocks<NextApiRequest, NextApiResponse>({ | ||
method: 'POST', | ||
body: { | ||
file: 'invalidBase64Image', | ||
prompt: 'Test prompt', | ||
}, | ||
}); | ||
|
||
const mockOpenAIError = new Error('Mock OpenAI error'); | ||
jest.spyOn(require('openai'), 'OpenAIApi').mockImplementation(() => ({ | ||
createCompletion: jest.fn().mockRejectedValueOnce(mockOpenAIError), | ||
})); | ||
|
||
await POST(req, res); | ||
|
||
expect(res._getStatusCode()).toBe(500); | ||
expect(JSON.parse(res._getData())).toEqual({ | ||
success: false, | ||
message: 'An error occurred while processing the image.', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import React from 'react'; | ||
import { render, fireEvent, act } from '@testing-library/react'; | ||
import { CameraButton } from './CameraButton'; | ||
|
||
describe('CameraButton component', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should call onCapture with the captured blob when a photo is taken', async () => { | ||
const mockOnCapture = jest.fn(); | ||
const mockBlob = new Blob(['test'], { type: 'image/png' }); | ||
|
||
global.URL.createObjectURL = jest.fn(); | ||
global.navigator.mediaDevices.getUserMedia = jest.fn().mockResolvedValueOnce({ | ||
getTracks: () => [{ stop: jest.fn() }], | ||
}); | ||
|
||
const { getByLabelText } = render(<CameraButton onCapture={mockOnCapture} />); | ||
const cameraButton = getByLabelText('Take a photo'); | ||
|
||
await act(async () => { | ||
fireEvent.click(cameraButton); | ||
}); | ||
|
||
const videoElement = document.querySelector('video'); | ||
const mockCaptureStream = { | ||
getVideoTracks: () => [{ stop: jest.fn() }], | ||
}; | ||
videoElement.srcObject = mockCaptureStream; | ||
|
||
const canvasElement = document.createElement('canvas'); | ||
canvasElement.toBlob = jest.fn().mockImplementationOnce((callback) => { | ||
callback(mockBlob); | ||
}); | ||
|
||
await act(async () => { | ||
fireEvent.click(cameraButton); | ||
}); | ||
|
||
expect(mockOnCapture).toHaveBeenCalledWith(mockBlob); | ||
}); | ||
|
||
it('should handle errors when accessing the camera stream', async () => { | ||
const mockOnCapture = jest.fn(); | ||
const mockError = new Error('Camera access denied'); | ||
|
||
global.navigator.mediaDevices.getUserMedia = jest.fn().mockRejectedValueOnce(mockError); | ||
|
||
const { getByLabelText } = render(<CameraButton onCapture={mockOnCapture} />); | ||
const cameraButton = getByLabelText('Take a photo'); | ||
|
||
await act(async () => { | ||
fireEvent.click(cameraButton); | ||
}); | ||
|
||
expect(mockOnCapture).not.toHaveBeenCalled(); | ||
// You can add more assertions to check error handling behavior | ||
}); | ||
}); |
Oops, something went wrong.