Skip to content

Commit f5e3896

Browse files
committed
resolves fileLimit issue and adds harnesses for testing.
1 parent 65935e8 commit f5e3896

File tree

4 files changed

+132
-18
lines changed

4 files changed

+132
-18
lines changed

src/lib/file.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@ import {
77
FileSizeMinimumNotMetError,
88
InvalidFileTypeError,
99
} from "./errors";
10+
1011
import type { FileWithPath } from "file-selector";
1112

13+
declare module "file-selector" {
14+
interface FileWithPath extends Blob {
15+
readonly path?: string;
16+
readonly webkitRelativePath: string;
17+
}
18+
}
19+
1220
export interface RejectedFile {
1321
file: FileWithPath;
1422
error: FileDropError;
@@ -27,7 +35,8 @@ type CheckParams = {
2735

2836
export function processFiles(files: FileWithPath[], options: FileDropOptions): Files {
2937
let { fileLimit } = options;
30-
if (options.multiple) {
38+
39+
if (options.multiple != undefined && !options.multiple) {
3140
fileLimit = 1;
3241
}
3342
let count = 0;
@@ -37,7 +46,7 @@ export function processFiles(files: FileWithPath[], options: FileDropOptions): F
3746
if (error != undefined) {
3847
accumulator.rejected.push({ file, error });
3948
return accumulator;
40-
} else if (fileLimit > 0 && count > fileLimit) {
49+
} else if (fileLimit > 0 && count >= fileLimit) {
4150
error = new FileCountExceededError(file, fileLimit);
4251
accumulator.rejected.push({ file, error });
4352
return accumulator;
@@ -50,7 +59,7 @@ export function processFiles(files: FileWithPath[], options: FileDropOptions): F
5059
);
5160
}
5261

53-
export function checkFile(file: File, params: CheckParams): FileDropError | undefined {
62+
export function checkFile(file: FileWithPath, params: CheckParams): FileDropError | undefined {
5463
const { accept, minSize: min, maxSize: max } = params;
5564
return checkFileType(file, accept) || checkFileSize(file, { min, max });
5665
}

src/routes/test/action.svelte

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,49 @@
1-
<script lang="ts">
2-
import type { FileDropOptions } from "$lib";
3-
// import { filedrop } from "$lib/actions/filedrop";
4-
let options: FileDropOptions = {};
1+
<script context="module" lang="ts">
2+
import type { Load } from "@sveltejs/kit";
3+
import type { FileDropOptions } from "$lib/options";
54
6-
globalThis.setOptions = (opts: FileDropOptions) => {
7-
options = opts;
5+
export const load: Load = async function ({ page }) {
6+
const { query } = page;
7+
function getNumber(key: string): number | undefined {
8+
const str = query.get(key);
9+
const n = parseInt(str);
10+
if (!isNaN(n)) {
11+
return n;
12+
}
13+
return undefined;
14+
}
15+
function getBool(key: string): boolean | undefined {
16+
const b = query.get(key);
17+
if (b === "true") {
18+
return true;
19+
}
20+
if (b === "false") {
21+
return false;
22+
}
23+
return undefined;
24+
}
25+
function getAccept(): string | string[] | undefined {
26+
const accept = query.getAll("accept").filter((v) => v.length > 0);
27+
if (accept.length === 1) {
28+
return accept[0];
29+
}
30+
if (accept.length > 1) {
31+
return accept;
32+
}
33+
return undefined;
34+
}
35+
const opts: FileDropOptions = {
36+
accept: getAccept(),
37+
clickToUpload: getBool("clickToUpload"),
38+
disabled: getBool("disabled"),
39+
hideInput: getBool("hideInput"),
40+
fileLimit: getNumber("fileLimit"),
41+
maxSize: getNumber("maxSize"),
42+
minSize: getNumber("minSize"),
43+
windowDrop: getBool("windowDrop"),
44+
multiple: getBool("multiple"),
45+
tabIndex: getNumber("tabIndex"),
46+
};
47+
return { props: { opts } };
848
};
949
</script>

src/routes/test/component.svelte

Lines changed: 71 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,75 @@
1-
<script lang="ts">
2-
import type { FileDropOptions } from "$lib";
3-
4-
import FileDrop from "$lib/components";
5-
let options: FileDropOptions = {};
1+
<script context="module" lang="ts">
2+
import type { Load } from "@sveltejs/kit";
3+
import type { FileDropOptions } from "$lib/options";
64
7-
globalThis.setOptions = (opts: FileDropOptions) => {
8-
options = opts;
5+
export const load: Load = async function ({ page }) {
6+
const { query } = page;
7+
function getNumber(key: string): number | undefined {
8+
const str = query.get(key);
9+
const n = parseInt(str);
10+
if (!isNaN(n)) {
11+
return n;
12+
}
13+
return undefined;
14+
}
15+
function getBool(key: string): boolean | undefined {
16+
const b = query.get(key);
17+
if (b === "true") {
18+
return true;
19+
}
20+
if (b === "false") {
21+
return false;
22+
}
23+
return undefined;
24+
}
25+
function getAccept(): string | string[] | undefined {
26+
const accept = query.getAll("accept").filter((v) => v.length > 0);
27+
if (accept.length === 1) {
28+
return accept[0];
29+
}
30+
if (accept.length > 1) {
31+
return accept;
32+
}
33+
return undefined;
34+
}
35+
const opts: FileDropOptions = {
36+
accept: getAccept(),
37+
clickToUpload: getBool("clickToUpload"),
38+
disabled: getBool("disabled"),
39+
hideInput: getBool("hideInput"),
40+
fileLimit: getNumber("fileLimit"),
41+
maxSize: getNumber("maxSize"),
42+
minSize: getNumber("minSize"),
43+
windowDrop: getBool("windowDrop"),
44+
multiple: getBool("multiple"),
45+
tabIndex: getNumber("tabIndex"),
46+
};
47+
return { props: { opts } };
948
};
1049
</script>
1150

12-
<FileDrop {...options} />
51+
<script lang="ts">
52+
import FileDrop from "$lib/components";
53+
import type { Files } from "$lib/file";
54+
import fileSize from "filesize";
55+
export let opts: FileDropOptions = {};
56+
let files: Files;
57+
console.log(opts);
58+
</script>
59+
60+
<FileDrop {...opts} on:filedrop={(e) => (files = e.detail.files)} />
61+
62+
{#if files}
63+
<h3>Accepted files</h3>
64+
<ul>
65+
{#each files.accepted as file}
66+
<li>{file.name} - {fileSize(file.size)}</li>
67+
{/each}
68+
</ul>
69+
<h3>Rejected files</h3>
70+
<ul>
71+
{#each files.rejected as rejected}
72+
<li>{rejected.file.name} - {rejected.error.message}</li>
73+
{/each}
74+
</ul>
75+
{/if}

tests/components/FileDrop.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { test, expect } from "@playwright/test";
22

33
test("basic test", async ({ page }) => {
4-
await page.goto("http://localhost:3000");
4+
page.on("console", (msg) => console.log(msg.text()));
5+
6+
await page.goto("http://localhost:3000/test/component?disabled=true");
57
});

0 commit comments

Comments
 (0)