|
| 1 | +import { execute } from "./ApplicationGotoViewUseCase"; |
| 2 | +import { describe, expect, it, vi, beforeEach } from "vitest"; |
| 3 | +import { MovieClip } from "@next2d/display"; |
| 4 | +import { Context } from "../../Context"; |
| 5 | +import { $setConfig } from "../../variable/Config"; |
| 6 | +import { $setContext } from "../../variable/Context"; |
| 7 | +import { response } from "../../../infrastructure/Response/variable/Response"; |
| 8 | + |
| 9 | +vi.mock("../../../domain/screen/Capture/service/AddScreenCaptureService", () => ({ |
| 10 | + execute: vi.fn().mockResolvedValue(undefined) |
| 11 | +})); |
| 12 | + |
| 13 | +vi.mock("../../../domain/screen/Capture/service/DisposeCaptureService", () => ({ |
| 14 | + execute: vi.fn() |
| 15 | +})); |
| 16 | + |
| 17 | +vi.mock("../../../domain/loading/Loading/service/LoadingStartService", () => ({ |
| 18 | + execute: vi.fn().mockResolvedValue(undefined) |
| 19 | +})); |
| 20 | + |
| 21 | +vi.mock("../../../domain/loading/Loading/service/LoadingEndService", () => ({ |
| 22 | + execute: vi.fn().mockResolvedValue(undefined) |
| 23 | +})); |
| 24 | + |
| 25 | +vi.mock("../../../infrastructure/Response/usecase/ResponseRemoveVariableUseCase", () => ({ |
| 26 | + execute: vi.fn() |
| 27 | +})); |
| 28 | + |
| 29 | +vi.mock("../service/ApplicationQueryStringParserService", () => ({ |
| 30 | + execute: vi.fn() |
| 31 | +})); |
| 32 | + |
| 33 | +vi.mock("../../../infrastructure/Request/usecase/RequestUseCase", () => ({ |
| 34 | + execute: vi.fn().mockResolvedValue([]) |
| 35 | +})); |
| 36 | + |
| 37 | +vi.mock("../../../domain/callback/service/CallbackService", () => ({ |
| 38 | + execute: vi.fn().mockResolvedValue(undefined) |
| 39 | +})); |
| 40 | + |
| 41 | +describe("ApplicationGotoViewUseCase Test", () => |
| 42 | +{ |
| 43 | + let mockApplication: any; |
| 44 | + let mockContext: Context; |
| 45 | + let root: MovieClip; |
| 46 | + |
| 47 | + beforeEach(() => |
| 48 | + { |
| 49 | + response.clear(); |
| 50 | + |
| 51 | + mockApplication = { |
| 52 | + currentName: "", |
| 53 | + popstate: false |
| 54 | + }; |
| 55 | + |
| 56 | + root = new MovieClip(); |
| 57 | + mockContext = new Context(root); |
| 58 | + mockContext.unbind = vi.fn().mockResolvedValue(undefined); |
| 59 | + mockContext.bind = vi.fn().mockResolvedValue(null); |
| 60 | + |
| 61 | + $setContext(mockContext); |
| 62 | + $setConfig({ |
| 63 | + platform: "web", |
| 64 | + spa: false, |
| 65 | + stage: { |
| 66 | + width: 800, |
| 67 | + height: 600, |
| 68 | + fps: 60 |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + global.history = { |
| 73 | + pushState: vi.fn() |
| 74 | + } as any; |
| 75 | + |
| 76 | + global.location = { |
| 77 | + origin: "http://localhost" |
| 78 | + } as any; |
| 79 | + |
| 80 | + vi.clearAllMocks(); |
| 81 | + }); |
| 82 | + |
| 83 | + it("execute test case1: basic navigation without loading", async () => |
| 84 | + { |
| 85 | + const { execute: applicationQueryStringParserService } = await import("../service/ApplicationQueryStringParserService"); |
| 86 | + const { execute: requestUseCase } = await import("../../../infrastructure/Request/usecase/RequestUseCase"); |
| 87 | + |
| 88 | + vi.mocked(applicationQueryStringParserService).mockReturnValue({ |
| 89 | + name: "home", |
| 90 | + queryString: "" |
| 91 | + }); |
| 92 | + |
| 93 | + vi.mocked(requestUseCase).mockResolvedValue([]); |
| 94 | + |
| 95 | + await execute(mockApplication, "home"); |
| 96 | + |
| 97 | + expect(mockContext.unbind).toHaveBeenCalled(); |
| 98 | + expect(mockApplication.currentName).toBe("home"); |
| 99 | + expect(mockContext.bind).toHaveBeenCalledWith("home"); |
| 100 | + }); |
| 101 | + |
| 102 | + it("execute test case2: navigation with response data", async () => |
| 103 | + { |
| 104 | + const { execute: applicationQueryStringParserService } = await import("../service/ApplicationQueryStringParserService"); |
| 105 | + const { execute: requestUseCase } = await import("../../../infrastructure/Request/usecase/RequestUseCase"); |
| 106 | + |
| 107 | + vi.mocked(applicationQueryStringParserService).mockReturnValue({ |
| 108 | + name: "dashboard", |
| 109 | + queryString: "" |
| 110 | + }); |
| 111 | + |
| 112 | + const mockResponses = [ |
| 113 | + { name: "user", response: { id: 1, name: "Test User" } }, |
| 114 | + { name: "settings", response: { theme: "dark" } } |
| 115 | + ]; |
| 116 | + |
| 117 | + vi.mocked(requestUseCase).mockResolvedValue(mockResponses); |
| 118 | + |
| 119 | + await execute(mockApplication, "dashboard"); |
| 120 | + |
| 121 | + expect(response.get("user")).toEqual({ id: 1, name: "Test User" }); |
| 122 | + expect(response.get("settings")).toEqual({ theme: "dark" }); |
| 123 | + expect(mockApplication.currentName).toBe("dashboard"); |
| 124 | + }); |
| 125 | + |
| 126 | + it("execute test case3: handle response without name", async () => |
| 127 | + { |
| 128 | + const { execute: applicationQueryStringParserService } = await import("../service/ApplicationQueryStringParserService"); |
| 129 | + const { execute: requestUseCase } = await import("../../../infrastructure/Request/usecase/RequestUseCase"); |
| 130 | + |
| 131 | + vi.mocked(applicationQueryStringParserService).mockReturnValue({ |
| 132 | + name: "test", |
| 133 | + queryString: "" |
| 134 | + }); |
| 135 | + |
| 136 | + const mockResponses = [ |
| 137 | + { name: "", response: { data: "should be skipped" } }, |
| 138 | + { name: "valid", response: { data: "should be set" } } |
| 139 | + ]; |
| 140 | + |
| 141 | + vi.mocked(requestUseCase).mockResolvedValue(mockResponses); |
| 142 | + |
| 143 | + await execute(mockApplication, "test"); |
| 144 | + |
| 145 | + expect(response.has("")).toBe(false); |
| 146 | + expect(response.get("valid")).toEqual({ data: "should be set" }); |
| 147 | + }); |
| 148 | +}); |
0 commit comments