generated from PolymeshAssociation/typescript-boilerplate
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0568c3f
commit 8bbf1be
Showing
6 changed files
with
140 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { Call } from '@polkadot/types/interfaces/runtime'; | ||
|
||
import { ProposalStage } from '~/api/entities/Proposal/types'; | ||
import { Context } from '~/context'; | ||
import { dsMockUtils, entityMockUtils } from '~/testUtils/mocks'; | ||
import { Mocked } from '~/testUtils/types'; | ||
|
||
import { assertProposalUnlocked } from '../utils'; | ||
|
||
jest.mock( | ||
'~/api/entities/Proposal', | ||
require('~/testUtils/mocks/entities').mockProposalModule('~/api/entities/Proposal') | ||
); | ||
|
||
describe('assertProposalUnlocked', () => { | ||
const pipId = 10; | ||
const mockAddress = 'someAddress'; | ||
|
||
let mockContext: Mocked<Context>; | ||
|
||
beforeAll(() => { | ||
dsMockUtils.initMocks({ | ||
contextOptions: { | ||
currentPairAddress: mockAddress, | ||
}, | ||
}); | ||
entityMockUtils.initMocks(); | ||
}); | ||
|
||
beforeEach(() => { | ||
mockContext = dsMockUtils.getContextInstance(); | ||
}); | ||
|
||
afterEach(() => { | ||
entityMockUtils.reset(); | ||
dsMockUtils.reset(); | ||
}); | ||
|
||
afterAll(() => { | ||
entityMockUtils.cleanup(); | ||
dsMockUtils.cleanup(); | ||
}); | ||
|
||
test('should throw an error if the proposal is not in pending state', async () => { | ||
entityMockUtils.configureMocks({ | ||
proposalOptions: { | ||
getDetails: dsMockUtils.createMockPip({ | ||
id: dsMockUtils.createMockU32(), | ||
proposal: ('proposal' as unknown) as Call, | ||
state: dsMockUtils.createMockProposalState('Killed'), | ||
}), | ||
}, | ||
}); | ||
|
||
let error; | ||
try { | ||
await assertProposalUnlocked(pipId, mockContext); | ||
} catch (err) { | ||
error = err; | ||
} | ||
|
||
expect(error.message).toBe('The proposal must be in pending state'); | ||
}); | ||
|
||
test('should throw an error if the proposal is not in the cool off period', async () => { | ||
entityMockUtils.configureMocks({ | ||
proposalOptions: { | ||
getDetails: dsMockUtils.createMockPip({ | ||
id: dsMockUtils.createMockU32(), | ||
proposal: ('proposal' as unknown) as Call, | ||
state: dsMockUtils.createMockProposalState('Pending'), | ||
}), | ||
}, | ||
}); | ||
|
||
let error; | ||
try { | ||
await assertProposalUnlocked(pipId, mockContext); | ||
} catch (err) { | ||
error = err; | ||
} | ||
|
||
expect(error.message).toBe('The proposal can be canceled only during its cool off period'); | ||
}); | ||
|
||
test('should does not throw an error if the proposal is unlocked', async () => { | ||
entityMockUtils.configureMocks({ | ||
proposalOptions: { | ||
getDetails: dsMockUtils.createMockPip({ | ||
id: dsMockUtils.createMockU32(), | ||
proposal: ('proposal' as unknown) as Call, | ||
state: dsMockUtils.createMockProposalState('Pending'), | ||
}), | ||
getStage: ProposalStage.CoolOff, | ||
}, | ||
}); | ||
|
||
const result = await assertProposalUnlocked(pipId, mockContext); | ||
|
||
expect(result).toBeUndefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Proposal } from '~/api/entities'; | ||
import { ProposalStage } from '~/api/entities/Proposal/types'; | ||
import { PolymeshError } from '~/base'; | ||
import { Context } from '~/context'; | ||
import { ErrorCode } from '~/types'; | ||
|
||
/** | ||
* @hidden | ||
*/ | ||
export async function assertProposalUnlocked(pipId: number, context: Context): Promise<void> { | ||
const proposal = new Proposal({ pipId }, context); | ||
|
||
const [details, stage] = await Promise.all([proposal.getDetails(), proposal.getStage()]); | ||
|
||
const { state } = details; | ||
|
||
if (!state.isPending) { | ||
throw new PolymeshError({ | ||
code: ErrorCode.ValidationError, | ||
message: 'The proposal must be in pending state', | ||
}); | ||
} | ||
|
||
if (stage !== ProposalStage.CoolOff) { | ||
throw new PolymeshError({ | ||
code: ErrorCode.ValidationError, | ||
message: 'The proposal can be canceled only during its cool off period', | ||
}); | ||
} | ||
} |