cloudku-murotal
Version:
JavaScript client for Murotal API - Islamic prayer times, Quran, Hadith, and Dua
183 lines (153 loc) ⢠7.9 kB
JavaScript
const MurotalAPI = require('./main.cjs');
class TestRunner {
constructor() {
this.api = new MurotalAPI();
this.passed = 0;
this.failed = 0;
this.total = 0;
}
async test(name, testFn) {
this.total++;
process.stdout.write(`Testing ${name}... `);
try {
await testFn();
console.log('ā
PASS');
this.passed++;
} catch (error) {
console.log('ā FAIL');
console.log(` Error: ${error.message}`);
this.failed++;
}
}
async runAll() {
console.log('š Starting Murotal API Tests\n');
await this.test('Cities API', async () => {
const result = await this.api.getCities();
if (!result.status || result.status !== 'success') throw new Error('Invalid response');
if (!result.result) throw new Error('No cities data');
});
await this.test('Search Cities', async () => {
const result = await this.api.searchCities('jakarta');
if (!result.status || result.status !== 'success') throw new Error('Invalid response');
});
await this.test('Today Hijri Date', async () => {
const result = await this.api.getTodayHijriDate();
if (!result.status || result.status !== 'success') throw new Error('Invalid response');
if (!result.result) throw new Error('No hijri date');
});
await this.test('All Asmaul Husna', async () => {
const result = await this.api.getAllHusna();
if (!result.status || result.status !== 'success') throw new Error('Invalid response');
if (!Array.isArray(result.result)) throw new Error('Result should be array');
});
await this.test('Random Asmaul Husna', async () => {
const result = await this.api.getRandomHusna();
if (!result.status || result.status !== 'success') throw new Error('Invalid response');
if (!result.result) throw new Error('No husna data');
});
await this.test('Husna by Number', async () => {
const result = await this.api.getHusnaByNumber(1);
if (!result.status || result.status !== 'success') throw new Error('Invalid response');
if (!result.result) throw new Error('No husna data');
});
await this.test('Surah List', async () => {
const result = await this.api.getSurahList();
if (!result.status || result.status !== 'success') throw new Error('Invalid response');
if (!Array.isArray(result.result)) throw new Error('Result should be array');
});
await this.test('Random Surah', async () => {
const result = await this.api.getRandomSurah();
if (!result.status || result.status !== 'success') throw new Error('Invalid response');
if (!result.result) throw new Error('No surah data');
});
await this.test('Surah by Number', async () => {
const result = await this.api.getSurahByNumber(1);
if (!result.status || result.status !== 'success') throw new Error('Invalid response');
if (!result.result) throw new Error('No surah data');
});
await this.test('Single Ayah', async () => {
const result = await this.api.getSingleAyah(1, 1);
if (!result.status || result.status !== 'success') throw new Error('Invalid response');
if (!result.result) throw new Error('No ayah data');
});
await this.test('Random Ayah', async () => {
const result = await this.api.getRandomAyah();
if (!result.status || result.status !== 'success') throw new Error('Invalid response');
if (!result.result) throw new Error('No ayah data');
});
await this.test('Ayah by Number', async () => {
const result = await this.api.getAyahByNumber(1);
if (!result.status || result.status !== 'success') throw new Error('Invalid response');
if (!result.result) throw new Error('No ayah data');
});
await this.test('Juz', async () => {
const result = await this.api.getJuz(1);
if (!result.status || result.status !== 'success') throw new Error('Invalid response');
if (!result.result) throw new Error('No juz data');
});
await this.test('All Themes', async () => {
const result = await this.api.getAllThemes();
if (!result.status || result.status !== 'success') throw new Error('Invalid response');
if (!Array.isArray(result.result)) throw new Error('Result should be array');
});
await this.test('Doa Sources', async () => {
const result = await this.api.getDoaSources();
if (!result.status || result.status !== 'success') throw new Error('Invalid response');
if (!result.result) throw new Error('No doa sources');
});
await this.test('Random Doa', async () => {
const result = await this.api.getRandomDoa();
if (!result.status || result.status !== 'success') throw new Error('Invalid response');
if (!result.result) throw new Error('No doa data');
});
await this.test('Search Doa', async () => {
const result = await this.api.searchDoa('harian');
if (!result.status || result.status !== 'success') throw new Error('Invalid response');
});
await this.test('Random Hadist Arbain', async () => {
const result = await this.api.getRandomHadistArbain();
if (!result.status || result.status !== 'success') throw new Error('Invalid response');
if (!result.result) throw new Error('No hadist data');
});
await this.test('Random Hadist Bukhari', async () => {
const result = await this.api.getRandomHadistBukhari();
if (!result.status || result.status !== 'success') throw new Error('Invalid response');
if (!result.result) throw new Error('No hadist data');
});
await this.test('Custom Base URL', async () => {
const customApi = new MurotalAPI('https://example.com');
if (customApi.baseURL !== 'https://example.com') throw new Error('Base URL not set');
});
await this.test('Prayer Times with Parameters', async () => {
try {
await this.api.getPrayerTimes('1', '2024-01-01');
} catch (error) {
if (!error.message.includes('HTTP')) throw error;
}
});
await this.test('Error Handling', async () => {
try {
await this.api.getHusnaByNumber('invalid');
throw new Error('Should have thrown error');
} catch (error) {
if (!error.message.includes('HTTP')) throw new Error('Wrong error type');
}
});
this.showResults();
}
showResults() {
console.log('\nš Test Results:');
console.log(`ā
Passed: ${this.passed}`);
console.log(`ā Failed: ${this.failed}`);
console.log(`š Total: ${this.total}`);
console.log(`š Success Rate: ${((this.passed / this.total) * 100).toFixed(1)}%`);
if (this.failed === 0) {
console.log('\nš All tests passed!');
} else {
console.log('\nā ļø Some tests failed. Check the errors above.');
process.exit(1);
}
}
}
const runner = new TestRunner();
runner.runAll().catch(console.error);