Skip to content

Commit

Permalink
Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyratox committed Mar 7, 2022
1 parent e68b6e4 commit 6a63674
Show file tree
Hide file tree
Showing 16 changed files with 830 additions and 485 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe('LaoEventBuilder', () => {
start: 12345,
end: 16345,
questions: [question1],
electionStatus: ElectionStatus.FINISHED,
electionStatus: ElectionStatus.TERMINATED,
registeredVotes: [registeredVotes],
};
expect(eventFromState(election)).toBeInstanceOf(Election);
Expand Down
12 changes: 0 additions & 12 deletions fe1-web/src/features/evoting/components/EventElection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,6 @@ const EventElection = (props: IPropTypes) => {
)}
</>
);
case ElectionStatus.FINISHED:
return (
<>
<Text style={styles.text}>Election finished</Text>
{isOrganizer && (
<WideButtonView
title="Terminate Election / Tally Votes"
onPress={onTerminateElection}
/>
)}
</>
);
case ElectionStatus.TERMINATED:
return (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,6 @@ const terminatedElection = new Election({
registeredVotes: [registeredVote],
});

const finishedElection = new Election({
lao: mockLaoIdHash,
id: mockElectionId,
name: 'An election',
version: STRINGS.election_version_identifier,
createdAt: TIMESTAMP,
start: TIMESTAMP,
end: CLOSE_TIMESTAMP,
questions: [question],
electionStatus: ElectionStatus.FINISHED,
registeredVotes: [registeredVote],
});

const resultElection = new Election({
lao: mockLaoIdHash,
id: mockElectionId,
Expand Down Expand Up @@ -160,6 +147,7 @@ beforeAll(() => {
onConfirmEventCreation: () => undefined,
});
});

afterEach(() => {
warn.mockClear();
});
Expand Down Expand Up @@ -205,18 +193,6 @@ describe('EventElection', () => {
});
});

describe('Finished election where the results are not yet available', () => {
it('renders correctly for an organizer', () => {
const component = render(<EventElection election={finishedElection} isOrganizer />).toJSON();
expect(component).toMatchSnapshot();
});

it('renders correctly for an attendee', () => {
const component = render(<EventElection election={finishedElection} />).toJSON();
expect(component).toMatchSnapshot();
});
});

describe('Finished election where the results are available', () => {
it('renders correctly for an organizer', () => {
const component = render(<EventElection election={resultElection} isOrganizer />).toJSON();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1296,147 +1296,6 @@ Array [
]
`;

exports[`EventElection Finished election where the results are not yet available renders correctly for an attendee 1`] = `
Array [
<View>
<Text
style={
Object {
"marginHorizontal": 10,
}
}
>
Start:
<time
dateTime="2020-12-31T23:00:00.000Z"
title="2020-12-31 23:00"
>
1 year ago
</time>
End:
<time
dateTime="2021-01-01T23:00:00.000Z"
title="2021-01-01 23:00"
>
1 year ago
</time>
</Text>
</View>,
<Text
style={
Object {
"fontSize": 25,
"marginHorizontal": 10,
"textAlign": "center",
}
}
>
Election finished
</Text>,
]
`;

exports[`EventElection Finished election where the results are not yet available renders correctly for an organizer 1`] = `
Array [
<View>
<Text
style={
Object {
"marginHorizontal": 10,
}
}
>
Start:
<time
dateTime="2020-12-31T23:00:00.000Z"
title="2020-12-31 23:00"
>
1 year ago
</time>
End:
<time
dateTime="2021-01-01T23:00:00.000Z"
title="2021-01-01 23:00"
>
1 year ago
</time>
</Text>
</View>,
<Text
style={
Object {
"fontSize": 25,
"marginHorizontal": 10,
"textAlign": "center",
}
}
>
Election finished
</Text>,
<View
style={
Object {
"marginHorizontal": 50,
"marginVertical": 10,
}
}
>
<View
accessibilityRole="button"
accessibilityState={
Object {
"disabled": false,
}
}
accessible={true}
collapsable={false}
focusable={true}
nativeID="animatedComponent"
onClick={[Function]}
onResponderGrant={[Function]}
onResponderMove={[Function]}
onResponderRelease={[Function]}
onResponderTerminate={[Function]}
onResponderTerminationRequest={[Function]}
onStartShouldSetResponder={[Function]}
style={
Object {
"opacity": 1,
}
}
>
<View
style={
Array [
Object {},
]
}
>
<Text
disabled={false}
style={
Array [
Object {
"color": "#007AFF",
"fontSize": 18,
"margin": 8,
"textAlign": "center",
},
]
}
>
Terminate Election / Tally Votes
</Text>
</View>
</View>
</View>,
]
`;

exports[`EventElection Not started election renders correctly for an attendee 1`] = `
Array [
<View>
Expand Down
27 changes: 27 additions & 0 deletions fe1-web/src/features/evoting/hooks/EvotingHooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { getEvotingConfig } from '../index';

export namespace EvotingHooks {
/**
* Gets the current lao id
* @returns The current lao id
*/
export const useCurrentLaoId = () => {
return getEvotingConfig().getCurrentLaoId();
};

/**
* Gets the current lao
* @returns The current lao
*/
export const useCurrentLao = () => {
return getEvotingConfig().getCurrentLao();
};

/**
* Gets the onConfirmEventCreation helper function
* @returns The onConfirmEventCreation function
*/
export const useOnConfirmEventCreation = () => {
return getEvotingConfig().onConfirmEventCreation;
};
}
38 changes: 38 additions & 0 deletions fe1-web/src/features/evoting/hooks/__tests__/EvotingHooks.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { describe } from '@jest/globals';
import { configure } from 'features/evoting';
import { mockLao, mockLaoIdHash, mockMessageRegistry, mockReduxAction } from '__tests__/utils';
import { EvotingHooks } from '../index';

const onConfirmEventCreation = jest.fn();

beforeAll(() => {
configure({
getCurrentLao: () => mockLao,
getCurrentLaoId: () => mockLaoIdHash,
addEvent: () => mockReduxAction,
updateEvent: () => mockReduxAction,
getEventFromId: () => undefined,
messageRegistry: mockMessageRegistry,
onConfirmEventCreation,
});
});

describe('E-Voting hooks', () => {
describe('EvotingHooks.useCurrentLao', () => {
it('should return the current lao', () => {
expect(EvotingHooks.useCurrentLao()).toEqual(mockLao);
});
});

describe('EvotingHooks.useCurrentLaoId', () => {
it('should return the current lao id', () => {
expect(EvotingHooks.useCurrentLaoId()).toEqual(mockLaoIdHash);
});
});

describe('EvotingHooks.useOnConfirmEventCreation', () => {
it('should return the onConfirmEventCreation config option', () => {
expect(EvotingHooks.useOnConfirmEventCreation()).toEqual(onConfirmEventCreation);
});
});
});
28 changes: 1 addition & 27 deletions fe1-web/src/features/evoting/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1 @@
import { getEvotingConfig } from '../index';

export namespace EvotingHooks {
/**
* Gets the current lao id
* @returns The current lao id
*/
export const useCurrentLaoId = () => {
return getEvotingConfig().getCurrentLaoId();
};

/**
* Gets the current lao
* @returns The current lao
*/
export const useCurrentLao = () => {
return getEvotingConfig().getCurrentLao();
};

/**
* Gets the onConfirmEventCreation helper function
* @returns The onConfirmEventCreation function
*/
export const useOnConfirmEventCreation = () => {
return getEvotingConfig().onConfirmEventCreation;
};
}
export * from './EvotingHooks';
18 changes: 10 additions & 8 deletions fe1-web/src/features/evoting/network/ElectionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,19 @@ export const handleCastVoteMessage =
}

const castVoteMsg = msg.messageData as CastVote;

const election = getEventFromId(castVoteMsg.election) as Election;
if (!election) {
console.warn(makeErr('No active election to register vote '));
return false;
}

const currentVote: RegisteredVote = {
createdAt: castVoteMsg.created_at.valueOf(),
sender: msg.sender.valueOf(),
votes: castVoteMsg.votes,
messageId: msg.message_id.valueOf(),
};
const election = getEventFromId(castVoteMsg.election) as Election;
if (!election) {
console.warn(makeErr('No active election to register vote '));
return false;
}

if (election.registeredVotes.some((votes) => votes.sender === currentVote.sender)) {
// Update the vote if the person has already voted before
Expand All @@ -144,7 +146,7 @@ export const handleCastVoteMessage =
} else {
election.registeredVotes = [...election.registeredVotes, currentVote];
}
dispatch(updateEvent(lao.id, election.toState()));
dispatch(updateEvent(msg.laoId, election.toState()));
return true;
};

Expand Down Expand Up @@ -210,14 +212,14 @@ export const handleElectionResultMessage =
return false;
}
const electionId = getLastPartOfChannel(msg.channel);
const ElectionResultMsg = msg.messageData as ElectionResult;
const electionResultMessage = msg.messageData as ElectionResult;
const election = getEventFromId(electionId) as Election;
if (!election) {
console.warn(makeErr('No active election for the result'));
return false;
}

election.questionResult = ElectionResultMsg.questions.map((q) => ({
election.questionResult = electionResultMessage.questions.map((q) => ({
id: q.id,
result: q.result.map((r) => ({ ballotOption: r.ballot_option, count: r.count })),
}));
Expand Down
Loading

0 comments on commit 6a63674

Please sign in to comment.