smartcache/test/test.ts

55 lines
1.4 KiB
TypeScript
Raw Normal View History

2018-12-02 14:39:52 +00:00
import { expect, tap } from '@pushrocks/tapbundle';
2022-06-26 18:36:22 +00:00
import * as smartcache from '../ts/index.js';
2018-07-12 19:32:20 +00:00
2018-07-12 21:52:06 +00:00
let smartcacheInstance: smartcache.SmartCache;
2018-07-12 19:32:20 +00:00
2018-12-02 14:39:52 +00:00
tap.test('should create a valid instance of SmartCache', async () => {
2018-07-12 21:52:06 +00:00
smartcacheInstance = new smartcache.SmartCache();
2022-06-26 18:36:22 +00:00
expect(smartcacheInstance).toBeInstanceOf(smartcache.SmartCache);
2018-07-12 21:52:06 +00:00
});
2022-06-26 18:36:22 +00:00
tap.test('try to get async responses', async (toolsArg) => {
2021-04-21 19:52:53 +00:00
let response1Counter = 0;
2018-07-12 21:52:06 +00:00
const getResponse = async () => {
const response = await smartcacheInstance
.cacheReturn(async () => {
2020-02-03 20:08:57 +00:00
console.log('function 1 ran');
2021-04-21 19:52:53 +00:00
response1Counter++;
2018-12-02 14:39:52 +00:00
return 'hello';
2018-07-12 21:52:06 +00:00
}, 1000)
2021-04-21 19:52:53 +00:00
.catch((err) => {
2018-07-12 21:52:06 +00:00
console.log(err);
});
2018-12-02 14:39:52 +00:00
console.log('response is:');
2018-07-12 21:52:06 +00:00
console.log(response);
};
2018-07-12 19:32:20 +00:00
2021-04-21 19:52:53 +00:00
let response2Counter = 0;
2018-07-12 21:52:06 +00:00
const getResponse2 = async () => {
const response = await smartcacheInstance
.cacheReturn(async () => {
2018-12-02 14:39:52 +00:00
console.log('function 2 ran');
2021-04-21 19:52:53 +00:00
response2Counter++;
2020-02-03 20:08:57 +00:00
return 'hello there!';
2018-07-12 21:52:06 +00:00
}, 1000)
2021-04-21 19:52:53 +00:00
.catch((err) => {
2018-07-12 21:52:06 +00:00
console.log(err);
});
2018-12-02 14:39:52 +00:00
console.log('response is:');
2018-07-12 21:52:06 +00:00
console.log(response);
};
2020-02-03 20:08:57 +00:00
await getResponse();
await getResponse();
await getResponse2();
await getResponse2();
2022-06-26 18:36:22 +00:00
await toolsArg.delayFor(500);
2021-04-21 19:52:53 +00:00
await getResponse();
2022-06-26 18:36:22 +00:00
await toolsArg.delayFor(2000);
2021-04-21 19:52:53 +00:00
await getResponse2();
2018-07-12 19:32:20 +00:00
});
2018-07-12 21:52:06 +00:00
tap.start({
2021-04-21 19:52:53 +00:00
throwOnError: true,
2018-07-12 21:52:06 +00:00
});