Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

cache.test.ts 2.7 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  1. import { fetchJsonWithCache, disableCache, enableCache } from '../src/cache.js';
  2. import fetch, { Response } from 'node-fetch';
  3. jest.mock('node-fetch');
  4. const mockedFetch = fetch as jest.MockedFunction<typeof fetch>;
  5. describe('fetchJsonWithCache', () => {
  6. afterEach(() => {
  7. mockedFetch.mockReset();
  8. });
  9. it('should not cache data with failed request', async () => {
  10. enableCache();
  11. const url = 'https://api.example.com/data';
  12. const response = { data: 'test data' };
  13. mockedFetch.mockResolvedValueOnce({
  14. ok: false,
  15. json: () => Promise.resolve(response),
  16. } as Response);
  17. const result = await fetchJsonWithCache(url, {}, 1000);
  18. expect(mockedFetch).toHaveBeenCalledTimes(1);
  19. expect(result).toEqual({ cached: false, data: response });
  20. });
  21. it('should fetch data with cache enabled', async () => {
  22. enableCache();
  23. const url = 'https://api.example.com/data';
  24. const response = { data: 'test data' };
  25. mockedFetch.mockResolvedValueOnce({
  26. ok: true,
  27. json: () => Promise.resolve(response),
  28. } as Response);
  29. const result = await fetchJsonWithCache(url, {}, 1000);
  30. expect(mockedFetch).toHaveBeenCalledTimes(1);
  31. expect(result).toEqual({ cached: false, data: response });
  32. });
  33. it('should fetch data with cache enabled after previous test', async () => {
  34. const url = 'https://api.example.com/data';
  35. const response = { data: 'test data' };
  36. mockedFetch.mockResolvedValueOnce({
  37. ok: true,
  38. json: () => Promise.resolve(response),
  39. } as Response);
  40. const result = await fetchJsonWithCache(url, {}, 1000);
  41. expect(mockedFetch).toHaveBeenCalledTimes(0);
  42. expect(result).toEqual({ cached: true, data: response });
  43. });
  44. it('should fetch data without cache for a single test', async () => {
  45. disableCache();
  46. const url = 'https://api.example.com/data';
  47. const response = { data: 'test data' };
  48. mockedFetch.mockResolvedValueOnce({
  49. json: () => Promise.resolve(response),
  50. } as Response);
  51. const result = await fetchJsonWithCache(url, {}, 1000);
  52. expect(mockedFetch).toHaveBeenCalledTimes(1);
  53. expect(result).toEqual({ cached: false, data: response });
  54. enableCache();
  55. });
  56. it('should still fetch data without cache for a single test', async () => {
  57. disableCache();
  58. const url = 'https://api.example.com/data';
  59. const response = { data: 'test data' };
  60. mockedFetch.mockResolvedValueOnce({
  61. ok: true,
  62. json: () => Promise.resolve(response),
  63. } as Response);
  64. const result = await fetchJsonWithCache(url, {}, 1000);
  65. expect(mockedFetch).toHaveBeenCalledTimes(1);
  66. expect(result).toEqual({ cached: false, data: response });
  67. enableCache();
  68. });
  69. });
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...