Skip to content

Commit

Permalink
Fixed unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtisassad committed Nov 15, 2024
1 parent 96e654b commit 3709179
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 41 deletions.
14 changes: 9 additions & 5 deletions packages/commonwealth/client/scripts/helpers/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { TopicWeightedVoting } from '@hicommonwealth/schemas';
import BigNumber from 'bignumber.js';
import moment from 'moment';
import React from 'react';
import app from 'state';
import Account from '../models/Account';
import { ThreadStage } from '../models/types';
import type { IApp } from '../state/index';

export async function sleep(msec) {
return new Promise((resolve) => setTimeout(resolve, msec));
Expand All @@ -26,7 +26,11 @@ export function threadStageToLabel(stage: string) {
}
}

export function isDefaultStage(stage: string, customStages?: string[]) {
export function isDefaultStage(
app: IApp,
stage: string,
customStages?: string[],
) {
return (
stage === ThreadStage.Discussion ||
stage ===
Expand Down Expand Up @@ -174,16 +178,16 @@ export function renderMultilineText(text: string) {
* blocknum helpers
*/

export function blocknumToTime(blocknum: number): moment.Moment {
export function blocknumToTime(app: IApp, blocknum: number): moment.Moment {
const currentBlocknum = app.chain.block.height;
const blocktime = app.chain.block.duration;
const lastBlockTime: moment.Moment = app.chain.block.lastTime.clone();
return lastBlockTime.add((blocknum - currentBlocknum) * blocktime, 'seconds');
}

export function blocknumToDuration(blocknum: number) {
export function blocknumToDuration(app: IApp, blocknum: number) {
return moment
.duration(blocknumToTime(blocknum).diff(moment()))
.duration(blocknumToTime(app, blocknum).diff(moment()))
.asMilliseconds();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@ import type { AnyProposal } from '../../../models/types';
import { ProposalStatus } from '../../../models/types';

import { Countdown } from 'views/components/countdown';
import app from '../../../state/index';

export const getStatusClass = (proposal: AnyProposal, isLoading?: boolean) => {
if (isLoading) return '';
return proposal.isPassing === ProposalStatus.Passing
? 'pass'
: proposal.isPassing === ProposalStatus.Passed
? 'pass'
: proposal.isPassing === ProposalStatus.Failing
? 'fail'
: proposal.isPassing === ProposalStatus.Failed
? 'fail'
: '';
? 'pass'
: proposal.isPassing === ProposalStatus.Failing
? 'fail'
: proposal.isPassing === ProposalStatus.Failed
? 'fail'
: '';
};

export const getStatusText = (proposal: AnyProposal, isLoading?: boolean) => {
Expand All @@ -45,34 +46,37 @@ export const getStatusText = (proposal: AnyProposal, isLoading?: boolean) => {
' left',
]
: proposal.endTime.kind === 'fixed_block'
? [
<Countdown
key={proposal.endTime.kind}
duration={blocknumToDuration(proposal.endTime.blocknum)}
/>,
` left (ends on block ${formatNumberLong(
proposal.endTime.blocknum,
)})`,
]
: proposal.endTime.kind === 'dynamic'
? [
<Countdown
key={proposal.endTime.kind}
duration={blocknumToDuration(proposal.endTime.getBlocknum())}
/>,
` left (ends on block ${formatNumberLong(
proposal.endTime.getBlocknum(),
)})`,
]
: proposal.endTime.kind === 'threshold'
? `needs ${proposal.endTime.threshold} votes`
: proposal.endTime.kind === 'not_started'
? 'not yet started'
: proposal.endTime.kind === 'queued'
? 'in queue'
: proposal.endTime.kind === 'unavailable'
? ''
: '';
? [
<Countdown
key={proposal.endTime.kind}
duration={blocknumToDuration(app, proposal.endTime.blocknum)}
/>,
` left (ends on block ${formatNumberLong(
proposal.endTime.blocknum,
)})`,
]
: proposal.endTime.kind === 'dynamic'
? [
<Countdown
key={proposal.endTime.kind}
duration={blocknumToDuration(
app,
proposal.endTime.getBlocknum(),
)}
/>,
` left (ends on block ${formatNumberLong(
proposal.endTime.getBlocknum(),
)})`,
]
: proposal.endTime.kind === 'threshold'
? `needs ${proposal.endTime.threshold} votes`
: proposal.endTime.kind === 'not_started'
? 'not yet started'
: proposal.endTime.kind === 'queued'
? 'in queue'
: proposal.endTime.kind === 'unavailable'
? ''
: '';

if (proposal.isPassing === ProposalStatus.Passed)
return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { getClasses } from 'views/components/component_kit/helpers';
import { CWTag } from 'views/components/component_kit/new_designs/CWTag';
import useBrowserWindow from '../../../../hooks/useBrowserWindow';
import { ThreadStage } from '../../../../models/types';
import app from '../../../../state/index';
import Permissions from '../../../../utils/Permissions';
import { CommentCard } from '../CommentCard';
import { isHot } from '../helpers';
Expand Down Expand Up @@ -116,7 +117,7 @@ export const ThreadCard = ({
const linkedSnapshots = filterLinks(thread.links, LinkSource.Snapshot);
const linkedProposals = filterLinks(thread.links, LinkSource.Proposal);

const isStageDefault = isDefaultStage(thread.stage, customStages);
const isStageDefault = isDefaultStage(app, thread.stage, customStages);
const isTagsRowVisible =
(thread.stage && !isStageDefault) || linkedProposals?.length > 0;
const stageLabel = threadStageToLabel(thread.stage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ const ViewThreadPage = ({ identifier }: ViewThreadPageProps) => {
const editsToSave = localStorage.getItem(
`${app.activeChainId()}-edit-thread-${thread?.id}-storedText`,
);
const isStageDefault = thread && isDefaultStage(thread.stage);
const isStageDefault = thread && isDefaultStage(app, thread.stage);

const tabsShouldBePresent =
showLinkedProposalOptions ||
Expand Down

0 comments on commit 3709179

Please sign in to comment.