diff --git a/src/quran/verse.test.ts b/src/quran/verse.test.ts new file mode 100644 index 0000000..6c1267b --- /dev/null +++ b/src/quran/verse.test.ts @@ -0,0 +1,112 @@ +import verse from './verse'; + +describe("Fetching verses by chapter number", () => { + beforeEach(() => jest.clearAllMocks()); + + it("Should fetch verses by chapter number", async () => { + const res: any = await verse.getVerseByChapter('1'); + expect(res).toHaveProperty('verses'); + expect(res).toHaveProperty('pagination'); + }); + + it("Should return a response with 404 status code", async () => { + const res: any = await verse.getVerseByChapter('101839'); + expect(res).toHaveProperty('status'); + expect(res.status).toEqual(404); + }); +}); + +describe("Fetching verses by page number", () => { + beforeEach(() => jest.clearAllMocks()); + + it("Should fetch verses by page number", async () => { + const res: any = await verse.getVerseByPage('1'); + expect(res).toHaveProperty('verses'); + expect(res).toHaveProperty('pagination'); + }); + + it("Should return a response with 404 status code", async () => { + const res: any = await verse.getVerseByPage('101839'); + expect(res).toHaveProperty('status'); + expect(res.status).toEqual(404); + }); +}); + +describe("Fetching verses by juz number", () => { + beforeEach(() => jest.clearAllMocks()); + + it("Should fetch verses by juz number", async () => { + const res: any = await verse.getVerseByJuz('1'); + expect(res).toHaveProperty('verses'); + expect(res).toHaveProperty('pagination'); + }); + + it("Should return a response with 404 status code", async () => { + const res: any = await verse.getVerseByJuz('101839'); + expect(res).toHaveProperty('status'); + expect(res.status).toEqual(404); + }); +}); + +describe("Fetching verses by hizb number", () => { + beforeEach(() => jest.clearAllMocks()); + + it("Should fetch verses by hizb number", async () => { + const res: any = await verse.getVerseByHizbNumber('1'); + expect(res).toHaveProperty('verses'); + expect(res).toHaveProperty('pagination'); + }); + + it("Should return a response with 404 status code", async () => { + const res: any = await verse.getVerseByHizbNumber('101839'); + expect(res).toHaveProperty('status'); + expect(res.status).toEqual(404); + }); +}); + +describe("Fetching verses by rub el hizb number", () => { + beforeEach(() => jest.clearAllMocks()); + + it("Should fetch verses by rub el hizb number", async () => { + const res: any = await verse.getVerseByRubElHizbNumber('1'); + expect(res).toHaveProperty('verses'); + expect(res).toHaveProperty('pagination'); + }); + + it("Should return a response with 404 status code", async () => { + const res: any = await verse.getVerseByRubElHizbNumber('101839'); + expect(res).toHaveProperty('status'); + expect(res.status).toEqual(404); + }); +}); + +describe("Fetching a specific verse by verse key", () => { + beforeEach(() => jest.clearAllMocks()); + + it("Should fetch a specific verse by verse key", async () => { + const res: any = await verse.getSpecificVerseByVerseKey('1:1'); + expect(res).toHaveProperty('verse'); + }); + + it("Should return a response with 404 status code", async () => { + const res: any = await verse.getSpecificVerseByVerseKey('101839:1'); + expect(res).toHaveProperty('status'); + expect(res.status).toEqual(404); + }); +}); + +describe("Fetching a random ayah", () => { + beforeEach(() => jest.clearAllMocks()); + + it("Should fetch a random ayah", async () => { + const res: any = await verse.getRandomAyah(); + expect(res).toHaveProperty('verse'); + }); + + it("Should return a response with 404 status code", async () => { + const res: any = await verse.getRandomAyah(); + expect(res).toHaveProperty('verse'); + expect(res.verse).toHaveProperty('id'); + expect(res.verse).toHaveProperty('verse_number'); + }); +}); diff --git a/src/quran/verse.ts b/src/quran/verse.ts index 2443aeb..a3edc70 100644 --- a/src/quran/verse.ts +++ b/src/quran/verse.ts @@ -51,4 +51,24 @@ const verse: VerseApi = { .catch(handleError(reject)); }); }, -}; \ No newline at end of file + getSpecificVerseByVerseKey(verse_key: string, query?: VerseQuery): Promise { + if (query?.language && !ALLOWED_LANGUAGES.has(query.language)) throw new Error('Provided query language is not allowed'); + const uri = query ? `/verses/by_key/${verse_key}?${new URLSearchParams(query as URLSearchParams)}` : `/verses/by_key/${verse_key}`; + return new Promise((resolve, reject) => { + api.get(uri) + .then(handleResponse(resolve)) + .catch(handleError(reject)); + }); + }, + getRandomAyah(query?: VerseQuery): Promise { + if (query?.language && !ALLOWED_LANGUAGES.has(query.language)) throw new Error('Provided query language is not allowed'); + const uri = query ? `/verses/random?${new URLSearchParams(query as URLSearchParams)}` : `/verses/random`; + return new Promise((resolve, reject) => { + api.get(uri) + .then(handleResponse(resolve)) + .catch(handleError(reject)); + }); + }, +}; + +export default verse; \ No newline at end of file