Skip to content

Commit

Permalink
remove experimental features, refactor, init storage unit tests (#54)
Browse files Browse the repository at this point in the history
* remove experimental features, refactor, init units for storage

* hide storage API on plugin, impove tests
  • Loading branch information
denbon05 authored Oct 31, 2024
1 parent b2754ea commit c10798c
Show file tree
Hide file tree
Showing 20 changed files with 2,598 additions and 9,118 deletions.
14 changes: 5 additions & 9 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,16 @@
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": [
"@typescript-eslint"
],
"plugins": ["@typescript-eslint"],
"rules": {
"import/extensions": "off",
"no-console": "warn",
"no-shadow": "off",
"@typescript-eslint/no-shadow": "error",
"no-underscore-dangle": "off",
"indent": "off",
"@typescript-eslint/indent": [
"error",
2
],
"@typescript-eslint/consistent-type-imports": "warn"
"@typescript-eslint/indent": ["error", 2],
"@typescript-eslint/consistent-type-imports": "warn",
"import/prefer-default-export": "off"
}
}
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ axios.get(url);
<p><b>app.lcache</b> available inside your app</p>

```ts
interface IStorage {
interface ILightCache {
// Get cached data
get(key: string): any;
get<T>(key: string): T;

// Set data to cache
set(key: string, value: any): void;
set<T>(key: string, value: T): void;

// Check if data exists in cache
has(key: string): boolean;
Expand Down
21 changes: 19 additions & 2 deletions __tests__/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,60 @@
/* eslint-disable import/prefer-default-export */
import fastify from 'fastify';
import lcache from '../../lib';
import type { ICacheOptions } from '../../lib/types/lcache';

export const getApp = (options: ICacheOptions = {}) => {
/** Injected spy functions in the tested endpoints */
export const spies = {
getPing: jest.fn(),
postPing: jest.fn(),
deletePing: jest.fn(),
getJSON: jest.fn(),
postPost: jest.fn(),
getDate: jest.fn(),
putPut: jest.fn(),
};

export const getApp = (options: Partial<ICacheOptions> = {}) => {
const app = fastify();
app.register(lcache, options);

app.after(() => {
app.get('/ping', async (_req, reply) => {
spies.getPing();
reply.send('pong');
});

app.post('/ping', async (_req, reply) => {
spies.postPing();
reply.send('pong');
});

app.delete('/ping', async (_req, reply) => {
spies.deletePing();
reply.send('pong');
});

app.get('/json', async (_req, reply) => {
spies.getJSON();
reply.send({ hello: 'world' });
});

// eslint-disable-next-line @typescript-eslint/no-explicit-any
app.post('/post', async (req: any, reply) => {
spies.postPost();
reply.status(201);
reply.send(req.body.data);
});

app.get('/date', async (_req, reply) => {
spies.getDate();
await new Promise((resolve) => {
setTimeout(() => resolve(reply.send(Date.now())), Math.random() * 100);
});
});

// eslint-disable-next-line @typescript-eslint/no-explicit-any
app.put('/put', async (req: any, reply) => {
spies.putPut();
reply.status(201).send(req.body.data);
});
});
Expand Down
Loading

0 comments on commit c10798c

Please sign in to comment.