Skip to content

Commit

Permalink
Improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
kibertoad committed Jan 2, 2025
1 parent e5f1f1d commit 7c69a4c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
47 changes: 46 additions & 1 deletion test/Loader-main.spec.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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<string, DummyLoaderParams>({
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<string, DummyLoaderParams>({
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<string, DummyLoaderParams>({
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)
Expand Down
1 change: 1 addition & 0 deletions test/fakes/DummyDataSourceWithParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { DataSource } from '../../lib/types/DataSources'

export type DummyLoaderParams = {
prefix: string
id?: string
key: string
suffix: string
}
Expand Down

0 comments on commit 7c69a4c

Please sign in to comment.