Skip to content

Commit

Permalink
[Misc] Add missing tests for AttachmentsTable
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelleduc committed Dec 31, 2024
1 parent 9853e57 commit 59f9fbc
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 0 deletions.
5 changes: 5 additions & 0 deletions core/attachments/attachments-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,14 @@
"reflect-metadata": "0.2.2"
},
"devDependencies": {
"@vue/test-utils": "2.4.6",
"@xwiki/cristal-dev-config": "workspace:*",
"@xwiki/cristal-dev-test-utils": "workspace:*",
"reflect-metadata": "0.2.2",
"typescript": "5.6.3",
"vite": "6.0.3",
"vitest": "2.1.8",
"vitest-mock-extended": "2.0.2",
"vue-tsc": "2.1.10"
},
"publishConfig": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* 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 AttachmentsTable from "../AttachmentsTable.vue";
import { shallowMount } from "@vue/test-utils";
import { CristalApp } from "@xwiki/cristal-api";
import { Attachment } from "@xwiki/cristal-attachments-api";
import { mockI18n } from "@xwiki/cristal-dev-test-utils";
import {
AttachmentReference,
DocumentReference,
EntityType,
} from "@xwiki/cristal-model-api";
import { ClickListener } from "@xwiki/cristal-model-click-listener";
import {
ModelReferenceParser,
ModelReferenceParserProvider,
} from "@xwiki/cristal-model-reference-api";
import { Container } from "inversify";
import { describe, expect, it, vi } from "vitest";
import { mock } from "vitest-mock-extended";

function initializeMocks() {
const cristalMock = mock<CristalApp>();
const containerMock = mock<Container>();
containerMock.get
.calledWith("ClickListener")
.mockReturnValue(mock<ClickListener>());
const modelReferenceParserProviderMock = mock<ModelReferenceParserProvider>();
const modelReferenceParserMock = mock<ModelReferenceParser>();
modelReferenceParserMock.parse
.calledWith("test.jpg", EntityType.ATTACHMENT)
.mockReturnValue(
new AttachmentReference("test.jpg", new DocumentReference("A")),
);
modelReferenceParserProviderMock.get.mockReturnValue(
modelReferenceParserMock,
);
containerMock.get
.calledWith("ModelReferenceParserProvider")
.mockReturnValue(modelReferenceParserProviderMock);
cristalMock.getContainer.mockReturnValue(containerMock);
return cristalMock;
}

function shallowMountAttachmentsTable({
attachments,
errorMessage,
isLoading,
}: {
attachments: Attachment[];
errorMessage?: string;
isLoading: boolean;
}) {
vi.mock("vue-router");
vi.mock("vue-i18n");
mockI18n();
const cristalMock = initializeMocks();
return shallowMount(AttachmentsTable, {
props: {
attachments,
errorMessage,
isLoading,
},
global: {
provide: {
cristal: cristalMock,
},
},
});
}

describe("attachments-table", () => {
it("Display a loading message", () => {
const attachmentsTable = shallowMountAttachmentsTable({
attachments: [],
errorMessage: undefined,
isLoading: true,
});

expect(attachmentsTable.text()).toEqual("attachments.tab.loading");
});

it("Display an error message", () => {
const attachmentsTable = shallowMountAttachmentsTable({
attachments: [],
errorMessage: "error message",
isLoading: false,
});

expect(attachmentsTable.text()).toEqual("error message");
});

it("Display an empty list", () => {
const attachmentsTable = shallowMountAttachmentsTable({
attachments: [],
errorMessage: undefined,
isLoading: false,
});

expect(attachmentsTable.text()).toEqual("attachments.tab.noAttachments");
});

// eslint-disable-next-line max-statements
it("Display a list", () => {
const date = new Date();
const attachmentsTable = shallowMountAttachmentsTable({
attachments: [
{
size: 1000,
name: "test.jpg",
href: "http://localhost/test.jpg",
mimetype: "application/pdf",
date: date,
author: {
name: "UserName",
},
id: "[email protected]",
},
],
errorMessage: undefined,
isLoading: false,
});

const profileLink = attachmentsTable.find("tbody td:nth-child(1) a");
expect(profileLink.attributes("href")).toBe("http://localhost/test.jpg");
expect(profileLink.text()).toBe("test.jpg");
const mimeType = attachmentsTable.find("tbody td:nth-child(2)");
expect(mimeType.text()).toBe(
"attachments.tab.table.header.mimetype application/pdf",
);
const fileSize = attachmentsTable.find(
"tbody td:nth-child(3) file-size-stub",
);
expect(fileSize.attributes("size")).toBe("1000");
const dateCell = attachmentsTable.find("tbody td:nth-child(4) c-date-stub");
const attributes: string = dateCell.attributes("date") ?? "";
expect(new Date(attributes).getTime()).toBe(date.setMilliseconds(0));
});
});
25 changes: 25 additions & 0 deletions core/attachments/attachments-ui/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -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);
15 changes: 15 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 59f9fbc

Please sign in to comment.