2018-12-02 14:39:52 +00:00
|
|
|
import { expect, tap } from '@pushrocks/tapbundle';
|
|
|
|
import * as smartcache from '../ts/index';
|
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();
|
|
|
|
expect(smartcacheInstance).to.be.instanceof(smartcache.SmartCache);
|
|
|
|
});
|
|
|
|
|
2021-04-21 19:52:53 +00:00
|
|
|
tap.test('try to get async responses', async (tools) => {
|
|
|
|
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();
|
2021-04-21 19:52:53 +00:00
|
|
|
await tools.delayFor(500);
|
|
|
|
await getResponse();
|
|
|
|
await tools.delayFor(2000);
|
|
|
|
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
|
|
|
});
|