-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathparseAllowedFormats.spec.ts
More file actions
163 lines (153 loc) · 5.54 KB
/
Copy pathparseAllowedFormats.spec.ts
File metadata and controls
163 lines (153 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { AllowedFileFormatsType } from "../../../typings/FileUploaderProps";
import { parseAllowedFormats } from "../parseAllowedFormats";
import { dynamicValue } from "@mendix/widget-plugin-test-utils";
describe("parseAllowedFormats", () => {
test("returns parsed results for correct advanced formats", () => {
const input: AllowedFileFormatsType[] = [
{
configMode: "advanced",
typeFormatDescription: dynamicValue("test"),
predefinedType: "pdfFile",
mimeType: "image/jpeg",
extensions: ".jpg,.jpeg"
},
{
configMode: "advanced",
typeFormatDescription: dynamicValue("test2"),
predefinedType: "pdfFile",
mimeType: "application/pdf",
extensions: ".pdf"
},
{
configMode: "advanced",
typeFormatDescription: dynamicValue("test"),
predefinedType: "pdfFile",
mimeType: "text/*",
extensions: ""
},
{
configMode: "advanced",
typeFormatDescription: dynamicValue("test2"),
predefinedType: "pdfFile",
mimeType: "",
extensions: ".html,.txt"
}
];
expect(parseAllowedFormats(input)).toEqual([
{
description: "test",
entries: [
["image/jpeg", [".jpg", ".jpeg"]],
["text/*", []]
]
},
{
description: "test2",
entries: [
["application/pdf", [".pdf"]],
["dummy/mime", [".html", ".txt"]]
]
}
]);
});
test("joins extensions of duplicated mime types", () => {
const input: AllowedFileFormatsType[] = [
{
configMode: "advanced",
typeFormatDescription: dynamicValue("test"),
predefinedType: "pdfFile",
mimeType: "image/*",
extensions: ".jpg"
},
{
configMode: "advanced",
typeFormatDescription: dynamicValue("test"),
predefinedType: "pdfFile",
mimeType: "image/*",
extensions: ".png"
}
];
expect(parseAllowedFormats(input)).toEqual([
{
description: "test",
entries: [
["image/*", [".jpg"]],
["image/*", [".png"]]
]
}
]);
});
test("throws on incorrect mime format", () => {
const input: AllowedFileFormatsType[] = [
{
configMode: "advanced",
typeFormatDescription: dynamicValue("test"),
predefinedType: "pdfFile",
mimeType: "application-pdf",
extensions: ".pdf"
}
];
expect(() => parseAllowedFormats(input)).toThrow(
"Value 'application-pdf' is not recognized. Accepted format: 'image/jpeg'"
);
});
test("handles extensions with special characters like dashes and plus signs", () => {
const input: AllowedFileFormatsType[] = [
{
configMode: "advanced",
typeFormatDescription: dynamicValue("special-extensions"),
predefinedType: "pdfFile",
mimeType: "application/x-custom",
extensions: ".tar-gz,.js-map,.c++"
}
];
expect(parseAllowedFormats(input)).toEqual([
{
description: "special-extensions",
entries: [["application/x-custom", [".tar-gz", ".js-map", ".c++"]]]
}
]);
});
test("throws on extension without leading dot", () => {
const input: AllowedFileFormatsType[] = [
{
configMode: "advanced",
typeFormatDescription: dynamicValue("test"),
predefinedType: "pdfFile",
mimeType: "text/*",
extensions: ".txt,pdf"
}
];
expect(() => parseAllowedFormats(input)).toThrow(
"Value 'pdf' is not recognized. Extension must start with a dot and contain only valid filename characters"
);
});
test("throws on incorrect extension format", () => {
const input: AllowedFileFormatsType[] = [
{
configMode: "advanced",
typeFormatDescription: dynamicValue("test"),
predefinedType: "pdfFile",
mimeType: "text/*",
extensions: ".txt,.cvs,abc"
}
];
expect(() => parseAllowedFormats(input)).toThrow(
"Value 'abc' is not recognized. Extension must start with a dot and contain only valid filename characters"
);
});
test("throws on extension with dot in the middle", () => {
const input: AllowedFileFormatsType[] = [
{
configMode: "advanced",
typeFormatDescription: dynamicValue("test"),
predefinedType: "pdfFile",
mimeType: "text/*",
extensions: ".txt,.config.json"
}
];
expect(() => parseAllowedFormats(input)).toThrow(
"Value '.config.json' is not recognized. Extension must start with a dot and contain only valid filename characters"
);
});
});