Skip to content

Commit

Permalink
feat: verse api wrapped and tested all the methods for querying verses
Browse files Browse the repository at this point in the history
  • Loading branch information
PrantaDas committed Aug 6, 2024
1 parent abf0ab3 commit bd4d879
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 1 deletion.
112 changes: 112 additions & 0 deletions src/quran/verse.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
22 changes: 21 additions & 1 deletion src/quran/verse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,24 @@ const verse: VerseApi = {
.catch(handleError(reject));
});
},
};
getSpecificVerseByVerseKey(verse_key: string, query?: VerseQuery): Promise<VerseResponse | AxiosError | Error> {
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<VerseResponse | AxiosError | Error> {
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;

0 comments on commit bd4d879

Please sign in to comment.