Skip to content
Snippets Groups Projects
Select Git revision
  • 10b940badd5507990d63c77c18a172e578d05bdc
  • main default protected
2 results

roleHandlers.spec.ts

Blame
  • roleHandlers.spec.ts 6.98 KiB
    import { type TezosToolkit } from '@taquito/taquito'
    import { handleAdminFATokenChoice, handleAdminAssociationChoice, handleAdherentChoice, handleConnectedChoice } from '../../src/handlers/roleHandlers'
    import { handleCreateAssociation, handleCreateToken, handleBurnToken, handleGetBalance, handleCreateProposal, handleJoinAssociation, handleGetAssociations } from '../../src/handlers/handlers'
    import { vi, describe, it, expect, afterEach } from 'vitest'
    import chalk from 'chalk'
    
    vi.mock('../../src/handlers/handlers', () => ({
      handleCreateAssociation: vi.fn(),
      handleCreateToken: vi.fn(),
      handleBurnToken: vi.fn(),
      handleGetBalance: vi.fn(),
      handleCreateProposal: vi.fn(),
      handleJoinAssociation: vi.fn(),
      handleGetAssociations: vi.fn()
    }))
    
    const mockedTezosToolkit = {} as unknown as TezosToolkit
    
    describe('roleHandlers', () => {
      afterEach(() => {
        vi.restoreAllMocks()
      })
    
      describe('handleAdminFATokenChoice', () => {
        describe('when choice is "0"', () => {
          it('should not call any handler', async () => {
            await handleAdminFATokenChoice('0', mockedTezosToolkit)
    
            expect(handleCreateAssociation).not.toHaveBeenCalled()
            expect(handleCreateToken).not.toHaveBeenCalled()
            expect(handleBurnToken).not.toHaveBeenCalled()
            expect(handleGetBalance).not.toHaveBeenCalled()
          })
        })
    
        describe('when choice is "1"', () => {
          it('should call handleCreateAssociation', async () => {
            await handleAdminFATokenChoice('1', mockedTezosToolkit)
    
            expect(handleCreateAssociation).toBeCalled()
          })
        })
    
        describe('when choice is "2"', () => {
          it('should call handleCreateToken', async () => {
            await handleAdminFATokenChoice('2', mockedTezosToolkit)
    
            expect(handleCreateToken).toBeCalled()
          })
        })
    
        describe('when choice is "3"', () => {
          it('should call handleBurnToken', async () => {
            await handleAdminFATokenChoice('3', mockedTezosToolkit)
    
            expect(handleBurnToken).toBeCalled()
          })
        })
    
        describe('when choice is "4"', () => {
          it('should call handleGetBalance', async () => {
            await handleAdminFATokenChoice('4', mockedTezosToolkit)
    
            expect(handleGetBalance).toBeCalled()
          })
        })
    
        describe('when choice is invalid', () => {
          it('should log "Choix invalide"', async () => {
            const consoleSpy = vi.spyOn(console, 'log')
            await handleAdminFATokenChoice('invalid', mockedTezosToolkit)
    
            expect(consoleSpy).toHaveBeenCalledWith(chalk.bgRedBright('\nChoix invalide\n'))
          })
        })
      })
    
      describe('handleAdminAssociationChoice', () => {
        describe('when choice is "0"', () => {
          it('should not call any handler', async () => {
            await handleAdminAssociationChoice('0', mockedTezosToolkit)
    
            expect(handleCreateToken).not.toHaveBeenCalled()
            expect(handleBurnToken).not.toHaveBeenCalled()
            expect(handleGetBalance).not.toHaveBeenCalled()
          })
        })
    
        describe('when choice is "1"', () => {
          it('should call handleCreateToken', async () => {
            await handleAdminAssociationChoice('1', mockedTezosToolkit)
    
            expect(handleCreateToken).toBeCalled()
          })
        })
    
        describe('when choice is "2"', () => {
          it('should call handleBurnToken', async () => {
            await handleAdminAssociationChoice('2', mockedTezosToolkit)
    
            expect(handleBurnToken).toBeCalled()
          })
        })
    
        describe('when choice is "3"', () => {
          it('should call handleGetBalance', async () => {
            await handleAdminAssociationChoice('3', mockedTezosToolkit)
    
            expect(handleGetBalance).toBeCalled()
          })
        })
    
        describe('when choice is invalid', () => {
          it('should log "Choix invalide"', async () => {
            const consoleSpy = vi.spyOn(console, 'log')
            await handleAdminAssociationChoice('invalid', mockedTezosToolkit)
    
            expect(consoleSpy).toHaveBeenCalledWith(chalk.bgRedBright('\nChoix invalide\n'))
          })
        })
      })
    
      describe('handleAdherentChoice', () => {
        describe('when choice is "0"', () => {
          it('should not call any handler', async () => {
            await handleAdherentChoice('0', mockedTezosToolkit)
    
            expect(handleCreateProposal).not.toHaveBeenCalled()
            expect(handleCreateToken).not.toHaveBeenCalled()
            expect(handleGetBalance).not.toHaveBeenCalled()
          })
        })
    
        describe('when choice is "1"', () => {
          it('should call handleCreateProposal', async () => {
            await handleAdherentChoice('1', mockedTezosToolkit)
    
            expect(handleCreateProposal).toBeCalled()
          })
        })
    
        describe('when choice is "2"', () => {
          it('should call handleCreateToken', async () => {
            await handleAdherentChoice('2', mockedTezosToolkit)
    
            expect(handleCreateToken).toBeCalled()
          })
        })
    
        describe('when choice is "3"', () => {
          it('should call handleGetBalance', async () => {
            await handleAdherentChoice('3', mockedTezosToolkit)
    
            expect(handleGetBalance).toBeCalled()
          })
        })
    
        describe('when choice is invalid', () => {
          it('should log "Choix invalide"', async () => {
            const consoleSpy = vi.spyOn(console, 'log')
            await handleAdherentChoice('invalid', mockedTezosToolkit)
    
            expect(consoleSpy).toHaveBeenCalledWith(chalk.bgRedBright('\nChoix invalide\n'))
          })
        })
      })
    
      describe('handleConnectedChoice', () => {
        describe('when choice is "0"', () => {
          it('should not call any handler', async () => {
            await handleConnectedChoice('0', mockedTezosToolkit)
    
            expect(handleJoinAssociation).not.toHaveBeenCalled()
            expect(handleCreateToken).not.toHaveBeenCalled()
            expect(handleGetAssociations).not.toHaveBeenCalled()
            expect(handleGetBalance).not.toHaveBeenCalled()
          })
        })
    
        describe('when choice is "1"', () => {
          it('should call handleJoinAssociation', async () => {
            await handleConnectedChoice('1', mockedTezosToolkit)
    
            expect(handleJoinAssociation).toBeCalled()
          })
        })
    
        describe('when choice is "2"', () => {
          it('should call handleCreateToken', async () => {
            await handleConnectedChoice('2', mockedTezosToolkit)
    
            expect(handleCreateToken).toBeCalled()
          })
        })
    
        describe('when choice is "3"', () => {
          it('should call handleGetAssociations', async () => {
            await handleConnectedChoice('3', mockedTezosToolkit)
    
            expect(handleGetAssociations).toBeCalled()
          })
        })
    
        describe('when choice is "4"', () => {
          it('should call handleGetBalance', async () => {
            await handleConnectedChoice('4', mockedTezosToolkit)
    
            expect(handleGetBalance).toBeCalled()
          })
        })
    
        describe('when choice is invalid', () => {
          it('should log "Choix invalide"', async () => {
            const consoleSpy = vi.spyOn(console, 'log')
            await handleConnectedChoice('invalid', mockedTezosToolkit)
    
            expect(consoleSpy).toHaveBeenCalledWith(chalk.bgRedBright('\nChoix invalide\n'))
          })
        })
      })
    })