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 3.5 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
  1. import type { Response } from 'node-fetch';
  2. import fetch from 'node-fetch';
  3. import { fetchWithCache, disableCache, enableCache, clearCache } from '../src/cache';
  4. jest.mock('node-fetch');
  5. const mockedFetch = jest.mocked(fetch);
  6. const mockedFetchResponse = (ok: boolean, response: object) => {
  7. return {
  8. ok,
  9. status: 200,
  10. text: () => Promise.resolve(JSON.stringify(response)),
  11. headers: {
  12. get: (name: string) => {
  13. if (name === 'content-type') {
  14. return 'application/json';
  15. }
  16. return null;
  17. },
  18. },
  19. } as unknown as Response;
  20. };
  21. describe('fetchWithCache', () => {
  22. afterEach(() => {
  23. mockedFetch.mockReset();
  24. });
  25. it('should not cache data with failed request', async () => {
  26. enableCache();
  27. const url = 'https://api.example.com/data';
  28. const response = { data: 'test data' };
  29. mockedFetch.mockResolvedValueOnce(mockedFetchResponse(false, response));
  30. const result = await fetchWithCache(url, {}, 1000);
  31. expect(mockedFetch).toHaveBeenCalledTimes(1);
  32. expect(result).toEqual({ cached: false, data: response });
  33. });
  34. it('should fetch data with cache enabled', async () => {
  35. enableCache();
  36. const url = 'https://api.example.com/data';
  37. const response = { data: 'test data' };
  38. mockedFetch.mockResolvedValueOnce(mockedFetchResponse(true, response));
  39. const result = await fetchWithCache(url, {}, 1000);
  40. expect(mockedFetch).toHaveBeenCalledTimes(1);
  41. expect(result).toEqual({ cached: false, data: response });
  42. });
  43. it('should fetch data with cache enabled after previous test', async () => {
  44. const url = 'https://api.example.com/data';
  45. const response = { data: 'test data' };
  46. mockedFetch.mockResolvedValueOnce(mockedFetchResponse(true, response));
  47. const result = await fetchWithCache(url, {}, 1000);
  48. expect(mockedFetch).toHaveBeenCalledTimes(0);
  49. expect(result).toEqual({ cached: true, data: response });
  50. });
  51. it('should only fetch data once with cache enabled', async () => {
  52. enableCache();
  53. clearCache();
  54. const url = 'https://api.example.com/data';
  55. const response = { data: 'test data' };
  56. mockedFetch.mockResolvedValueOnce(mockedFetchResponse(true, response));
  57. mockedFetch.mockRejectedValue(new Error('Should not be called'));
  58. const [a, b] = await Promise.all([
  59. fetchWithCache(url, {}, 1000),
  60. fetchWithCache(url, {}, 1000),
  61. ]);
  62. expect(mockedFetch).toHaveBeenCalledTimes(1);
  63. expect(a).toEqual({ cached: false, data: response });
  64. expect(b).toEqual({ cached: true, data: response });
  65. });
  66. it('should fetch data without cache for a single test', async () => {
  67. disableCache();
  68. const url = 'https://api.example.com/data';
  69. const response = { data: 'test data' };
  70. mockedFetch.mockResolvedValueOnce(mockedFetchResponse(true, response));
  71. const result = await fetchWithCache(url, {}, 1000);
  72. expect(mockedFetch).toHaveBeenCalledTimes(1);
  73. expect(result).toEqual({ cached: false, data: response });
  74. enableCache();
  75. });
  76. it('should still fetch data without cache for a single test', async () => {
  77. disableCache();
  78. const url = 'https://api.example.com/data';
  79. const response = { data: 'test data' };
  80. mockedFetch.mockResolvedValueOnce(mockedFetchResponse(true, response));
  81. const result = await fetchWithCache(url, {}, 1000);
  82. expect(mockedFetch).toHaveBeenCalledTimes(1);
  83. expect(result).toEqual({ cached: false, data: response });
  84. enableCache();
  85. });
  86. });
Tip!

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

Comments

Loading...