diff --git a/lib/utils/ens-cache.test.js b/lib/utils/ens-cache.test.js --- a/lib/utils/ens-cache.test.js +++ b/lib/utils/ens-cache.test.js @@ -24,6 +24,13 @@ return baseResolveName(ensName); }; +const baseGetAvatar = provider.getAvatar.bind(provider); +let timesGetAvatarCalled = 0; +provider.getAvatar = (ethAddress: string) => { + timesGetAvatarCalled++; + return baseGetAvatar(ethAddress); +}; + if (!process.env.ALCHEMY_API_KEY) { // Test only works if we can query blockchain console.log( @@ -138,7 +145,7 @@ const timesResolveNameCalledBeforeSingleFetch = timesResolveNameCalled; const ashoatAddrResult1 = await ensCache.getAddressForName(ashoatDotEth); expect(ashoatAddrResult1).toBe(ashoatAddr); - const timesResolveNamesCalledForSingleFetch = + const timesResolveNameCalledForSingleFetch = timesResolveNameCalled - timesResolveNameCalledBeforeSingleFetch; ensCache.clearCache(); @@ -153,7 +160,7 @@ timesResolveNameCalled - timesResolveNameCalledBeforeDoubleFetch; expect(timesResolveNamesCalledForDoubleFetch).toBe( - timesResolveNamesCalledForSingleFetch, + timesResolveNameCalledForSingleFetch, ); }); }); @@ -184,4 +191,44 @@ ensCache.getCachedAvatarURIForAddress(ashoatAddr); expect(ashoatAvatarResult).toBe(ashoatAvatar); }); + it("should have ashoat.eth's avatar cached", async () => { + if (!process.env.ALCHEMY_API_KEY) { + return; + } + const timesGetAvatarCalledBefore = timesGetAvatarCalled; + const ashoatAvatarResult = await ensCache.getAvatarURIForAddress( + ashoatAddr, + ); + expect(ashoatAvatarResult).toBe(ashoatAvatar); + expect(timesGetAvatarCalled).toBe(timesGetAvatarCalledBefore); + }); + it('should dedup simultaneous fetches', async () => { + if (!process.env.ALCHEMY_API_KEY) { + return; + } + + ensCache.clearCache(); + const timesGetAvatarCalledBeforeSingleFetch = timesGetAvatarCalled; + const ashoatAvatarResult1 = await ensCache.getAvatarURIForAddress( + ashoatAddr, + ); + expect(ashoatAvatarResult1).toBe(ashoatAvatar); + const timesGetAvatarCalledForSingleFetch = + timesGetAvatarCalled - timesGetAvatarCalledBeforeSingleFetch; + + ensCache.clearCache(); + const timesGetAvatarCalledBeforeDoubleFetch = timesGetAvatarCalled; + const [ashoatAvatarResult2, ashoatAvatarResult3] = await Promise.all([ + ensCache.getAvatarURIForAddress(ashoatAddr), + ensCache.getAvatarURIForAddress(ashoatAddr), + ]); + expect(ashoatAvatarResult2).toBe(ashoatAvatar); + expect(ashoatAvatarResult3).toBe(ashoatAvatar); + const timesGetAvatarCalledForDoubleFetch = + timesGetAvatarCalled - timesGetAvatarCalledBeforeDoubleFetch; + + expect(timesGetAvatarCalledForDoubleFetch).toBe( + timesGetAvatarCalledForSingleFetch, + ); + }); });