Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ljankoschek committed Jun 28, 2024
1 parent b49e5d6 commit 6cb8579
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import 'jest-extended';
import '__tests__/utils/matchers';

import { mockAddress, mockKeyPair, mockLaoId, mockLaoServerAddress } from '__tests__/utils';
import {
mockAddress,
mockKeyPair,
mockLaoId,
mockLaoId2,
mockLaoServerAddress,
} from '__tests__/utils';
import { ActionType, Message, ObjectType, ProcessableMessage } from 'core/network/jsonrpc/messages';
import {
Base64UrlData,
Expand All @@ -14,12 +20,14 @@ import {
import { dispatch } from 'core/redux';
import { Challenge } from 'features/linked-organizations/objects/Challenge';
import { setChallenge } from 'features/linked-organizations/reducer';
import { mockRollCall } from 'features/rollCall/__tests__/utils';

import {
handleChallengeMessage,
handleChallengeRequestMessage,
handleFederationExpectMessage,
handleFederationInitMessage,
handleTokensExchangeMessage,
} from '../LinkedOrgHandler';
import {
ChallengeRequest,
Expand All @@ -28,6 +36,7 @@ import {
FederationInit,
FederationResult,
} from '../messages';
import { TokensExchange } from '../messages/TokensExchange';

jest.mock('core/network/jsonrpc/messages/Message', () => {
return {
Expand Down Expand Up @@ -87,6 +96,13 @@ const mockMessageData = {
witness_signatures: [],
};

const mockTokensExchange = new TokensExchange({
lao_id: mockLaoId2,
roll_call_id: mockRollCall.id,
tokens: mockRollCall.attendees,
timestamp: TIMESTAMP,
});

const getCurrentLaoId = () => mockLaoId;

jest.mock('core/redux', () => {
Expand Down Expand Up @@ -380,3 +396,53 @@ describe('handleFederationResultMessage', () => {
).toBeFalse();
});
});

describe('handleTokensExchangeMessage', () => {
it('should return false if the object type is wrong', () => {
expect(
handleTokensExchangeMessage(getCurrentLaoId)({
...mockMessageData,
messageData: {
object: ObjectType.MEETING,
action: ActionType.TOKENS_EXCHANGE,
},
} as ProcessableMessage),
).toBeFalse();
});

it('should return false if the action type is wrong', () => {
expect(
handleTokensExchangeMessage(getCurrentLaoId)({
...mockMessageData,
messageData: {
object: ObjectType.FEDERATION,
action: ActionType.ADD,
},
} as ProcessableMessage),
).toBeFalse();
});

it('should return false if there is an issue with the message data', () => {
expect(
handleTokensExchangeMessage(getCurrentLaoId)({
...mockMessageData,
messageData: {
object: ObjectType.FEDERATION,
action: ActionType.TOKENS_EXCHANGE,
lao_id: undefined as unknown as Hash,
roll_call_id: undefined as unknown as Hash,
tokens: undefined as unknown as PublicKey[],
timestamp: undefined as unknown as Timestamp,
} as TokensExchange,
}),
).toBeFalse();
});
it('should return false if there is both a public key and a reason in the message data', () => {
expect(
handleTokensExchangeMessage(getCurrentLaoId)({
...mockMessageData,
messageData: mockTokensExchange,
}),
).toBeTrue();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import { publish as mockPublish } from 'core/network/JsonRpcApi';
import { Hash, Timestamp } from 'core/objects';
import { OpenedLaoStore } from 'features/lao/store';
import { Challenge, ChallengeState } from 'features/linked-organizations/objects/Challenge';
import { mockRollCall } from 'features/rollCall/__tests__/utils';

import * as msApi from '../LinkedOrgMessageApi';
import { ChallengeRequest } from '../messages';
import { FederationExpect } from '../messages/FederationExpect';
import { FederationInit } from '../messages/FederationInit';
import { TokensExchange } from '../messages/TokensExchange';

jest.mock('core/network/JsonRpcApi', () => {
return {
Expand Down Expand Up @@ -67,6 +69,18 @@ const checkDataFederationExpect = (obj: MessageData) => {
expect(data.challenge).toBeInstanceOf(Message);
};

const checkDataTokensExchange = (obj: MessageData) => {
expect(obj.object).toBe(ObjectType.FEDERATION);
expect(obj.action).toBe(ActionType.TOKENS_EXCHANGE);

const data: TokensExchange = obj as TokensExchange;
expect(data).toBeObject();
expect(data.lao_id).toBeBase64Url();
expect(data.roll_call_id).toBeBase64Url();
expect(data.tokens).toBeArray();
expect(data.timestamp).toBeNumberObject();
};

beforeAll(configureTestFeatures);

beforeEach(() => {
Expand Down Expand Up @@ -123,4 +137,24 @@ describe('LinkedOrgMessageApi', () => {
expect(channel).toBe(`/root/${mockLaoId2.valueOf()}/federation`);
checkDataFederationExpect(msgData);
});

it('should create the correct request for tokenExchange', async () => {
const mockTokensExchange = new TokensExchange({
lao_id: mockLaoId2,
roll_call_id: mockRollCall.id,
tokens: mockRollCall.attendees,
timestamp: VALID_TIMESTAMP,
});
await msApi.tokensExchange(
mockLaoId,
mockTokensExchange.lao_id,
mockTokensExchange.roll_call_id,
mockTokensExchange.tokens,
);

expect(publishMock).toBeCalledTimes(1);
const [channel, msgData] = publishMock.mock.calls[0];
expect(channel).toBe(`/root/${mockLaoId.valueOf()}/federation`);
checkDataTokensExchange(msgData);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ const linkedOrganizationSlice = createSlice({
};
}

if (!state.byLaoId[laoId].allLaoIds.includes(linkedLaoId.valueOf())) {
if (
!state.byLaoId[laoId].allLaoIds.includes(linkedLaoId.valueOf()) &&
linkedLaoId.valueOf() !== laoId.valueOf()
) {
state.byLaoId[laoId].allLaoIds.push(linkedLaoId);
}
},
Expand Down

0 comments on commit 6cb8579

Please sign in to comment.