|
1 | 1 | import { expect } from 'chai'; |
2 | 2 | import { describe, it } from 'mocha'; |
3 | 3 |
|
| 4 | +import { expectPromise } from '../../__testUtils__/expectPromise.js'; |
4 | 5 | import { resolveOnNextTick } from '../../__testUtils__/resolveOnNextTick.js'; |
5 | 6 |
|
6 | 7 | import { isPromise } from '../isPromise.js'; |
7 | 8 | import { withCache } from '../withCache.js'; |
8 | 9 |
|
9 | 10 | describe('withCache', () => { |
10 | 11 | it('returns asynchronously using asynchronous cache', async () => { |
11 | | - let cached: string | undefined; |
| 12 | + let cached: string | Error | undefined; |
12 | 13 | let getAttempts = 0; |
13 | 14 | let cacheHits = 0; |
14 | 15 | const customCache = { |
15 | | - set: async (result: string) => { |
| 16 | + set: async (result: string | Error) => { |
16 | 17 | await resolveOnNextTick(); |
17 | 18 | cached = result; |
18 | 19 | }, |
@@ -46,11 +47,11 @@ describe('withCache', () => { |
46 | 47 | }); |
47 | 48 |
|
48 | 49 | it('returns synchronously using cache with sync getter and async setter', async () => { |
49 | | - let cached: string | undefined; |
| 50 | + let cached: string | Error | undefined; |
50 | 51 | let getAttempts = 0; |
51 | 52 | let cacheHits = 0; |
52 | 53 | const customCache = { |
53 | | - set: async (result: string) => { |
| 54 | + set: async (result: string | Error) => { |
54 | 55 | await resolveOnNextTick(); |
55 | 56 | cached = result; |
56 | 57 | }, |
@@ -80,11 +81,11 @@ describe('withCache', () => { |
80 | 81 | }); |
81 | 82 |
|
82 | 83 | it('returns asynchronously using cache with async getter and sync setter', async () => { |
83 | | - let cached: string | undefined; |
| 84 | + let cached: string | Error | undefined; |
84 | 85 | let getAttempts = 0; |
85 | 86 | let cacheHits = 0; |
86 | 87 | const customCache = { |
87 | | - set: (result: string) => { |
| 88 | + set: (result: string | Error) => { |
88 | 89 | cached = result; |
89 | 90 | }, |
90 | 91 | get: () => { |
@@ -151,4 +152,74 @@ describe('withCache', () => { |
151 | 152 | expect(getAttempts).to.equal(2); |
152 | 153 | expect(cacheHits).to.equal(0); |
153 | 154 | }); |
| 155 | + |
| 156 | + it('caches fn errors with sync cache', () => { |
| 157 | + let cached: string | Error | undefined; |
| 158 | + let getAttempts = 0; |
| 159 | + let cacheHits = 0; |
| 160 | + const customCache = { |
| 161 | + set: (result: string | Error) => { |
| 162 | + cached = result; |
| 163 | + }, |
| 164 | + get: () => { |
| 165 | + getAttempts++; |
| 166 | + if (cached !== undefined) { |
| 167 | + cacheHits++; |
| 168 | + } |
| 169 | + return cached; |
| 170 | + }, |
| 171 | + }; |
| 172 | + |
| 173 | + const fnWithCache = withCache((): string => { |
| 174 | + throw new Error('Oops'); |
| 175 | + }, customCache); |
| 176 | + |
| 177 | + expect(() => fnWithCache()).to.throw('Oops'); |
| 178 | + |
| 179 | + expect(getAttempts).to.equal(1); |
| 180 | + expect(cacheHits).to.equal(0); |
| 181 | + |
| 182 | + expect(() => fnWithCache()).to.throw('Oops'); |
| 183 | + |
| 184 | + expect(getAttempts).to.equal(2); |
| 185 | + expect(cacheHits).to.equal(1); |
| 186 | + }); |
| 187 | + |
| 188 | + it('caches fn errors with async cache', async () => { |
| 189 | + let cached: string | Error | undefined; |
| 190 | + let getAttempts = 0; |
| 191 | + let cacheHits = 0; |
| 192 | + const customCache = { |
| 193 | + set: async (result: string | Error) => { |
| 194 | + await resolveOnNextTick(); |
| 195 | + cached = result; |
| 196 | + }, |
| 197 | + get: () => { |
| 198 | + getAttempts++; |
| 199 | + if (cached !== undefined) { |
| 200 | + cacheHits++; |
| 201 | + } |
| 202 | + return Promise.resolve(cached); |
| 203 | + }, |
| 204 | + }; |
| 205 | + |
| 206 | + const fnWithCache = withCache((): string => { |
| 207 | + throw new Error('Oops'); |
| 208 | + }, customCache); |
| 209 | + |
| 210 | + const firstResultPromise = fnWithCache(); |
| 211 | + expect(isPromise(firstResultPromise)).to.equal(true); |
| 212 | + |
| 213 | + await expectPromise(firstResultPromise).toRejectWith('Oops'); |
| 214 | + expect(getAttempts).to.equal(1); |
| 215 | + expect(cacheHits).to.equal(0); |
| 216 | + |
| 217 | + const secondResultPromise = fnWithCache(); |
| 218 | + |
| 219 | + expect(isPromise(secondResultPromise)).to.equal(true); |
| 220 | + |
| 221 | + await expectPromise(secondResultPromise).toRejectWith('Oops'); |
| 222 | + expect(getAttempts).to.equal(2); |
| 223 | + expect(cacheHits).to.equal(1); |
| 224 | + }); |
154 | 225 | }); |
0 commit comments