|
| 1 | +import { ImportMock, MockManager } from 'ts-mock-imports'; |
| 2 | +import { spy, restore } from 'sinon'; |
| 3 | +import * as contractWrappersModule from '@polymathnetwork/contract-wrappers'; |
| 4 | +import * as contextModule from '../../Context'; |
| 5 | +import { Factories } from '../../Context'; |
| 6 | +import * as wrappersModule from '../../PolymathBase'; |
| 7 | +import * as tokenFactoryModule from '../../testUtils/MockedTokenFactoryModule'; |
| 8 | +import { SignDisableControllerAck } from '../SignDisableControllerAck'; |
| 9 | +import { Procedure } from '../Procedure'; |
| 10 | +import { ProcedureType, ErrorCode } from '../../types'; |
| 11 | +import { PolymathError } from '../../PolymathError'; |
| 12 | +import { mockFactories } from '../../testUtils/mockFactories'; |
| 13 | + |
| 14 | +const params = { |
| 15 | + symbol: 'TEST1', |
| 16 | +}; |
| 17 | + |
| 18 | +describe('SignDisableControllerAck', () => { |
| 19 | + let target: SignDisableControllerAck; |
| 20 | + let contextMock: MockManager<contextModule.Context>; |
| 21 | + let wrappersMock: MockManager<wrappersModule.PolymathBase>; |
| 22 | + let tokenFactoryMock: MockManager<tokenFactoryModule.MockedTokenFactoryModule>; |
| 23 | + let securityTokenMock: MockManager<contractWrappersModule.SecurityToken_3_0_0>; |
| 24 | + let factoriesMockedSetup: Factories; |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + // Mock the context, wrappers, tokenFactory and securityToken to test SignDisableControllerAck |
| 28 | + contextMock = ImportMock.mockClass(contextModule, 'Context'); |
| 29 | + wrappersMock = ImportMock.mockClass(wrappersModule, 'PolymathBase'); |
| 30 | + |
| 31 | + tokenFactoryMock = ImportMock.mockClass(tokenFactoryModule, 'MockedTokenFactoryModule'); |
| 32 | + securityTokenMock = ImportMock.mockClass(contractWrappersModule, 'SecurityToken_3_0_0'); |
| 33 | + |
| 34 | + tokenFactoryMock.mock( |
| 35 | + 'getSecurityTokenInstanceFromTicker', |
| 36 | + securityTokenMock.getMockInstance() |
| 37 | + ); |
| 38 | + |
| 39 | + contextMock.set('contractWrappers', wrappersMock.getMockInstance()); |
| 40 | + wrappersMock.set('tokenFactory', tokenFactoryMock.getMockInstance()); |
| 41 | + |
| 42 | + factoriesMockedSetup = mockFactories(); |
| 43 | + contextMock.set('factories', factoriesMockedSetup); |
| 44 | + |
| 45 | + target = new SignDisableControllerAck(params, contextMock.getMockInstance()); |
| 46 | + }); |
| 47 | + |
| 48 | + afterEach(() => { |
| 49 | + restore(); |
| 50 | + }); |
| 51 | + |
| 52 | + describe('Types', () => { |
| 53 | + test('should extend procedure and have SignDisableControllerAck type', async () => { |
| 54 | + expect(target instanceof Procedure).toBe(true); |
| 55 | + expect(target.type).toBe(ProcedureType.SignDisableControllerAck); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe('SignDisableControllerAck', () => { |
| 60 | + test('should throw if there is no valid security token being provided', async () => { |
| 61 | + tokenFactoryMock |
| 62 | + .mock('getSecurityTokenInstanceFromTicker') |
| 63 | + .withArgs(params.symbol) |
| 64 | + .throws(); |
| 65 | + |
| 66 | + await expect(target.prepareTransactions()).rejects.toThrow( |
| 67 | + new PolymathError({ |
| 68 | + code: ErrorCode.ProcedureValidationError, |
| 69 | + message: `There is no Security Token with symbol ${params.symbol}`, |
| 70 | + }) |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + test('should add a signature request to the queue to sign confirmation for disabling the controller functionality', async () => { |
| 75 | + const addSignatureRequestSpy = spy(target, 'addSignatureRequest'); |
| 76 | + securityTokenMock.mock( |
| 77 | + 'signDisableControllerAck', |
| 78 | + Promise.resolve('SignDisableControllerAck') |
| 79 | + ); |
| 80 | + |
| 81 | + // Real call |
| 82 | + await target.prepareTransactions(); |
| 83 | + |
| 84 | + // Verifications |
| 85 | + expect( |
| 86 | + addSignatureRequestSpy |
| 87 | + .getCall(0) |
| 88 | + .calledWith(securityTokenMock.getMockInstance().signDisableControllerAck) |
| 89 | + ).toEqual(true); |
| 90 | + expect(addSignatureRequestSpy.callCount).toEqual(1); |
| 91 | + }); |
| 92 | + }); |
| 93 | +}); |
0 commit comments