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 4.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
  1. import { fetchWithCache, disableCache, enableCache, clearCache } from '../src/cache';
  2. const mockedFetch = jest.mocked(jest.fn());
  3. global.fetch = mockedFetch;
  4. const mockedFetchResponse = (ok: boolean, response: object): Response => {
  5. const responseText = JSON.stringify(response);
  6. return {
  7. ok,
  8. status: ok ? 200 : 400,
  9. statusText: ok ? 'OK' : 'Bad Request',
  10. text: () => Promise.resolve(responseText),
  11. json: () => Promise.resolve(response),
  12. headers: new Headers({
  13. 'content-type': 'application/json',
  14. 'x-session-id': '45',
  15. }),
  16. } as Response;
  17. };
  18. describe('fetchWithCache', () => {
  19. afterEach(() => {
  20. mockedFetch.mockReset();
  21. });
  22. it('should not cache data with failed request', async () => {
  23. enableCache();
  24. const url = 'https://api.example.com/data';
  25. const response = { data: 'test data' };
  26. mockedFetch.mockResolvedValueOnce(mockedFetchResponse(false, response));
  27. const result = await fetchWithCache(url, {}, 1000);
  28. expect(mockedFetch).toHaveBeenCalledTimes(1);
  29. expect(result).toEqual({
  30. cached: false,
  31. data: response,
  32. headers: { 'content-type': 'application/json', 'x-session-id': '45' },
  33. status: 400,
  34. statusText: 'Bad Request',
  35. });
  36. });
  37. it('should fetch data with cache enabled', async () => {
  38. enableCache();
  39. const url = 'https://api.example.com/data';
  40. const response = {
  41. data: 'test data',
  42. };
  43. mockedFetch.mockResolvedValueOnce(mockedFetchResponse(true, response));
  44. const result = await fetchWithCache(url, {}, 1000);
  45. expect(mockedFetch).toHaveBeenCalledTimes(1);
  46. expect(result).toEqual({
  47. cached: false,
  48. data: response,
  49. status: 200,
  50. statusText: 'OK',
  51. headers: { 'x-session-id': '45', 'content-type': 'application/json' },
  52. });
  53. });
  54. it('should fetch data with cache enabled after previous test', async () => {
  55. const url = 'https://api.example.com/data';
  56. const response = { data: 'test data' };
  57. mockedFetch.mockResolvedValueOnce(mockedFetchResponse(true, response));
  58. const result = await fetchWithCache(url, {}, 1000);
  59. expect(mockedFetch).toHaveBeenCalledTimes(0);
  60. expect(result).toEqual({
  61. cached: true,
  62. data: response,
  63. status: 200,
  64. statusText: 'OK',
  65. headers: { 'content-type': 'application/json', 'x-session-id': '45' },
  66. });
  67. });
  68. it('should only fetch data once with cache enabled', async () => {
  69. enableCache();
  70. clearCache();
  71. const url = 'https://api.example.com/data';
  72. const response = { data: 'test data' };
  73. mockedFetch.mockResolvedValueOnce(mockedFetchResponse(true, response));
  74. mockedFetch.mockRejectedValue(new Error('Should not be called'));
  75. const [a, b] = await Promise.all([
  76. fetchWithCache(url, {}, 1000),
  77. fetchWithCache(url, {}, 1000),
  78. ]);
  79. expect(mockedFetch).toHaveBeenCalledTimes(1);
  80. expect(a).toEqual({
  81. cached: false,
  82. data: response,
  83. status: 200,
  84. statusText: 'OK',
  85. headers: { 'x-session-id': '45', 'content-type': 'application/json' },
  86. });
  87. expect(b).toEqual({
  88. cached: true,
  89. data: response,
  90. status: 200,
  91. statusText: 'OK',
  92. headers: { 'x-session-id': '45', 'content-type': 'application/json' },
  93. });
  94. });
  95. it('should fetch data without cache for a single test', async () => {
  96. disableCache();
  97. const url = 'https://api.example.com/data';
  98. const response = { data: 'test data' };
  99. mockedFetch.mockResolvedValueOnce(mockedFetchResponse(true, response));
  100. const result = await fetchWithCache(url, {}, 1000);
  101. expect(mockedFetch).toHaveBeenCalledTimes(1);
  102. expect(result).toEqual({
  103. cached: false,
  104. data: response,
  105. status: 200,
  106. statusText: 'OK',
  107. headers: { 'content-type': 'application/json', 'x-session-id': '45' },
  108. });
  109. enableCache();
  110. });
  111. it('should still fetch data without cache for a single test', async () => {
  112. disableCache();
  113. const url = 'https://api.example.com/data';
  114. const response = { data: 'test data' };
  115. mockedFetch.mockResolvedValueOnce(mockedFetchResponse(true, response));
  116. const result = await fetchWithCache(url, {}, 1000);
  117. expect(mockedFetch).toHaveBeenCalledTimes(1);
  118. expect(result).toEqual({
  119. cached: false,
  120. data: response,
  121. status: 200,
  122. statusText: 'OK',
  123. headers: { 'content-type': 'application/json', 'x-session-id': '45' },
  124. });
  125. enableCache();
  126. });
  127. });
Tip!

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

Comments

Loading...