Select Git revision
handlers.spec.ts
-
Nawfel Senoussi authoredNawfel Senoussi authored
handlers.spec.ts 12.09 KiB
import { handleCreateAssociation, handleCreateProposal, handleCreateToken, handleGetBalance, handleJoinAssociation, handleGetAssociations, handleGetAssociationDetails, handleBurnToken } from '../../src/handlers/handlers'
import { type TezosToolkit } from '@taquito/taquito'
import chalk from 'chalk'
import { vi, describe, it, expect, beforeEach } from 'vitest'
const { createAssociationSpy, promptSpy, createFATokenSpy, getBalanceSpy, createProposalSpy, joinAssociationSpy, getAssociationsSpy, getAssociationDetailsSpy, burnTokenSpy } =
vi.hoisted(() => {
return {
createAssociationSpy: vi.fn().mockResolvedValue({ hash: 'association-hash' }),
createFATokenSpy: vi.fn().mockResolvedValue({ hash: 'token-hash' }),
getBalanceSpy: vi.fn().mockResolvedValue({ hash: 'balance-hash' }),
createProposalSpy: vi.fn().mockResolvedValue({ hash: 'proposal-hash' }),
joinAssociationSpy: vi.fn().mockResolvedValue({ hash: 'association-hash' }),
getAssociationsSpy: vi.fn().mockResolvedValue({ hash: 'association-hash' }),
getAssociationDetailsSpy: vi.fn().mockResolvedValue({ hash: 'association-hash' }),
burnTokenSpy: vi.fn().mockResolvedValue({ hash: 'token-hash' }),
promptSpy: vi.fn().mockResolvedValue({})
}
})
vi.mock('inquirer', async (importOriginal) => {
const actual = await importOriginal()
if (typeof actual === 'object' && actual !== null) {
return {
...actual,
default: {
prompt: promptSpy
}
}
} else {
return actual
}
})
vi.mock('../../src/services/association.service', () => ({
createAssociation: createAssociationSpy,
joinAssociation: joinAssociationSpy,
getAssociations: getAssociationsSpy,
getAssociationDetails: getAssociationDetailsSpy
}))
vi.mock('../../src/services/token.service', () => ({
createFAToken: createFATokenSpy,
burnToken: burnTokenSpy
}))
vi.mock('../../src/services/balance.service', () => ({
getBalance: getBalanceSpy
}))
vi.mock('../../src/services/proposal.service', () => ({
createProposal: createProposalSpy
}))
const mockedTezosToolkit = {} as unknown as TezosToolkit
describe('handlers', () => {
beforeEach(() => {
vi.clearAllMocks()
})
describe('handleCreateAssociation', () => {
describe('when createAssociation is called with success', () => {
it('should create an association with provided name and description', async () => {
const name = 'Association Name'
const description = 'Association Description'
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
promptSpy.mockResolvedValueOnce({ name, description })
await handleCreateAssociation(mockedTezosToolkit)
expect(createAssociationSpy).toBeCalledWith(
{ name: 'Association Name', description: 'Association Description' },
mockedTezosToolkit
)
expect(consoleSpy).toBeCalledWith(chalk.bgGreenBright('\nVotre association a été créée !!\n'))
})
})
describe('when createAssociation is called with error', () => {
it('should log error message', async () => {
const error = {
lastError: {
with: {
string: 'Custom Error'
}
}
}
createAssociationSpy.mockRejectedValueOnce(error)
const name = 'Association Name'
const description = 'Association Description'
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
promptSpy.mockResolvedValueOnce({ name, description })
await handleCreateAssociation(mockedTezosToolkit)
expect(createAssociationSpy).toBeCalledWith(
{ name: 'Association Name', description: 'Association Description' },
mockedTezosToolkit
)
expect(consoleSpy).toBeCalledWith(chalk.bgRed('\nCustom Error\n'))
})
})
})
describe.skip('handleCreateToken', () => {
describe('when createFAToken is called with success', () => {
it('should create a token with provided nbTokenFungible', async () => {
const nbTokenFungible = 5
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
promptSpy.mockResolvedValueOnce({ nbTokenFungible })
await handleCreateToken(mockedTezosToolkit)
expect(createFATokenSpy).toBeCalledWith(nbTokenFungible, mockedTezosToolkit)
expect(consoleSpy).toBeCalledWith(chalk.bgGreenBright('\nVotre token a été créé !!\n'))
})
})
describe('when createFAToken is called with error', () => {
it('should log error message', async () => {
const error = {
lastError: {
with: {
string: 'Custom Error'
}
}
}
createAssociationSpy.mockRejectedValueOnce(error)
const nbTokenFungible = 5
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
promptSpy.mockResolvedValueOnce({ nbTokenFungible })
await handleCreateToken(mockedTezosToolkit)
expect(createAssociationSpy).toBeCalledWith(nbTokenFungible, mockedTezosToolkit)
expect(consoleSpy).toBeCalledWith(chalk.red('\nCustom Error\n'))
})
})
})
describe('handleGetBalance', () => {
describe('when getBalance is called with success', () => {
it('should call getBalance', async () => {
await handleGetBalance(mockedTezosToolkit)
expect(getBalanceSpy).toBeCalledWith(mockedTezosToolkit)
})
})
describe('when getBalance is called with error', () => {
it('should log error message', async () => {
const error = {
lastError: {
with: {
string: 'Custom Error'
}
}
}
getBalanceSpy.mockRejectedValueOnce(error)
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
await handleGetBalance(mockedTezosToolkit)
expect(getBalanceSpy).toBeCalled()
expect(consoleSpy).toBeCalledWith(chalk.bgRed('\nCustom Error\n'))
})
})
})
describe('handleCreateProposal', () => {
describe('when createProposal is called with success', () => {
it('should create a proposal with provided title and description', async () => {
const title = 'Proposal Title'
const description = 'Proposal Description'
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
promptSpy.mockResolvedValueOnce({ title, description })
await handleCreateProposal(mockedTezosToolkit)
expect(createProposalSpy).toBeCalledWith({ title: 'Proposal Title', description: 'Proposal Description' }, mockedTezosToolkit)
expect(consoleSpy).toBeCalledWith(chalk.bgGreenBright('\nVous avez soumis une proposition !!\n'))
})
})
describe('when createProposal is called with error', () => {
it('should log error message', async () => {
const error = {
lastError: {
with: {
string: 'Custom Error'
}
}
}
createProposalSpy.mockRejectedValueOnce(error)
const title = 'Proposal Title'
const description = 'Proposal Description'
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
promptSpy.mockResolvedValueOnce({ title, description })
await handleCreateProposal(mockedTezosToolkit)
expect(createProposalSpy).toBeCalled()
expect(consoleSpy).toBeCalledWith(chalk.bgRed('\nCustom Error\n'))
})
})
})
describe('handleJoinAssociation', () => {
describe('when joinAssociation is called with success', () => {
it('should join an association with provided name', async () => {
const associationName = 'Association Name'
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
promptSpy.mockResolvedValueOnce({ associationName })
await handleJoinAssociation(mockedTezosToolkit)
expect(joinAssociationSpy).toBeCalledWith('Association Name', mockedTezosToolkit)
expect(consoleSpy).toBeCalledWith(chalk.bgGreenBright("\nVous avez rejoint l'association !!\n"))
})
})
describe('when joinAssociation is called with error', () => {
it('should log error message', async () => {
const error = {
lastError: {
with: {
string: 'Custom Error'
}
}
}
joinAssociationSpy.mockRejectedValueOnce(error)
const name = 'Association Name'
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
promptSpy.mockResolvedValueOnce({ name })
await handleJoinAssociation(mockedTezosToolkit)
expect(joinAssociationSpy).toBeCalled()
expect(consoleSpy).toBeCalledWith(chalk.bgRed('\nCustom Error\n'))
})
})
})
describe('handleGetAssociations', () => {
describe('when getAssociations is called with success', () => {
it('should call getAssociations', async () => {
await handleGetAssociations(mockedTezosToolkit)
expect(getAssociationsSpy).toBeCalledWith(mockedTezosToolkit)
})
})
describe('when getAssociations is called with error', () => {
it('should log error message', async () => {
const error = {
lastError: {
with: {
string: 'Custom Error'
}
}
}
getAssociationsSpy.mockRejectedValueOnce(error)
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
await handleGetAssociations(mockedTezosToolkit)
expect(getAssociationsSpy).toBeCalled()
expect(consoleSpy).toBeCalledWith(chalk.bgRed('\nCustom Error\n'))
})
})
})
describe('handleGetAssociationDetails', () => {
describe('when getAssociationDetails is called with success', () => {
it('should get association details with provided name', async () => {
const associationName = 'Association Name'
promptSpy.mockResolvedValueOnce({ associationName })
await handleGetAssociationDetails(mockedTezosToolkit)
expect(getAssociationDetailsSpy).toBeCalledWith('Association Name', mockedTezosToolkit)
})
})
describe('when getAssociationDetails is called with error', () => {
it('should log error message', async () => {
const error = {
lastError: {
with: {
string: 'Custom Error'
}
}
}
getAssociationDetailsSpy.mockRejectedValueOnce(error)
const associationName = 'Association Name'
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
promptSpy.mockResolvedValueOnce({ associationName })
await handleGetAssociationDetails(mockedTezosToolkit)
expect(getAssociationDetailsSpy).toBeCalled()
expect(consoleSpy).toBeCalledWith(chalk.bgRed('\nCustom Error\n'))
})
})
})
describe('handleBurnToken', () => {
describe('when burnToken is called with success', () => {
it('should create a token with provided nbTokenToBurn', async () => {
const nbTokenToBurn = 5
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
promptSpy.mockResolvedValueOnce({ nbTokenToBurn })
await handleBurnToken(mockedTezosToolkit)
expect(burnTokenSpy).toBeCalledWith(nbTokenToBurn, mockedTezosToolkit)
expect(consoleSpy).toBeCalledWith(chalk.bgGreenBright('\nVous avez bruler vos tokens !!\n'))
})
})
describe('when burnToken is called with error', () => {
it('should log error message', async () => {
const error = {
lastError: {
with: {
string: 'Custom Error'
}
}
}
burnTokenSpy.mockRejectedValueOnce(error)
const nbTokenToBurn = 5
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
promptSpy.mockResolvedValueOnce({ nbTokenToBurn })
await handleBurnToken(mockedTezosToolkit)
expect(burnTokenSpy).toBeCalledWith(nbTokenToBurn, mockedTezosToolkit)
expect(consoleSpy).toBeCalledWith(chalk.bgRed('\nCustom Error\n'))
})
})
})
})