Skip to content

Commit

Permalink
Test handling of pollValidate errors
Browse files Browse the repository at this point in the history
  • Loading branch information
anxolin committed Sep 1, 2023
1 parent c680217 commit 51a4ed9
Showing 1 changed file with 37 additions and 16 deletions.
53 changes: 37 additions & 16 deletions src/composable/ConditionalOrder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,23 @@ const ERROR_REASON = 'Not valid, because I say so!'
describe('Constructor', () => {
test('Create TestConditionalOrder', () => {
// bad address
expect(createTestConditionalOrder({ handler: '0xdeadbeef' })).toThrow('Invalid handler: 0xdeadbeef')
expect(() => createTestConditionalOrder({ handler: '0xdeadbeef' })).toThrow('Invalid handler: 0xdeadbeef')
})

test('Fail if bad address', () => {
// bad address
expect(createTestConditionalOrder({ handler: '0xdeadbeef' })).toThrow('Invalid handler: 0xdeadbeef')
expect(() => createTestConditionalOrder({ handler: '0xdeadbeef' })).toThrow('Invalid handler: 0xdeadbeef')
})

describe('Fail if bad salt', () => {
test('Fails if salt is not an hex', () => {
expect(
expect(() =>
createTestConditionalOrder({ handler: '0x910d00a310f7Dc5B29FE73458F47f519be547D3d', salt: 'cowtomoon' })
).toThrow('Invalid salt: cowtomoon')
})

test('Fails if salt is too short (not 32 bytes)', () => {
expect(
expect(() =>
createTestConditionalOrder({ handler: '0x910d00a310f7Dc5B29FE73458F47f519be547D3d', salt: '0xdeadbeef' })
).toThrow('Invalid salt: 0xdeadbeef')
})
Expand Down Expand Up @@ -257,11 +257,21 @@ describe('Poll Single Orders', () => {
})
})

async function testPollValidateResult(result: PollResultErrors | undefined) {
async function testPollValidateResult(result: PollResultErrors | undefined | Error) {
// GIVEN: The pollValidate returns undefined
const order = new MockTestConditionalOrder(DEFAULT_ORDER_PARAMS)
const isSuccess = result == undefined
mockPollValidate.mockReturnValue(Promise.resolve(result))
const isUnhandledError = result instanceof Error

if (isUnhandledError) {
// Pretend pollValidate throws an error
mockPollValidate.mockImplementation(() => {
throw result
})
} else {
// Pretend pollValidate returns a result
mockPollValidate.mockReturnValue(Promise.resolve(result))
}

// GIVEN: Everything else is OK (auth + contract returns an order)
mockSingleOrders.mockReturnValue(true)
Expand All @@ -273,16 +283,23 @@ describe('Poll Single Orders', () => {
// THEN: we expect no CALLs to the
expect(mockGetTradeableOrderWithSignature).toBeCalledTimes(isSuccess ? 1 : 0)

// THEN: We expect a SUCCESS result, which returns the order and the signature
expect(pollResult).toEqual(
isSuccess
? {
result: PollResultCode.SUCCESS,
order: DISCRETE_ORDER,
signature,
}
: result
)
if (isUnhandledError) {
// THEN: We expect an error
expect(pollResult).toEqual({
result: PollResultCode.UNEXPECTED_ERROR,
error: result,
})
} else if (isSuccess) {
// THEN: We expect a SUCCESS result (which returns the order and the signature)
expect(pollResult).toEqual({
result: PollResultCode.SUCCESS,
order: DISCRETE_ORDER,
signature,
})
} else {
// THEN: We expect the result from pollValidate
expect(pollResult).toEqual(result)
}
}

test('[pollValidate::SUCCESS] Return success when pollValidate returns undefined', async () => {
Expand Down Expand Up @@ -318,4 +335,8 @@ describe('Poll Single Orders', () => {
reason: ERROR_REASON,
})
})

test(`[pollValidate::UNEXPECTED_ERROR] Return an unexpected error when pollValidate throws`, async () => {
testPollValidateResult(new Error(`There was an unexpected error while polling`))
})
})

0 comments on commit 51a4ed9

Please sign in to comment.