import { JwtTokenHandler } from '@/infra/gateways' ; import jwt from 'jsonwebtoken' ; jest . mock ( 'jsonwebtoken' ) ; describe ( 'JwtTokenHandler' , ( ) => { let sut ; let fakeJwt ; let secret ; beforeAll ( ( ) => { secret = 'any_secret' ; fakeJwt = jwt as jest . Mocked < typeof jwt > ; } ) ; beforeEach ( ( ) => { sut = new JwtTokenHandler ( secret ) ; } ) ; describe ( 'generate' , ( ) => { let key ; let expirationInMs ; let token ; beforeAll ( ( ) => { key = 'any_key' ; expirationInMs = 1000 ; token = 'any_token' ; fakeJwt . sign . mockImplementation ( ( ) => token ) ; } ) ; it ( 'should call sign with correct input' , async ( ) => { await sut . generate ( { key , expirationInMs } ) ; expect ( fakeJwt . sign ) . toHaveBeenCalledWith ( { key } , secret , { expiresIn : 1 } ) ; expect ( fakeJwt . sign ) . toHaveBeenCalledTimes ( 1 ) ; } ) ; it ( 'should return a token' , async ( ) => { const generatedToken = await sut . generate ( { key , expirationInMs } ) ; expect ( generatedToken ) . toBe ( token ) ; } ) ; it ( 'should rethrow if sign throws' , async ( ) => { fakeJwt . sign . mockImplementationOnce ( ( ) => { throw new Error ( 'token_error' ) ; } ) ; const promise = sut . generate ( { key , expirationInMs } ) ; await expect ( promise ) . rejects . toThrow ( new Error ( 'token_error' ) ) ; } ) ; } ) ; describe ( 'validate' , ( ) => { let token ; let key ; beforeAll ( ( ) => { token = 'any_token' ; key = 'any_key' ; fakeJwt . verify . mockImplementation ( ( ) => ( { key } ) ) ; } ) ; it ( 'should call sign with correct input' , async ( ) => { await sut . validate ( { token } ) ; expect ( fakeJwt . verify ) . toHaveBeenCalledWith ( token , secret ) ; expect ( fakeJwt . verify ) . toHaveBeenCalledTimes ( 1 ) ; } ) ; it ( 'should return the key used to sign' , async ( ) => { const generatedKey = await sut . validate ( { token } ) ; expect ( generatedKey ) . toBe ( key ) ; } ) ; it ( 'should rethrow if verify throws' , async ( ) => { fakeJwt . verify . mockImplementationOnce ( ( ) => { throw new Error ( 'key_error' ) ; } ) ; const promise = sut . validate ( { token } ) ; await expect ( promise ) . rejects . toThrow ( new Error ( 'key_error' ) ) ; } ) ; it ( 'should throw if verify returns null' , async ( ) => { fakeJwt . verify . mockImplementationOnce ( ( ) => null ) ; const promise = sut . validate ( { token } ) ; await expect ( promise ) . rejects . toThrow ( ) ; } ) ; } ) ; } ) ;