/ src / utils / menuLoop.test.js
menuLoop.test.js
 1  import { describe, beforeEach, it, expect, vi } from "vitest";
 2  import { MenuLoop } from "./menuLoop.js";
 3  
 4  describe("MenuLoop", () => {
 5    let menuLoop;
 6    const mockPrompt = vi.fn();
 7  
 8    beforeEach(() => {
 9      vi.resetAllMocks();
10      menuLoop = new MenuLoop();
11      menuLoop.initialize(mockPrompt);
12    });
13  
14    it("can show menu once", async () => {
15      await menuLoop.showOnce();
16      expect(mockPrompt).toHaveBeenCalledTimes(1);
17    });
18  
19    it("can stop the menu loop", async () => {
20      mockPrompt.mockImplementation(() => {
21        menuLoop.stopLoop();
22      });
23      await menuLoop.showLoop();
24  
25      expect(mockPrompt).toHaveBeenCalledTimes(1);
26      expect(menuLoop.running).toBe(false);
27    });
28  
29    it("can run menu in a loop", async () => {
30      let calls = 0;
31      mockPrompt.mockImplementation(() => {
32        calls++;
33        if (calls >= 3) {
34          menuLoop.stopLoop();
35        }
36      });
37  
38      await menuLoop.showLoop();
39  
40      expect(mockPrompt).toHaveBeenCalledTimes(3);
41    });
42  });