|
1 | 1 | "use strict";
|
2 |
| -var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
3 |
| - function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
| 2 | +var __awaiter = |
| 3 | + (this && this.__awaiter) || |
| 4 | + function (thisArg, _arguments, P, generator) { |
| 5 | + function adopt(value) { |
| 6 | + return value instanceof P |
| 7 | + ? value |
| 8 | + : new P(function (resolve) { |
| 9 | + resolve(value); |
| 10 | + }); |
| 11 | + } |
4 | 12 | return new (P || (P = Promise))(function (resolve, reject) {
|
5 |
| - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
6 |
| - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
7 |
| - function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
8 |
| - step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 13 | + function fulfilled(value) { |
| 14 | + try { |
| 15 | + step(generator.next(value)); |
| 16 | + } catch (e) { |
| 17 | + reject(e); |
| 18 | + } |
| 19 | + } |
| 20 | + function rejected(value) { |
| 21 | + try { |
| 22 | + step(generator["throw"](value)); |
| 23 | + } catch (e) { |
| 24 | + reject(e); |
| 25 | + } |
| 26 | + } |
| 27 | + function step(result) { |
| 28 | + result.done |
| 29 | + ? resolve(result.value) |
| 30 | + : adopt(result.value).then(fulfilled, rejected); |
| 31 | + } |
| 32 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
9 | 33 | });
|
10 |
| -}; |
11 |
| -var __importDefault = (this && this.__importDefault) || function (mod) { |
12 |
| - return (mod && mod.__esModule) ? mod : { "default": mod }; |
13 |
| -}; |
| 34 | + }; |
| 35 | +var __importDefault = |
| 36 | + (this && this.__importDefault) || |
| 37 | + function (mod) { |
| 38 | + return mod && mod.__esModule ? mod : { default: mod }; |
| 39 | + }; |
14 | 40 | Object.defineProperty(exports, "__esModule", { value: true });
|
15 | 41 | const src_1 = require("../src");
|
16 | 42 | const axios_1 = __importDefault(require("axios"));
|
17 | 43 | jest.setTimeout(10000);
|
18 | 44 | describe("GhostCache - API Caching Tests", () => {
|
19 |
| - const POKEMON_API = "https://pokeapi.co/api/v2/pokemon/ditto"; |
20 |
| - beforeAll(() => { |
21 |
| - // Enable caching with a TTL of 5 seconds, no persistent storage for testing |
22 |
| - (0, src_1.enableGhostCache)({ ttl: 5000, persistent: false }); |
23 |
| - }); |
24 |
| - afterEach(() => { |
25 |
| - (0, src_1.clearGhostCache)(); |
26 |
| - }); |
27 |
| - afterAll(() => { |
28 |
| - (0, src_1.disableGhostCache)(); |
29 |
| - }); |
30 |
| - it("fetches data from the Pokémon API via fetch", () => __awaiter(void 0, void 0, void 0, function* () { |
31 |
| - const response = yield fetch(POKEMON_API); |
32 |
| - const data = yield response.json(); |
33 |
| - expect(data).toHaveProperty("name", "ditto"); |
| 45 | + const POKEMON_API = "https://pokeapi.co/api/v2/pokemon/ditto"; |
| 46 | + beforeAll(() => { |
| 47 | + // Enable caching with a TTL of 5 seconds, no persistent storage for testing |
| 48 | + (0, src_1.enableGhostCache)({ ttl: 5000, persistent: false }); |
| 49 | + }); |
| 50 | + afterEach(() => { |
| 51 | + (0, src_1.clearGhostCache)(); |
| 52 | + }); |
| 53 | + afterAll(() => { |
| 54 | + (0, src_1.disableGhostCache)(); |
| 55 | + }); |
| 56 | + it("fetches data from the Pokémon API via fetch", () => |
| 57 | + __awaiter(void 0, void 0, void 0, function* () { |
| 58 | + const response = yield fetch(POKEMON_API); |
| 59 | + const data = yield response.json(); |
| 60 | + expect(data).toHaveProperty("name", "ditto"); |
34 | 61 | }));
|
35 |
| - it("returns cached data on subsequent fetch calls", () => __awaiter(void 0, void 0, void 0, function* () { |
36 |
| - const startTime = Date.now(); |
37 |
| - const response1 = yield fetch(POKEMON_API); |
38 |
| - const data1 = yield response1.json(); |
39 |
| - const networkTime = Date.now() - startTime; |
40 |
| - expect(data1).toHaveProperty("name", "ditto"); |
41 |
| - const cacheStartTime = Date.now(); |
42 |
| - const response2 = yield fetch(POKEMON_API); |
43 |
| - const data2 = yield response2.json(); |
44 |
| - const cacheTime = Date.now() - cacheStartTime; |
45 |
| - expect(data2).toHaveProperty("name", "ditto"); |
46 |
| - expect(cacheTime).toBeLessThan(networkTime); |
| 62 | + it("returns cached data on subsequent fetch calls", () => |
| 63 | + __awaiter(void 0, void 0, void 0, function* () { |
| 64 | + const startTime = Date.now(); |
| 65 | + const response1 = yield fetch(POKEMON_API); |
| 66 | + const data1 = yield response1.json(); |
| 67 | + const networkTime = Date.now() - startTime; |
| 68 | + expect(data1).toHaveProperty("name", "ditto"); |
| 69 | + const cacheStartTime = Date.now(); |
| 70 | + const response2 = yield fetch(POKEMON_API); |
| 71 | + const data2 = yield response2.json(); |
| 72 | + const cacheTime = Date.now() - cacheStartTime; |
| 73 | + expect(data2).toHaveProperty("name", "ditto"); |
| 74 | + expect(cacheTime).toBeLessThan(networkTime); |
47 | 75 | }));
|
48 |
| - it("expires cache after TTL for fetch", () => __awaiter(void 0, void 0, void 0, function* () { |
49 |
| - const response1 = yield fetch(POKEMON_API); |
50 |
| - const data1 = yield response1.json(); |
51 |
| - expect(data1).toHaveProperty("name", "ditto"); |
52 |
| - // Wait 6 seconds (beyond the 5-second TTL) |
53 |
| - yield new Promise(resolve => setTimeout(resolve, 6000)); |
54 |
| - const response2 = yield fetch(POKEMON_API); |
55 |
| - const data2 = yield response2.json(); |
56 |
| - expect(data2).toHaveProperty("name", "ditto"); |
| 76 | + it("expires cache after TTL for fetch", () => |
| 77 | + __awaiter(void 0, void 0, void 0, function* () { |
| 78 | + const response1 = yield fetch(POKEMON_API); |
| 79 | + const data1 = yield response1.json(); |
| 80 | + expect(data1).toHaveProperty("name", "ditto"); |
| 81 | + // Wait 6 seconds (beyond the 5-second TTL) |
| 82 | + yield new Promise((resolve) => setTimeout(resolve, 6000)); |
| 83 | + const response2 = yield fetch(POKEMON_API); |
| 84 | + const data2 = yield response2.json(); |
| 85 | + expect(data2).toHaveProperty("name", "ditto"); |
57 | 86 | }));
|
58 |
| - it("allows manual cache set and get", () => __awaiter(void 0, void 0, void 0, function* () { |
59 |
| - yield (0, src_1.setCache)("test-key", { foo: "bar" }); |
60 |
| - const cached = yield (0, src_1.getCache)("test-key"); |
61 |
| - expect(cached).toEqual({ foo: "bar" }); |
| 87 | + it("allows manual cache set and get", () => |
| 88 | + __awaiter(void 0, void 0, void 0, function* () { |
| 89 | + yield (0, src_1.setCache)("test-key", { foo: "bar" }); |
| 90 | + const cached = yield (0, src_1.getCache)("test-key"); |
| 91 | + expect(cached).toEqual({ foo: "bar" }); |
62 | 92 | }));
|
63 |
| - it("works with Axios", () => __awaiter(void 0, void 0, void 0, function* () { |
64 |
| - const api = axios_1.default.create(); |
65 |
| - (0, src_1.registerAxios)(api); |
66 |
| - const response1 = yield api.get(POKEMON_API); |
67 |
| - expect(response1.data).toHaveProperty("name", "ditto"); |
68 |
| - // Second request should come from cache |
69 |
| - const response2 = yield api.get(POKEMON_API); |
70 |
| - expect(response2.data).toHaveProperty("name", "ditto"); |
| 93 | + it("works with Axios", () => |
| 94 | + __awaiter(void 0, void 0, void 0, function* () { |
| 95 | + const api = axios_1.default.create(); |
| 96 | + (0, src_1.registerAxios)(api); |
| 97 | + const response1 = yield api.get(POKEMON_API); |
| 98 | + expect(response1.data).toHaveProperty("name", "ditto"); |
| 99 | + // Second request should come from cache |
| 100 | + const response2 = yield api.get(POKEMON_API); |
| 101 | + expect(response2.data).toHaveProperty("name", "ditto"); |
71 | 102 | }));
|
72 | 103 | });
|
0 commit comments