create-snippet
Version:
npx tool for generating code snippets
78 lines (77 loc) • 3.64 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const globals_1 = require("@jest/globals");
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const cache_module_1 = require("./cache.module");
(0, globals_1.describe)('ModuleCache', () => {
const cacheDir = path_1.default.join(__dirname, '..', '..', '__cache__');
let cache;
(0, globals_1.beforeAll)(() => {
// Создаем временную директорию для тестового кэша
if (!fs_1.default.existsSync(cacheDir)) {
fs_1.default.mkdirSync(cacheDir);
}
});
(0, globals_1.afterAll)(() => {
// Удаляем тестовый кэш
fs_1.default.rmdirSync(cacheDir, { recursive: true });
});
(0, globals_1.beforeEach)(() => {
// Создаем экземпляр класса для каждого теста
cache = new cache_module_1.ModuleCache();
});
(0, globals_1.afterEach)(() => {
// Удаляем все файлы из тестового кэша после каждого теста
fs_1.default.readdirSync(cacheDir).forEach((file) => {
fs_1.default.unlinkSync(path_1.default.join(cacheDir, file));
});
});
(0, globals_1.describe)('get', () => {
(0, globals_1.test)('should return null for non-existing key', () => {
(0, globals_1.expect)(cache.get('non-existing-key')).toBeNull();
});
(0, globals_1.test)('should return null for expired key', () => {
cache.set('key', 'value', -1000);
(0, globals_1.expect)(cache.get('key')).toBeNull();
});
(0, globals_1.test)('should return cached value for valid key', () => {
cache.set('key', 'value', 1000);
(0, globals_1.expect)(cache.get('key')).toEqual('value');
});
});
(0, globals_1.describe)('set', () => {
(0, globals_1.test)('should add new key-value pair to cache', () => {
cache.set('key', 'value');
(0, globals_1.expect)(cache.get('key')).toEqual(null);
});
(0, globals_1.test)('should overwrteste existing key-value pair in cache', () => {
cache.set('key', 'old-value');
cache.set('key', 'new-value');
(0, globals_1.expect)(cache.get('key')).toEqual(null);
});
(0, globals_1.test)('should set expiration for key-value pair', () => {
const expiration = 1000;
cache.set('key', 'value', expiration);
const filePath = path_1.default.join(cacheDir, 'key.json');
const content = fs_1.default.readFileSync(filePath, 'utf-8');
const { expiration: fileExpiration } = JSON.parse(content);
(0, globals_1.expect)(fileExpiration).toBeGreaterThan(Date.now());
(0, globals_1.expect)(fileExpiration).toBeLessThanOrEqual(Date.now() + expiration);
});
});
(0, globals_1.describe)('delete', () => {
(0, globals_1.test)('should remove key-value pair from cache', () => {
cache.set('key', 'value');
cache.delete('key');
(0, globals_1.expect)(cache.get('key')).toBeNull();
});
(0, globals_1.test)('should do nothing for non-existing key', () => {
cache.delete('non-existing-key');
(0, globals_1.expect)(cache.get('non-existing-key')).toBeNull();
});
});
});