48 lines
1.2 KiB
JavaScript
48 lines
1.2 KiB
JavaScript
const { compareEntities } = require('../src/index');
|
|
const axios = require('axios');
|
|
|
|
jest.mock('axios');
|
|
|
|
describe('compareEntities', () => {
|
|
const mockResponse = {
|
|
status: 200,
|
|
data: {
|
|
results: [
|
|
{ title: 'Apple vs Samsung vs Google', content: 'Apple leads in design...' },
|
|
],
|
|
},
|
|
};
|
|
|
|
beforeEach(() => {
|
|
axios.post.mockResolvedValue(mockResponse);
|
|
});
|
|
|
|
afterEach(() => {
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
it('should return data from Tavily API', async () => {
|
|
const data = await compareEntities('Apple', 'Samsung', 'Google');
|
|
expect(data).toEqual(mockResponse.data);
|
|
expect(axios.post).toHaveBeenCalledWith(
|
|
'https://api.tavily.com/search',
|
|
expect.objectContaining({
|
|
query: expect.stringContaining('Apple'),
|
|
}),
|
|
expect.any(Object)
|
|
);
|
|
});
|
|
|
|
it('should throw error if any entity is missing', async () => {
|
|
await expect(compareEntities('Apple', '', 'Google')).rejects.toThrow(
|
|
'All three entity names must be provided.'
|
|
);
|
|
});
|
|
|
|
it('should propagate API errors', async () => {
|
|
axios.post.mockRejectedValue(new Error('Network error'));
|
|
await expect(compareEntities('Apple', 'Samsung', 'Google')).rejects.toThrow(
|
|
'Network error'
|
|
);
|
|
});
|
|
}); |