Skip to content

Commit

Permalink
✅ estimate component tests
Browse files Browse the repository at this point in the history
  • Loading branch information
haliphax committed Jan 8, 2025
1 parent d77113a commit 4275ed4
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 2 deletions.
110 changes: 110 additions & 0 deletions src/front-end/app/views/story/estimate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import store from "@/front-end/app/store";
import scales from "@/scales";
import { shallowMount, VueWrapper } from "@vue/test-utils";
import { afterEach, beforeEach, describe, it, vi } from "vitest";
import Estimate from "./estimate.vue";
import PieChart from "./piechart.vue";
import { StoryStoreState } from "./types";

vi.mock("uuid", () => ({ v4: () => "test" }));

describe("Estimate component", () => {
const mockVote = {
participantId: "test",
participantName: "User",
storyId: "test",
vote: "1",
};
const mockStory = {
id: "test",
scale: "Fibonacci",
votes: [mockVote],
};
let estimate: VueWrapper;

beforeEach(() => {
store.registerModule("story", {
actions: { "story.vote"() {} },
mutations: {
story(state, payload) {
(state as StoryStoreState).story = payload;
},
},
state() {
return { story: mockStory };
},
});

estimate = shallowMount(Estimate, { global: { plugins: [store] } });
});

afterEach(() => {
estimate.unmount();
store.unregisterModule("story");
vi.clearAllMocks();
});

it("creates buttons for voting options", ({ expect }) => {
const opts = scales.get("Fibonacci");
const buttons = estimate.findAll("button");

for (const button of buttons) {
expect(opts).toContain(button.text().trim());
}
});

it("hides pie chart if story is not revealed", ({ expect }) => {
expect(estimate.findAllComponents(PieChart)).toHaveLength(0);
});

it("shows pie chart if story is revealed", async ({ expect }) => {
store.commit("story", {
...mockStory,
revealed: true,
});
await estimate.vm.$nextTick();

expect(estimate.findAllComponents(PieChart)).toHaveLength(1);
});

it("dispatches vote action on button click", async ({ expect }) => {
let vote = false;
store.subscribeAction((o) => {
if (o.type !== "story.vote") return;
vote = true;
});

estimate.findAll("button")[0].trigger("click");
await estimate.vm.$nextTick();

expect(vote).toBe(true);
});

it("commits proper vote object on button click", async ({ expect }) => {
let payload: {
participantId: string;
participantName: string;
storyId: string;
vote: string;
};
store.subscribeAction((o) => {
if (o.type !== "story.vote") return;
payload = o.payload;
});
store.commit("story", {
...mockStory,
votes: [],
});
await estimate.vm.$nextTick();

estimate.findAll("button")[0].trigger("click");
await estimate.vm.$nextTick();

expect(payload!).toEqual({
participantId: "test",
participantName: "User",
storyId: "test",
vote: "1",
});
});
});
2 changes: 0 additions & 2 deletions src/front-end/app/views/story/estimate.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ const Estimate = defineComponent({
const value = v.vote.toString();
if (!votes.has(value)) votes.set(value, 0);
votes.set(value, (votes.get(value) ?? 0) + 1);
});
Expand Down

0 comments on commit 4275ed4

Please sign in to comment.