Skip to content

Commit d2c39a6

Browse files
committed
Add tests
1 parent d318503 commit d2c39a6

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

cdn.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const CDN = require('./cdn.js');
2+
3+
describe('CDN Tests', () => {
4+
const cdn = new CDN({
5+
dir: './cdn/',
6+
extension: '.cdn.json',
7+
ttl: 3600,
8+
});
9+
10+
test('Constructor', () => {
11+
expect(cdn.dir).toBe('./cdn/');
12+
expect(cdn.extension).toBe('.cdn.json');
13+
expect(cdn.ttl).toBe(3600);
14+
});
15+
16+
async function cmsdata() { // Example fetching and caching CMS data
17+
if (cdn.valid('cms')) return cdn.get('cms'); // Return cached CMS data if valid
18+
try {
19+
console.log('Writing...')
20+
cdn.cache('cms', { 1: 2 }); // Cache the CMS data
21+
return { 1: 2 };
22+
} catch {
23+
try {
24+
return cdn.get('cms'); // Retrieve cached CMS data
25+
} catch {
26+
console.error('Failed to fetch CMS data and no cached data found.');
27+
return null;
28+
};
29+
};
30+
};
31+
32+
test('Cache', async () => {
33+
var cms = await cmsdata();
34+
expect(cms).toEqual({ 1: 2 });
35+
});
36+
37+
test('Get', async () => {
38+
var cms = await cmsdata();
39+
expect(cms).toEqual({ 1: 2 });
40+
});
41+
});

jest.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
testEnvironment: 'node',
3+
collectCoverage: false,
4+
testMatch: ['**/*.test.js'],
5+
verbose: true
6+
};

0 commit comments

Comments
 (0)