From abae504ef80c60b66e3802b90e56e23296baeda6 Mon Sep 17 00:00:00 2001 From: Juraj Piar Date: Tue, 23 Feb 2021 12:53:55 +0100 Subject: [PATCH] feat(tests-unit): adds confirmations context Update src/context/Confirmations/__tests__/actions.test.tsx --- .../Confirmations/__tests__/Context.test.tsx | 24 ++++ .../Confirmations/__tests__/actions.test.tsx | 115 ++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 src/context/Confirmations/__tests__/Context.test.tsx create mode 100644 src/context/Confirmations/__tests__/actions.test.tsx diff --git a/src/context/Confirmations/__tests__/Context.test.tsx b/src/context/Confirmations/__tests__/Context.test.tsx new file mode 100644 index 000000000..c9372612a --- /dev/null +++ b/src/context/Confirmations/__tests__/Context.test.tsx @@ -0,0 +1,24 @@ +import React from 'react' +import { render } from '@testing-library/react' +import { useProviderTest } from '__tests__/testUtils' +import { Context, Provider } from '../Context' + +const ProviderTest = useProviderTest( + Provider, Context, +) +describe('Confirmations Context', () => { + describe('initialState', () => { + test('should contain contextID: "confirmations"', () => { + render( { + expect(contextID).toEqual('confirmations') + }} + />) + }) + test('should contain an empty confirmations object', () => { + render( { + expect(confirmations).toEqual({}) + }} + />) + }) + }) +}) diff --git a/src/context/Confirmations/__tests__/actions.test.tsx b/src/context/Confirmations/__tests__/actions.test.tsx new file mode 100644 index 000000000..9e7b29132 --- /dev/null +++ b/src/context/Confirmations/__tests__/actions.test.tsx @@ -0,0 +1,115 @@ +import { initialState } from '../Context' +import { + ConfirmationsRecord, ContractAction, NewConfirmationPayload, NewRequestPayload, State, +} from '../interfaces' +import actions from '../actions' + +const MOCK_THASH = 'MOCK_HASH' +let state: State + +describe('Confirmations Context Actions', () => { + beforeEach(() => { + state = { ...initialState } + }) + describe('NEW_CONFIRMATION', () => { + test('should return state with added confirmations when payload.confirmations < payload.targetConfirmation', () => { + const expectedConfirmation = { + currentCount: 2, + targetCount: 20, + contractAction: 'STAKING_STAKE' as ContractAction, + } + + const payload: NewConfirmationPayload = { + confirmations: expectedConfirmation.currentCount, + event: 'MOCK_EVENT', + targetConfirmation: expectedConfirmation.targetCount, + transactionHash: MOCK_THASH, + } + + state.confirmations = { + [MOCK_THASH]: { + ...expectedConfirmation, + currentCount: 1, + }, + } + + const { + confirmations, + }: Partial = actions.NEW_CONFIRMATION( + state, payload, + ) + + expect(confirmations[MOCK_THASH]).toBeTruthy() + expect(confirmations[MOCK_THASH]).toEqual(expectedConfirmation) + }) + + test('should return state with no change when transaction hash is not present in state.confirmations', () => { + const payload: NewConfirmationPayload = { + confirmations: 4, + event: 'MOCK_EVENT', + targetConfirmation: 20, + transactionHash: MOCK_THASH, + } + + const { + confirmations, + }: Partial = actions.NEW_CONFIRMATION( + state, payload, + ) + + expect(confirmations).toBeTruthy() + expect(confirmations).toEqual({}) + }) + + test('should return state without given confirmation hash when payload.confirmations >= payload.targetConfirmation', () => { + const initialConfirmations = { + [MOCK_THASH]: { + currentCount: 1, + targetCount: 2, + contractAction: 'STAKING_STAKE' as ContractAction, + }, + } + + const payload: NewConfirmationPayload = { + confirmations: 2, + event: 'MOCK_EVENT', + targetConfirmation: 2, + transactionHash: MOCK_THASH, + } + + state.confirmations = initialConfirmations + expect(state.confirmations).toEqual(initialConfirmations) + const { + confirmations, + }: Partial = actions.NEW_CONFIRMATION( + state, payload, + ) + + expect(confirmations).toBeTruthy() + expect(confirmations.MOCK_EVENT).toEqual(undefined) + }) + }) + + describe('NEW_REQUEST', () => { + test('should return state with added transaction', () => { + const payload: NewRequestPayload = { + txHash: MOCK_THASH, + contractAction: 'AGREEMENT_NEW', + } + + const expectedConfirmations: ConfirmationsRecord = { + [MOCK_THASH]: { + currentCount: 0, + contractAction: 'AGREEMENT_NEW', + }, + } + + const { + confirmations, + }: Partial = actions.NEW_REQUEST(state, payload) + + expect(confirmations).toBeTruthy() + expect(confirmations).toEqual(expectedConfirmations) + }) + }) +})