Skip to content

Commit

Permalink
Restore all mocks in NnsProposalDetail.spec.ts (#5769)
Browse files Browse the repository at this point in the history
# Motivation

To avoid unpredictable behavior, each test should start with a clean
slate.
One component of that is restoring all mocks before each test.
To be able to do this, mock behavior needs to be defined in `beforeEach`
rather than the top-level scope.

This PR does that for `NnsProposalDetail.spec.ts`.

# Changes

1. Mock `proposalsApi.queryProposal` in `beforeEach`.
2. Remove mocking of `markdownToHTML` as it seems to be unnecessary.
3. Add `vi.restoreAllMocks();` in `beforeEach`.

# Tests

Still passes.

# Todos

- [ ] Add entry to changelog (if necessary).
not necessary
  • Loading branch information
dskloetd authored Nov 13, 2024
1 parent 9fe7ff8 commit 165b0b4
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions frontend/src/tests/workflows/NnsProposalDetail.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { resetNeuronsApiService } from "$lib/api-services/governance.api-service";
import * as governanceApi from "$lib/api/governance.api";
import * as proposalsApi from "$lib/api/proposals.api";
import { queryProposal } from "$lib/api/proposals.api";
import { OWN_CANISTER_ID_TEXT } from "$lib/constants/canister-ids.constants";
import NnsProposalDetail from "$lib/pages/NnsProposalDetail.svelte";
Expand Down Expand Up @@ -40,28 +41,20 @@ const proposal = {
],
};

vi.mock("$lib/api/proposals.api", () => {
return {
queryProposal: vi.fn().mockImplementation(() => Promise.resolve(proposal)),
};
});

vi.mock("$lib/utils/html.utils", () => ({
markdownToHTML: (value) => Promise.resolve(value),
}));

let resolveCertifiedPromise;
let resolveUncertifiedPromise;

describe("Proposal detail page when not logged in user", () => {
beforeEach(() => {
vi.clearAllMocks();
vi.restoreAllMocks();
neuronsStore.reset();
resetNeuronsApiService();
resolveCertifiedPromise = undefined;
resolveUncertifiedPromise = undefined;
// we don't display actionable proposals for non-logged in users
actionableProposalsSegmentStore.set("all");

vi.spyOn(proposalsApi, "queryProposal").mockResolvedValue(proposal);
});

describe("when logged in user", () => {
Expand Down

0 comments on commit 165b0b4

Please sign in to comment.