From f6c95e8ee5e32138b457cee61289dc6d8c4c2add Mon Sep 17 00:00:00 2001 From: Manuel Leduc Date: Tue, 31 Dec 2024 10:04:41 +0100 Subject: [PATCH] CRISTAL-194: Support for images edition Add test for DefaultDocumentService --- core/document/document-default/package.json | 5 +- .../__tests__/defaultDocumentService.test.ts | 80 +++++++++++++++++++ .../document-default/vitest.config.ts | 25 ++++++ pnpm-lock.yaml | 9 +++ 4 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 core/document/document-default/src/__tests__/defaultDocumentService.test.ts create mode 100644 core/document/document-default/vitest.config.ts diff --git a/core/document/document-default/package.json b/core/document/document-default/package.json index 511ccf276..46949a555 100644 --- a/core/document/document-default/package.json +++ b/core/document/document-default/package.json @@ -35,9 +35,12 @@ "reflect-metadata": "0.2.2" }, "devDependencies": { + "@vue/test-utils": "2.4.6", + "@xwiki/cristal-dev-config": "workspace:*", "reflect-metadata": "0.2.2", "typescript": "5.6.3", - "vite": "6.0.3" + "vite": "6.0.3", + "vitest-mock-extended": "2.0.2" }, "publishConfig": { "exports": { diff --git a/core/document/document-default/src/__tests__/defaultDocumentService.test.ts b/core/document/document-default/src/__tests__/defaultDocumentService.test.ts new file mode 100644 index 000000000..43027dab3 --- /dev/null +++ b/core/document/document-default/src/__tests__/defaultDocumentService.test.ts @@ -0,0 +1,80 @@ +/* + * See the LICENSE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +import { DefaultDocumentService } from "../defaultDocumentService"; +import { flushPromises } from "@vue/test-utils"; +import { + ModelReferenceParser, + ModelReferenceParserProvider, + ModelReferenceSerializer, + ModelReferenceSerializerProvider, +} from "@xwiki/cristal-model-reference-api"; +import { Container } from "inversify"; +import { createPinia, setActivePinia } from "pinia"; +import { beforeEach, describe, expect, it } from "vitest"; +import { MockProxy, any, mock } from "vitest-mock-extended"; +import type { CristalApp, PageData } from "@xwiki/cristal-api"; + +function initMocks(): MockProxy & CristalApp { + const cristalMock = mock(); + const containerMock = mock(); + const modelReferenceParserProviderMock = mock(); + modelReferenceParserProviderMock.get.mockReturnValue( + mock(), + ); + const modelReferenceSerializerProviderMock = + mock(); + modelReferenceSerializerProviderMock.get.mockReturnValue( + mock(), + ); + + containerMock.get + .calledWith("ModelReferenceParserProvider") + .mockReturnValue(modelReferenceParserProviderMock); + containerMock.get + .calledWith("ModelReferenceSerializerProvider") + .mockReturnValue(modelReferenceSerializerProviderMock); + cristalMock.getContainer.mockReturnValue(containerMock); + + return cristalMock; +} + +describe("defaultDocumentService", () => { + beforeEach(() => { + // creates a fresh pinia and makes it active so it's automatically picked up by any useStore() call without having + // to pass it to it: `useStore(pinia)` + setActivePinia(createPinia()); + }); + it("setCurrentDocument", async () => { + const mockPageData = mock(); + const cristalMock = initMocks(); + cristalMock.getPage + .calledWith("A.B.C", any()) + .mockReturnValue(Promise.resolve(mockPageData)); + const defaultDocumentService = new DefaultDocumentService(cristalMock); + + const currentDocument = defaultDocumentService.getCurrentDocument(); + + expect(currentDocument.value).toBeUndefined(); + defaultDocumentService.setCurrentDocument("A.B.C"); + // Wait for the asynchronous store operations + await flushPromises(); + expect(currentDocument.value).toBe(mockPageData); + }); +}); diff --git a/core/document/document-default/vitest.config.ts b/core/document/document-default/vitest.config.ts new file mode 100644 index 000000000..89b9fa78c --- /dev/null +++ b/core/document/document-default/vitest.config.ts @@ -0,0 +1,25 @@ +/* + * See the LICENSE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + +import localConfig from "./vite.config"; +import { mergeConfig } from "vitest/config"; +import defaultConfig from "@xwiki/cristal-dev-config/vitest-vue.config"; + +export default mergeConfig(defaultConfig, localConfig); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9bb0554f2..79d22841f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -778,6 +778,12 @@ importers: specifier: 3.5.13 version: 3.5.13(typescript@5.6.3) devDependencies: + '@vue/test-utils': + specifier: 2.4.6 + version: 2.4.6 + '@xwiki/cristal-dev-config': + specifier: workspace:* + version: link:../../../dev/config reflect-metadata: specifier: 0.2.2 version: 0.2.2 @@ -787,6 +793,9 @@ importers: vite: specifier: 6.0.3 version: 6.0.3(@types/node@22.10.2)(tsx@4.19.2)(yaml@2.6.1) + vitest-mock-extended: + specifier: 2.0.2 + version: 2.0.2(typescript@5.6.3)(vitest@2.1.8(@types/node@22.10.2)(happy-dom@15.11.7)) core/extra-tabs/extra-tabs-api: dependencies: