Skip to content

Commit

Permalink
Fixed bug when using metadata template and deep copying it
Browse files Browse the repository at this point in the history
  • Loading branch information
marcvelmer committed Oct 4, 2024
1 parent 1861c5d commit 51d94d8
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/types/election/approval.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { MultiLanguage } from '../../util/lang';
import { IElectionParameters, IVoteType } from './election';
import { UnpublishedElection } from './unpublished';
import { ElectionMetadata, ElectionMetadataTemplate, ElectionResultsTypeNames } from '../metadata';
import { ElectionMetadata, ElectionResultsTypeNames, getElectionMetadataTemplate } from '../metadata';
import { Vote } from '../vote';

export interface IApprovalElectionParameters extends IElectionParameters {}
Expand Down Expand Up @@ -63,7 +63,7 @@ export class ApprovalElection extends UnpublishedElection {
}

public generateMetadata(): ElectionMetadata {
const metadata = ElectionMetadataTemplate;
const metadata = getElectionMetadataTemplate();

metadata.type = {
name: ElectionResultsTypeNames.APPROVAL,
Expand Down
9 changes: 7 additions & 2 deletions src/types/election/budget.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { MultiLanguage } from '../../util/lang';
import { IElectionParameters, IVoteType } from './election';
import { UnpublishedElection } from './unpublished';
import { ElectionMetadata, ElectionMetadataTemplate, ElectionResultsType, ElectionResultsTypeNames } from '../metadata';
import {
ElectionMetadata,
ElectionResultsType,
ElectionResultsTypeNames,
getElectionMetadataTemplate,
} from '../metadata';
import { Vote } from '../vote';

export interface IBudgetElectionParametersInfo extends IElectionParameters {
Expand Down Expand Up @@ -86,7 +91,7 @@ export class BudgetElection extends UnpublishedElection {
}

public generateMetadata(): ElectionMetadata {
const metadata = ElectionMetadataTemplate;
const metadata = getElectionMetadataTemplate();

metadata.type = {
name: ElectionResultsTypeNames.BUDGET,
Expand Down
4 changes: 2 additions & 2 deletions src/types/election/multichoice.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { MultiLanguage } from '../../util/lang';
import { IElectionParameters, IVoteType } from './election';
import { UnpublishedElection } from './unpublished';
import { ElectionMetadata, ElectionMetadataTemplate, ElectionResultsTypeNames } from '../metadata';
import { ElectionMetadata, ElectionResultsTypeNames, getElectionMetadataTemplate } from '../metadata';
import { Vote } from '../vote';

export interface IMultiChoiceElectionParameters extends IElectionParameters {
Expand Down Expand Up @@ -73,7 +73,7 @@ export class MultiChoiceElection extends UnpublishedElection {
}

public generateMetadata(): ElectionMetadata {
const metadata = ElectionMetadataTemplate;
const metadata = getElectionMetadataTemplate();

metadata.type = {
name: ElectionResultsTypeNames.MULTIPLE_CHOICE,
Expand Down
9 changes: 7 additions & 2 deletions src/types/election/quadratic.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { MultiLanguage } from '../../util/lang';
import { IElectionParameters, IVoteType } from './election';
import { UnpublishedElection } from './unpublished';
import { ElectionMetadata, ElectionMetadataTemplate, ElectionResultsType, ElectionResultsTypeNames } from '../metadata';
import {
ElectionMetadata,
ElectionResultsType,
ElectionResultsTypeNames,
getElectionMetadataTemplate,
} from '../metadata';
import { Vote } from '../vote';

export interface IQuadraticElectionParametersInfo extends IElectionParameters {
Expand Down Expand Up @@ -91,7 +96,7 @@ export class QuadraticElection extends UnpublishedElection {
}

public generateMetadata(): ElectionMetadata {
const metadata = ElectionMetadataTemplate;
const metadata = getElectionMetadataTemplate();

metadata.type = {
name: ElectionResultsTypeNames.QUADRATIC,
Expand Down
4 changes: 2 additions & 2 deletions src/types/election/unpublished.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { MultiLanguage } from '../../util/lang';
import {
checkValidElectionMetadata,
ElectionMetadata,
ElectionMetadataTemplate,
getElectionMetadataTemplate,
IChoice,
IQuestion,
} from '../metadata';
Expand Down Expand Up @@ -91,7 +91,7 @@ export class UnpublishedElection extends Election {

public generateMetadata(metadata?: ElectionMetadata): ElectionMetadata {
if (!metadata) {
metadata = ElectionMetadataTemplate;
metadata = getElectionMetadataTemplate();
}

metadata.title = this.title;
Expand Down
4 changes: 4 additions & 0 deletions src/types/metadata/election.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,7 @@ export const ElectionMetadataTemplate: ElectionMetadata = {
properties: {},
},
};

export const getElectionMetadataTemplate = () => {
return structuredClone(ElectionMetadataTemplate);
};
4 changes: 2 additions & 2 deletions test/integration/other.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ElectionMetadataTemplate, VocdoniSDKClient } from '../../src';
import { ElectionMetadataTemplate, getElectionMetadataTemplate, VocdoniSDKClient } from '../../src';
// @ts-ignore
import { clientParams } from './util/client.params';

Expand All @@ -11,7 +11,7 @@ describe('Other tests', () => {
});
it('should calculate the correct IPFS hash with special characters', async () => {
const client = new VocdoniSDKClient(clientParams());
const election = ElectionMetadataTemplate;
const election = getElectionMetadataTemplate();
election.title = {
default: 'Això és un títol',
};
Expand Down
4 changes: 2 additions & 2 deletions test/unit/types/metadata/election.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ElectionMetadata, checkValidElectionMetadata, ElectionMetadataTemplate } from '../../../../src';
import { ElectionMetadata, checkValidElectionMetadata, getElectionMetadataTemplate } from '../../../../src';

let electionMetadata: ElectionMetadata;

beforeEach(() => {
electionMetadata = ElectionMetadataTemplate;
electionMetadata = getElectionMetadataTemplate();
});

describe('Election metadata', () => {
Expand Down

0 comments on commit 51d94d8

Please sign in to comment.