@@ -8,6 +8,7 @@ import MockRedisClient from 'ioredis-mock'
88import DefaultExportRedisStore , {
99 RedisStore ,
1010 type RedisReply ,
11+ type SendCommandClusterDetails ,
1112} from '../source/index.js'
1213
1314// The mock redis client to use.
@@ -345,4 +346,65 @@ describe('redis store test', () => {
345346 // With NEW script we expect a fresh window: hits=1 and ttl reset
346347 expect ( result . totalHits ) . toEqual ( 1 )
347348 } )
349+
350+ it ( 'should bind sendCommand to this' , async ( ) => {
351+ // A custom sendCommand that verifies `this` is bound to the RedisStore instance
352+ const customSendCommand = async function (
353+ this : CustomRedisStore ,
354+ ...args : string [ ]
355+ ) {
356+ if ( ! ( this instanceof CustomRedisStore ) ) {
357+ throw new TypeError ( 'this is not bound to RedisStore instance' )
358+ }
359+
360+ // Throw an error on DECR to test disableDecrement provided by the store
361+ if ( args [ 0 ] === 'DECR' && this . disableDecrement ) {
362+ throw new Error ( 'Decrement not supported in this test' )
363+ }
364+
365+ return sendCommand ( ...args )
366+ }
367+
368+ class CustomRedisStore extends RedisStore {
369+ constructor ( ) {
370+ super ( {
371+ sendCommand : customSendCommand ,
372+ } )
373+ this . init ( { windowMs : 60 } as Options )
374+ }
375+
376+ public get disableDecrement ( ) {
377+ return true
378+ }
379+ }
380+ const store = new CustomRedisStore ( )
381+ const key = 'test-store'
382+ const { totalHits } = await store . increment ( key )
383+ await expect ( store . decrement ( key ) ) . rejects . toThrow (
384+ 'Decrement not supported in this test' ,
385+ )
386+ expect ( totalHits ) . toEqual ( 1 )
387+ } )
388+
389+ it ( 'should bind sendCommandCluster to this' , async ( ) => {
390+ // A custom sendCommand that verifies `this` is bound to the RedisStore instance
391+ const customSendCommandCluster = async function (
392+ this : RedisStore ,
393+ commandDetails : SendCommandClusterDetails ,
394+ ) {
395+ if ( ! ( this instanceof RedisStore ) ) {
396+ throw new TypeError ( 'this is not bound to RedisStore instance' )
397+ }
398+
399+ return sendCommand ( ...commandDetails . command )
400+ }
401+
402+ const store = new RedisStore ( {
403+ sendCommandCluster : customSendCommandCluster ,
404+ } )
405+ store . init ( { windowMs : 60 } as Options )
406+ const key = 'test-store'
407+ const { totalHits } = await store . increment ( key )
408+ expect ( totalHits ) . toEqual ( 1 )
409+ } )
348410} )
0 commit comments