@ -0,0 +1,18 @@ | |||||
import type { Config } from 'jest'; | |||||
import nextJest from 'next/jest.js'; | |||||
const createJestConfig = nextJest({ | |||||
// Provide the path to your Next.js app to load next.config.js and .env files in your test environment | |||||
dir: './', | |||||
}); | |||||
// Add any custom config to be passed to Jest | |||||
const config: Config = { | |||||
coverageProvider: 'v8', | |||||
testEnvironment: 'jsdom', | |||||
// Add more setup options before each test is run | |||||
// setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'], | |||||
}; | |||||
// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async | |||||
export default createJestConfig(config); |
@ -0,0 +1,7 @@ | |||||
// __mocks__/axiosInstance.ts | |||||
import axios from 'axios'; | |||||
import MockAdapter from 'axios-mock-adapter'; | |||||
const mock = new MockAdapter(axios); | |||||
export default mock; |
@ -0,0 +1,33 @@ | |||||
import axiosInstance from '../axios/axiosInstance'; | |||||
import { loginApi } from './loginApi'; | |||||
jest.mock('../axios/axiosInstance'); | |||||
describe('login API', () => { | |||||
it('should return token on successful login', async () => { | |||||
const mockResponse = { | |||||
data: { | |||||
token: 'fake-token', | |||||
}, | |||||
}; | |||||
(axiosInstance.post as jest.Mock).mockResolvedValue(mockResponse); | |||||
const data = { username: 'emilys', password: 'emilyspass' }; | |||||
const result = await loginApi(data); | |||||
expect(result).toEqual(mockResponse.data); | |||||
expect(axiosInstance.post).toHaveBeenCalledWith('/auth/login', data); | |||||
}); | |||||
it('should throw an error on failed login', async () => { | |||||
const errorMessage = 'Invalid credentials'; | |||||
(axiosInstance.post as jest.Mock).mockRejectedValue( | |||||
new Error(errorMessage) | |||||
); | |||||
const data = { username: 'test@example.com', password: 'wrongpassword' }; | |||||
await expect(loginApi(data)).rejects.toThrow(errorMessage); | |||||
expect(axiosInstance.post).toHaveBeenCalledWith('/auth/login', data); | |||||
}); | |||||
}); |
@ -0,0 +1,29 @@ | |||||
import axios from 'axios'; | |||||
import homeServices from './productsApi'; | |||||
jest.mock('axios'); | |||||
describe('homeServices.getProducts', () => { | |||||
afterEach(() => { | |||||
jest.clearAllMocks(); | |||||
}); | |||||
test('should return data on successful API call', async () => { | |||||
const mockData = { products: [{ id: 1, name: 'Product 1' }] }; | |||||
(axios.get as jest.Mock).mockResolvedValue({ data: mockData }); | |||||
const result = await homeServices.getProducts(); | |||||
expect(result).toEqual(mockData); | |||||
expect(axios.get).toHaveBeenCalledWith('https://dummyjson.com/products'); | |||||
}); | |||||
test('should throw generic error message on non-Axios error', async () => { | |||||
(axios.get as jest.Mock).mockRejectedValue(new Error('Non-Axios error')); | |||||
await expect(homeServices.getProducts()).rejects.toThrow( | |||||
'Something went wrong. Please try again.' | |||||
); | |||||
expect(axios.get).toHaveBeenCalledWith('https://dummyjson.com/products'); | |||||
}); | |||||
}); |
@ -1,16 +1,16 @@ | |||||
import axios, { isAxiosError } from "axios"; | |||||
import axios, { isAxiosError } from 'axios'; | |||||
/** Product List Home Page */ | /** Product List Home Page */ | ||||
const homeServices = { | const homeServices = { | ||||
getProducts: async () => { | getProducts: async () => { | ||||
try { | try { | ||||
const res = await axios.get("https://dummyjson.com/products"); | |||||
const res = await axios.get('https://dummyjson.com/products'); | |||||
return res.data; | return res.data; | ||||
} catch (error: any) { | } catch (error: any) { | ||||
if (isAxiosError(error)) { | if (isAxiosError(error)) { | ||||
throw new Error(error.response?.data.message); | throw new Error(error.response?.data.message); | ||||
} | } | ||||
throw new Error("Something went wrong. Please try again."); | |||||
throw new Error('Something went wrong. Please try again.'); | |||||
} | } | ||||
}, | }, | ||||
}; | }; |