diff --git a/package.json b/package.json index 4252727..ed404cb 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "test": "vitest", "test:everything": "npm run lint && npm run test:coverage", "test:coverage": "vitest --coverage", - "lint": "biome check", + "lint": "biome check && tsc --noEmit", "lint:fix": "biome check --write", "prepublishOnly": "npm run build:release", "update:dependencies": "npx npm-check-updates -u" diff --git a/test/Loader-main.spec.ts b/test/Loader-main.spec.ts index 96a5c34..d0be8c7 100644 --- a/test/Loader-main.spec.ts +++ b/test/Loader-main.spec.ts @@ -1,7 +1,7 @@ import { setTimeout } from 'node:timers/promises' import { HitStatisticsRecord } from 'toad-cache' import { afterEach, beforeEach, describe, expect, it, vitest } from 'vitest' -import type { CacheKeyResolver } from '../lib/AbstractCache' +import { type CacheKeyResolver, DEFAULT_FROM_ID_RESOLVER } from '../lib/AbstractCache' import type { LoaderConfig } from '../lib/Loader' import { Loader } from '../lib/Loader' import type { InMemoryCacheConfiguration } from '../lib/memory/InMemoryCache' @@ -571,6 +571,51 @@ describe('Loader Main', () => { expect(valuePost2).toBe('prevaluepost') }) + it('resolves id from loadParams to the loader', async () => { + const cache2 = new DummyCache(undefined) + const operation = new Loader({ + inMemoryCache: IN_MEMORY_CACHE_CONFIG, + asyncCache: cache2, + dataSources: [new DummyDataSourceWithParams('value')], + cacheKeyFromLoadParamsResolver: DEFAULT_FROM_ID_RESOLVER, + }) + // @ts-ignore + const cache1 = operation.inMemoryCache + + const valuePre = await cache1.get('key') + await operation.get({ prefix: 'pre', key: 'dummy', id: 'key', suffix: 'post' }) + const valuePost = await cache1.get('key') + const valuePost2 = await cache2.get('key') + + expect(valuePre).toBeUndefined() + expect(valuePost).toBe('prevaluepost') + expect(valuePost2).toBe('prevaluepost') + }) + + it('throws an error if default resolver is used for composite loadparams', async () => { + const cache2 = new DummyCache(undefined) + const operation = new Loader({ + inMemoryCache: IN_MEMORY_CACHE_CONFIG, + asyncCache: cache2, + dataSources: [new DummyDataSourceWithParams('value')], + }) + expect(() => operation.get({ prefix: 'pre', key: 'key', suffix: 'post' })).toThrowError( + /Please define cacheKeyFromLoadParamsResolver/, + ) + }) + + it('throws an error if default resolver is used for bulk api', async () => { + const cache2 = new DummyCache(undefined) + const operation = new Loader({ + inMemoryCache: IN_MEMORY_CACHE_CONFIG, + asyncCache: cache2, + dataSources: [new DummyDataSourceWithParams('value')], + }) + await expect(() => operation.getMany(['test'], {})).rejects.toThrowError( + /Please define cacheKeyFromValueResolver/, + ) + }) + it('correctly reuses value from cache', async () => { const cache2 = new DummyCache(undefined) const loader1 = new CountingDataSource(undefined) diff --git a/test/fakes/DummyDataSourceWithParams.ts b/test/fakes/DummyDataSourceWithParams.ts index d99c613..cd8c245 100644 --- a/test/fakes/DummyDataSourceWithParams.ts +++ b/test/fakes/DummyDataSourceWithParams.ts @@ -3,6 +3,7 @@ import type { DataSource } from '../../lib/types/DataSources' export type DummyLoaderParams = { prefix: string + id?: string key: string suffix: string }