-
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.
feat: chapter api wrapped and tested
- Loading branch information
Showing
4 changed files
with
154 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
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,70 @@ | ||
import { ListChapters } from "../types"; | ||
import chapter from "./chapter"; | ||
|
||
describe("Fetch all the chapter according to language", () => { | ||
beforeEach(() => jest.clearAllMocks()); | ||
|
||
it("Should fetch all the chapter", async () => { | ||
const res = await chapter.listChapters() as ListChapters; | ||
expect(res).toHaveProperty('chapters'); | ||
}); | ||
|
||
it("Shoud Throw an error if invalid language code is provided", async () => { | ||
try { | ||
await chapter.listChapters("bla-bla"); | ||
} catch (err: any) { | ||
expect(err.message).toEqual('Provided language is not supported'); | ||
} | ||
}); | ||
}); | ||
|
||
describe("Fetch a specific chapter according to provided chapter id and language", () => { | ||
|
||
beforeEach(() => jest.clearAllMocks()); | ||
|
||
it("Should fetch the chapter when a valid id and language is provided", async () => { | ||
const res: any = await chapter.getChapter(1, 'ha'); | ||
expect(res).toHaveProperty('chapter'); | ||
expect(res.chapter).toHaveProperty('id'); | ||
expect(res.chapter).toHaveProperty('translated_name'); | ||
}); | ||
|
||
|
||
it("Should throw and error if invalid chapter id provided", async () => { | ||
try { | ||
const res: any = await chapter.getChapter(3434343, 'ha'); | ||
expect(res).toHaveProperty('status'); | ||
expect(res.status).toEqual(404); | ||
expect(res.error).toEqual('Surah number or slug is invalid. Please select valid slug or surah number from 1-114.'); | ||
} catch (err: any) { | ||
console.log(err.response); | ||
expect(err.message).toEqual('Request failed with status code 404'); | ||
} | ||
}); | ||
}); | ||
|
||
|
||
describe("Fetching the chapter info", () => { | ||
beforeEach(() => jest.clearAllMocks()); | ||
|
||
it("Should fetch the chapter info according to the chapter id and language provided", async () => { | ||
const res: any = await chapter.getChapterInfo(1, 'ha'); | ||
expect(res).toHaveProperty('chapter_info'); | ||
expect(res.chapter_info).toHaveProperty('id'); | ||
expect(res.chapter_info).toHaveProperty('text'); | ||
expect(res.chapter_info.language_name).toEqual('english'); | ||
}); | ||
|
||
it("Should throw and error if invalid chapter id provided", async () => { | ||
try { | ||
const res: any = await chapter.getChapterInfo(3434343, 'ha'); | ||
expect(res).toHaveProperty('status'); | ||
expect(res.status).toEqual(404); | ||
expect(res.error).toEqual('Surah number or slug is invalid. Please select valid slug or surah number from 1-114.'); | ||
} catch (err: any) { | ||
console.log(err.response); | ||
expect(err.message).toEqual('Request failed with status code 404'); | ||
} | ||
}); | ||
|
||
}); |
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,39 @@ | ||
import { AxiosError } from "axios"; | ||
import Api from "../api"; | ||
import { ChapterApi, ListChapters, Chapter, ChapterInfo } from "../types"; | ||
import { handleError, handleResponse } from "../utils"; | ||
|
||
const api = Api(); | ||
|
||
const ALLOWED_LANGUAGES = new Set(['en', 'ur', 'bn', 'tr', 'es', 'fr', 'bs', 'ru', 'ml', 'id', 'uz', 'nl', 'de', 'tg', 'ta', 'ja', 'it', 'vi', 'zh', 'sq', 'fa', 'bg', 'bm', 'ha', 'pt', 'ro', 'hi', 'sw', 'kk', 'th', 'tl', 'km', 'as', 'ko', 'so', 'az', 'ku', 'dv', 'ms', 'prs', 'zgh', 'am', 'ce', 'cs', 'fi', 'gu', 'he', 'ka', 'kn', 'ks', 'lg', 'mk', 'mr', 'mrn', 'ne', 'no', 'om', 'pl', 'ps', 'rw', 'sd', 'se', 'si', 'sr', 'sv', 'te', 'tt', 'ug', 'uk', 'sq', 'yo']); | ||
|
||
|
||
const chapter: ChapterApi = { | ||
listChapters(language = 'en'): Promise<ListChapters | Error | AxiosError> { | ||
const isLanguageSupported = language && ALLOWED_LANGUAGES.has(language); | ||
if (!isLanguageSupported) return Promise.reject(new Error("Provided language is not supported")); | ||
return new Promise((resolve, reject) => { | ||
api.get(`/chapters?${new URLSearchParams({ language })}`) | ||
.then(handleResponse(resolve)) | ||
.catch(handleError(reject)); | ||
}); | ||
}, | ||
getChapter(id: number, language = 'en'): Promise<Chapter | Error | AxiosError> { | ||
if (language && !ALLOWED_LANGUAGES.has(language)) return Promise.reject(new Error("Provided language is not supported")); | ||
return new Promise((resolve, reject) => { | ||
api.get(`/chapters/${id}?${new URLSearchParams({ language })}`) | ||
.then(handleResponse(resolve)) | ||
.catch(handleError(reject)); | ||
}); | ||
}, | ||
getChapterInfo(chapter_id: number, language = 'en'): Promise<ChapterInfo | Error | AxiosError> { | ||
if (language && !ALLOWED_LANGUAGES.has(language)) return Promise.reject(new Error("Provided language is not supported")); | ||
return new Promise((resolve, reject) => { | ||
api.get(`/chapters/${chapter_id}/info?${new URLSearchParams({ language })}`) | ||
.then(handleResponse(resolve)) | ||
.catch(handleError(reject)); | ||
}); | ||
} | ||
}; | ||
|
||
export default chapter; |
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