generated from xwiki-contrib/github-template-repository
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Misc] Add missing tests for AttachmentsTable
- Loading branch information
1 parent
9853e57
commit 59f9fbc
Showing
4 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
core/attachments/attachments-ui/src/vue/__tests__/AttachmentsTable.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.