Skip to content

Commit

Permalink
chore: fix filename extension
Browse files Browse the repository at this point in the history
  • Loading branch information
valeriocomo committed Dec 3, 2024
1 parent 0ec6fdc commit 156c661
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/app/components/Foot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ChangeEvent, FormEvent, useState } from "react";
import { useTranslation } from "react-i18next";

import validator from "validator";
import { isYamlFile } from "../yaml-upload";
import { hasYamlFileExtension, isYamlFile } from "../yaml-upload";
import { ResetFormConfirm } from "./ResetFormConfirm";
import UploadModal from "./UploadModal";

Expand Down Expand Up @@ -51,8 +51,8 @@ export const Footer = (props: Props): JSX.Element => {
return;
}

const ext = value.split(/[. ]+/).pop();
if (ext != "yml" && ext != "yaml") {
const hasNotYamlFilenameExtension = !hasYamlFileExtension(value)
if (hasNotYamlFilenameExtension) {
notify(t("editor.filenotsupported"), { state: "error" });
return;
}
Expand Down
28 changes: 27 additions & 1 deletion src/app/yaml-upload.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isYamlFile } from "./yaml-upload";
import { hasYamlFileExtension, isYamlFile } from "./yaml-upload";

describe('yaml upload helper functions tests', () => {

Expand Down Expand Up @@ -29,4 +29,30 @@ describe('yaml upload helper functions tests', () => {
testFactory('application/json', false);
})
})

describe('hasYamlFileExtension function tests', () => {
const testFactory = (value?: string, assertCondition = true) => {
//arrange
//act
const actual = hasYamlFileExtension(value);
//assert
expect(actual).toBe(assertCondition)
}

it('should return true if filename has .yml extension', () => {
testFactory('publiccode.yml');
})

it('should return true if filename has .yaml extension', () => {
testFactory('publiccode.yaml');
})

it('should return false if filename has .bkp extension', () => {
testFactory('publiccode.yaml.bkp', false);
})

it('should return false if filename is undefined', () => {
testFactory(undefined, false);
})
})
})
7 changes: 6 additions & 1 deletion src/app/yaml-upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ const yamlMimeTypes = [
'text/yaml'
]

export const isYamlFile = (file?: File | null) => Boolean(file?.type && yamlMimeTypes.includes(file?.type.toLowerCase()))
export const isYamlFile = (file?: File | null) => Boolean(file?.type && yamlMimeTypes.includes(file?.type.toLowerCase()))

export const hasYamlFileExtension = (value?: string) => {
const ext = value?.split(/[. ]+/).pop();
return ext === "yml" || ext === "yaml";
}

0 comments on commit 156c661

Please sign in to comment.