UNPKG

barikoiapis

Version:

Bangladeshi location data provider API library from barikoi.com

58 lines (51 loc) 1.58 kB
import { autocomplete } from "../src/autocomplete"; // Mocking axios for testing purposes jest.mock("axios"); import axios from "axios"; describe("autocomplete function", () => { it("fetches successfully data from the API", async () => { // Mock the axios.get function to return a successful response const mockedResponse = { data: { places: [ { id: 1, longitude: "90.3763", latitude: "23.8103", address: "Dhaka", city: "Dhaka", area: "Dhaka", postCode: 1000, pType: "city", uCode: "1000", address_bn: "ঢাকা", city_bn: "ঢাকা", area_bn: "ঢাকা", }, ], status: 200, }, }; (axios.get as jest.Mock).mockResolvedValue(mockedResponse); // Call the autocomplete function const results = await autocomplete({ q: "jessore", city: "dhaka", bangla: true, }); // Expectations expect(results).toEqual(mockedResponse.data.places); }); it("handles errors correctly", async () => { // Mock the axios.get function to simulate an error const errorMessage = "Request failed with status code 404"; (axios.get as jest.Mock).mockRejectedValue(new Error(errorMessage)); // Call the autocomplete function try { await autocomplete({ q: "jessore", city: "dhaka" }); } catch (error) { // Expectations for error handling expect(error.message).toBe(errorMessage); } }); });