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

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

Comments

Loading...