Skip to content

Commit 3176f99

Browse files
committed
test: increase coverage
1 parent 09bb88f commit 3176f99

2 files changed

Lines changed: 60 additions & 4 deletions

File tree

src/getter.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,11 @@ function getFromDB(objectStore, url) {
5454

5555
async function loadResource(config, url) {
5656
if (! url.includes('://')) {
57-
url = url.replace(/^\//, '');
58-
url = `${config.protocol}://${config.hostName}${config.versionPath}${url}`
57+
if (url.startsWith('/api/v2/')) {
58+
url = `${config.protocol}://${config.hostName}${url}`
59+
} else if (!url.includes('://')) {
60+
url = `${config.protocol}://${config.hostName}${config.versionPath}${url}`
61+
}
5962
}
6063
if (canUseCache(config, db)) {
6164
const transaction = db.transaction("cache", "readonly");
@@ -104,7 +107,7 @@ function sizeCache(config) {
104107
request.onerror = () => reject(request.error);
105108
});
106109
} else {
107-
return Promise.reject()
110+
return Promise.reject(new Error('Cache not available'))
108111
}
109112
}
110113

@@ -146,7 +149,7 @@ function clearCache(config) {
146149
request.onerror = () => reject(request.error);
147150
});
148151
} else {
149-
return Promise.reject()
152+
return Promise.reject(new Error('Cache not available'))
150153
}
151154
}
152155

test/test.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ describe("pokedex", { timeout: 30000 }, function () {
77
let p2;
88

99
const id = 2;
10+
const string = 'pokemon/33';
1011
const path = '/api/v2/pokemon/34';
1112
const url = 'https://pokeapi.co/api/v2/pokemon/35';
1213
const interval = { limit: 10, offset: 34 };
@@ -31,6 +32,36 @@ describe("pokedex", { timeout: 30000 }, function () {
3132
});
3233
});
3334

35+
// --- Resource Methods ---
36+
describe(".resource()", function () {
37+
it("should succeed with a single path", async function () {
38+
const res = await p1.resource(path);
39+
assert.ok(res.height, "Response should have height");
40+
});
41+
it("should succeed with a single path", async function () {
42+
const res = await p1.resource(string);
43+
assert.ok(res.height, "Response should have height");
44+
});
45+
it("should succeed with an array of paths", async function () {
46+
const res = await p1.resource([path, url, string]);
47+
assert.strictEqual(res.length, 3);
48+
assert.ok(res[0].height, 'Should have property height');
49+
assert.ok(res[1].height, 'Should have property height');
50+
assert.ok(res[2].height, 'Should have property height');
51+
});
52+
it("should succeed with an array of paths with trailing /", async function () {
53+
const res = await p1.resource([`${path}/`, `${url}/`, `${string}/`]);
54+
assert.strictEqual(res.length, 3);
55+
assert.ok(res[0].height, 'Should have property height');
56+
assert.ok(res[1].height, 'Should have property height');
57+
assert.ok(res[2].height, 'Should have property height');
58+
});
59+
it("should fail with an invalid path", async function () {
60+
const result = await p1.resource(123);
61+
assert.strictEqual(result, "String or Array is required");
62+
});
63+
});
64+
3465
// --- List Methods ---
3566
describe(".getPokemonsList()", function () {
3667
it("should succeed with default interval", async function () {
@@ -49,4 +80,26 @@ describe("pokedex", { timeout: 30000 }, function () {
4980
);
5081
});
5182
});
83+
84+
// --- IndexedDB ---
85+
describe("IndexedDB", function () {
86+
it(".getCacheLength() should throw an error", async function () {
87+
await assert.rejects(
88+
p1.getCacheLength(),
89+
Error
90+
);
91+
});
92+
it(".clearCache() should throw an error", async function () {
93+
await assert.rejects(
94+
p1.clearCache(),
95+
Error
96+
);
97+
});
98+
it(".invalidateCache() should throw an error", async function () {
99+
await assert.rejects(
100+
p1.invalidateCache(),
101+
Error
102+
);
103+
});
104+
});
52105
});

0 commit comments

Comments
 (0)