-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from crab85193/develop
feat: 都道府県データを取得するAPI呼び出し機能を追加
- Loading branch information
Showing
8 changed files
with
113 additions
and
2 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
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 |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
|
||
# misc | ||
.DS_Store | ||
.env | ||
.env.local | ||
.env.development.local | ||
.env.test.local | ||
|
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,23 @@ | ||
import axios from "axios"; | ||
import { Prefecture } from "../types/prefecture"; | ||
|
||
export const fetchPrefectures = async (): Promise<Prefecture[]> => { | ||
try { | ||
const response = await axios.get( | ||
`${process.env.REACT_APP_YUMEMI_API_URL}/prefectures`, | ||
{ | ||
headers: { | ||
"X-API-KEY": process.env.REACT_APP_YUMEMI_API_KEY!, | ||
}, | ||
} | ||
); | ||
|
||
if (response.status !== 200) { | ||
throw new Error("Failed to fetch prefectures"); | ||
} | ||
|
||
return response.data as Prefecture[]; | ||
} catch (error) { | ||
throw new Error("Error fetching prefectures: " + error); | ||
} | ||
}; |
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,44 @@ | ||
import axios from "axios"; | ||
import { fetchPrefectures } from "./../api/prefectures"; | ||
import { Prefecture } from "../types/prefecture"; | ||
|
||
jest.mock("axios"); | ||
const mockedAxios = axios as jest.Mocked<typeof axios>; | ||
|
||
describe("fetchPrefectures", () => { | ||
it("正常に都道府県データを取得できる", async () => { | ||
const mockData: Prefecture[] = [ | ||
{ prefCode: 1, prefName: "北海道" }, | ||
{ prefCode: 2, prefName: "青森県" }, | ||
{ prefCode: 3, prefName: "宮城県" }, | ||
]; | ||
|
||
mockedAxios.get.mockResolvedValue({ status: 200, data: mockData }); | ||
|
||
const result = await fetchPrefectures(); | ||
|
||
expect(result).toEqual(mockData); | ||
expect(mockedAxios.get).toHaveBeenCalledWith( | ||
`${process.env.REACT_APP_YUMEMI_API_URL}/prefectures`, | ||
{ | ||
headers: { | ||
"X-API-KEY": process.env.REACT_APP_YUMEMI_API_KEY!, | ||
}, | ||
} | ||
); | ||
}); | ||
|
||
it("APIがエラーを返した場合、エラーをスローする", async () => { | ||
mockedAxios.get.mockRejectedValue(new Error("API Error")); | ||
|
||
await expect(fetchPrefectures()).rejects.toThrow("API Error"); | ||
expect(mockedAxios.get).toHaveBeenCalledWith( | ||
`${process.env.REACT_APP_YUMEMI_API_URL}/prefectures`, | ||
{ | ||
headers: { | ||
"X-API-KEY": process.env.REACT_APP_YUMEMI_API_KEY!, | ||
}, | ||
} | ||
); | ||
}); | ||
}); |
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,4 @@ | ||
export interface Prefecture { | ||
prefCode: number; | ||
prefName: string; | ||
} |
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