Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bischofmax committed Oct 18, 2023
1 parent 02bfe22 commit 5af3539
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ describe("FileAlerts", () => {
});
});

describe("when alerts contains FileAlert.AUDIO_FORMAT_ERROR", () => {
it("should render FileAlert.AUDIO_FORMAT_ERROR", () => {
const { wrapper } = setup([FileAlert.AUDIO_FORMAT_ERROR]);

const infoAlert = wrapper.findComponent(InfoAlert);

expect(infoAlert.text()).toBe(
"components.cardElement.fileElement.audioFormatError"
);
});
});

describe("when alerts contains FileAlert.AWAITING_SCAN_STATUS", () => {
it("should render FileAlert.AWAITING_SCAN_STATUS", () => {
const { wrapper } = setup([FileAlert.AWAITING_SCAN_STATUS]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { isVideoMimeType } from "@/utils/fileHelper";
import { isAudioMimeType, isVideoMimeType } from "@/utils/fileHelper";
import { fileElementResponseFactory } from "@@/tests/test-utils";
import createComponentMocks from "@@/tests/test-utils/componentMocks";
import { shallowMount } from "@vue/test-utils";
import AudioDisplay from "./audio-display/AudioDisplay.vue";
import FileDescription from "./file-description/FileDescription.vue";
import FileDisplay from "./FileDisplay.vue";
import ImageDisplay from "./image-display/ImageDisplay.vue";
import VideoDisplay from "./video-display/VideoDisplay.vue";

jest.mock("@/utils/fileHelper");
const isVideoMimeTypeMock = jest.mocked(isVideoMimeType);
const isAudioMimeTypeMock = jest.mocked(isAudioMimeType);

describe("FileDisplay", () => {
describe("when previewUrl is defined", () => {
Expand Down Expand Up @@ -65,12 +67,12 @@ describe("FileDisplay", () => {
expect(props.element).toBeDefined();
});

it("should render file description display component", () => {
it("should pass showTitle true to file description", () => {
const { wrapper } = setup();

const fileDescription = wrapper.findComponent(FileDescription);
const props = wrapper.findComponent(FileDescription).attributes();

expect(fileDescription.exists()).toBe(true);
expect(props.showtitle).toBeFalsy();
});
});

Expand Down Expand Up @@ -135,6 +137,14 @@ describe("FileDisplay", () => {

expect(fileDescription.exists()).toBe(true);
});

it("should pass showTitle true to file description", () => {
const { wrapper } = setup();

const props = wrapper.findComponent(FileDescription).attributes();

expect(props.showtitle).toBeFalsy();
});
});
});

Expand Down Expand Up @@ -189,9 +199,74 @@ describe("FileDisplay", () => {
expect(props.src).toBe(url);
expect(props.name).toBe(fileNameProp);
});

it("should pass showTitle false to file description", () => {
const { wrapper } = setup();

const props = wrapper.findComponent(FileDescription).attributes();

expect(props.showtitle).toBeFalsy();
});
});

describe("when mimeType is not a video type", () => {
describe("when mimeType is a audio type", () => {
const setup = () => {
document.body.setAttribute("data-app", "true");

const element = fileElementResponseFactory.build();
const propsData = {
fileProperties: {
name: "test",
size: 100,
url: "test",
previewStatus: "test",
isDownloadAllowed: true,
element,
},
isEditMode: true,
};

isAudioMimeTypeMock.mockReset();
isAudioMimeTypeMock.mockReturnValueOnce(true);

const wrapper = shallowMount(FileDisplay, {
propsData,
...createComponentMocks({}),
});

return {
wrapper,
fileNameProp: propsData.fileProperties.name,
srcProp: propsData.fileProperties.url,
};
};

it("should be found in dom", () => {
const { wrapper } = setup();

const fileDisplay = wrapper.findComponent(FileDisplay);

expect(fileDisplay.exists()).toBe(true);
});

it("should pass correct props to audio display component", () => {
const { wrapper, srcProp } = setup();
console.log(wrapper.html());
const props = wrapper.findComponent(AudioDisplay).attributes();

expect(props.src).toBe(srcProp);
});

it("should pass showTitle false to file description", () => {
const { wrapper } = setup();

const props = wrapper.findComponent(FileDescription).attributes();

expect(props.showtitle).toBeFalsy();
});
});

describe("when mimeType is not a video or audio type", () => {
const setup = () => {
document.body.setAttribute("data-app", "true");

Expand All @@ -212,24 +287,25 @@ describe("FileDisplay", () => {
isVideoMimeTypeMock.mockReset();
isVideoMimeTypeMock.mockReturnValueOnce(false);

isAudioMimeTypeMock.mockReset();
isAudioMimeTypeMock.mockReturnValueOnce(false);

const wrapper = shallowMount(FileDisplay, {
propsData,
...createComponentMocks({}),
});

return {
wrapper,
fileNameProp: propsData.fileProperties.name,
previewUrlProp: propsData.fileProperties.previewUrl,
};
};

it("should render file description display component", () => {
it("should pass showTitle true to file description", () => {
const { wrapper } = setup();
console.log(wrapper.html());
const props = wrapper.findComponent(FileDescription).attributes();

const fileDescription = wrapper.findComponent(FileDescription);

expect(fileDescription.exists()).toBe(true);
expect(props.showtitle).toBeTruthy();
});

it("should not render image display component", () => {
Expand All @@ -239,6 +315,22 @@ describe("FileDisplay", () => {

expect(imageDisplay.exists()).toBe(false);
});

it("should not render audio display component", () => {
const { wrapper } = setup();

const audioDisplay = wrapper.findComponent(AudioDisplay);

expect(audioDisplay.exists()).toBe(false);
});

it("should not render video display component", () => {
const { wrapper } = setup();

const videoDisplay = wrapper.findComponent(VideoDisplay);

expect(videoDisplay.exists()).toBe(false);
});
});
});
});
51 changes: 51 additions & 0 deletions src/utils/fileHelper.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
convertFileSize,
downloadFile,
getFileExtension,
isAudioMimeType,
isDownloadAllowed,
isPreviewPossible,
isVideoMimeType,
Expand Down Expand Up @@ -378,4 +379,54 @@ describe("@/utils/fileHelper", () => {
});
});
});

describe("isAudioMimeType", () => {
describe("when file has audio mime type", () => {
describe("when file mime type has audio/ prefix", () => {
it("should return true", () => {
const result = isAudioMimeType("audio/mp4");

expect(result).toBe(true);
});

it("should return true", () => {
const result = isAudioMimeType("audio/");

expect(result).toBe(true);
});

it("should return true", () => {
const result = isAudioMimeType("audio/ ");

expect(result).toBe(true);
});
});
});

describe("when file has no audio mime type", () => {
it("should return false", () => {
const result = isAudioMimeType("image/png");

expect(result).toBe(false);
});

it("should return false", () => {
const result = isAudioMimeType("application/");

expect(result).toBe(false);
});

it("should return false", () => {
const result = isAudioMimeType(" ");

expect(result).toBe(false);
});

it("should return false", () => {
const result = isAudioMimeType("");

expect(result).toBe(false);
});
});
});
});

0 comments on commit 5af3539

Please sign in to comment.