Skip to content

Commit 5d456d5

Browse files
Merge pull request #157 from Geode-solutions/fix/auto_upload
fix(FileUploader): upload_files only if auto_upload
2 parents ffc13c6 + 0568510 commit 5d456d5

File tree

3 files changed

+93
-38
lines changed

3 files changed

+93
-38
lines changed

components/FileUploader.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,11 @@
8282
toggle_loading()
8383
}
8484
85-
if (props.files.length && props.auto_upload) {
85+
if (props.files.length) {
8686
files.value = props.files
87-
upload_files()
87+
if (props.auto_upload) {
88+
upload_files()
89+
}
8890
}
8991
9092
function clear() {

test/components/FileSelector.nuxt.test.js

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @vitest-environment nuxt
22

3-
import { describe, expect, test } from "vitest"
3+
import { describe, expect, test, vi } from "vitest"
44
import { registerEndpoint, mountSuspended } from "@nuxt/test-utils/runtime"
55
import { flushPromises } from "@vue/test-utils"
66

@@ -68,28 +68,65 @@ describe("FileSelector.vue", async () => {
6868
})
6969
})
7070

71-
test(`Files prop`, async () => {
71+
describe(`Files prop`, () => {
7272
registerEndpoint(allowed_files_schema.$id, {
7373
method: allowed_files_schema.methods[0],
7474
handler: () => ({
7575
extensions: ["1", "2", "3"],
7676
}),
7777
})
7878

79-
const files = [new File(["fake_file"], "fake_file.txt")]
80-
81-
const wrapper = await mountSuspended(FileSelector, {
82-
global: {
83-
plugins: [vuetify, pinia],
84-
},
85-
props: { multiple: false, supported_feature: "test", files: files },
86-
})
8779
registerEndpoint(upload_file_schema.$id, {
8880
method: upload_file_schema.methods[1],
8981
handler: () => ({}),
9082
})
9183

92-
await flushPromises()
93-
expect(wrapper.componentVM.files).toEqual(files)
84+
const files = [new File(["fake_file"], "fake_file.txt")]
85+
test("auto_upload true", async () => {
86+
const auto_upload = true
87+
const wrapper = await mountSuspended(FileSelector, {
88+
global: {
89+
plugins: [vuetify, pinia],
90+
},
91+
props: {
92+
multiple: false,
93+
supported_feature: "test",
94+
files: files,
95+
auto_upload,
96+
},
97+
})
98+
99+
await flushPromises()
100+
expect(wrapper.componentVM.files).toEqual(files)
101+
expect(wrapper.emitted()).toHaveProperty("update_values")
102+
expect(wrapper.emitted().update_values).toHaveLength(1)
103+
expect(wrapper.emitted().update_values[0][0]).toEqual({
104+
files,
105+
auto_upload: false,
106+
})
107+
})
108+
109+
test("auto_upload false", async () => {
110+
const auto_upload = false
111+
const wrapper = await mountSuspended(FileSelector, {
112+
global: {
113+
plugins: [vuetify, pinia],
114+
},
115+
props: {
116+
multiple: false,
117+
supported_feature: "test",
118+
files: files,
119+
auto_upload,
120+
},
121+
})
122+
123+
await flushPromises()
124+
125+
const file_uploader = wrapper.findComponent(FileUploader)
126+
console.log("wrapper", wrapper)
127+
expect(wrapper.vm.files).toEqual(files)
128+
const upload_files = vi.spyOn(file_uploader.vm, "upload_files")
129+
expect(upload_files).not.toHaveBeenCalled()
130+
})
94131
})
95132
})

test/components/FileUploader.nuxt.test.js

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,47 @@ describe("FileUploader.vue", async () => {
2727
const geode_store = use_geode_store()
2828
geode_store.base_url = ""
2929

30-
test(`Upload file`, async () => {
31-
registerEndpoint(upload_file_schema.$id, {
32-
method: upload_file_schema.methods[0],
33-
handler: () => ({}),
34-
})
35-
const wrapper = await mountSuspended(FileUploader, {
36-
global: {
37-
plugins: [vuetify, pinia],
38-
},
39-
props: { multiple: false, accept: "*.txt" },
40-
})
41-
const v_file_input = wrapper.findComponent(components.VFileInput)
42-
await v_file_input.trigger("click")
43-
const files = [new File(["fake_file"], "fake_file.txt")]
44-
await v_file_input.setValue(files)
45-
await v_file_input.trigger("change")
46-
const v_btn = wrapper.findComponent(components.VBtn)
47-
48-
registerEndpoint(upload_file_schema.$id, {
49-
method: upload_file_schema.methods[1],
50-
handler: () => ({}),
30+
registerEndpoint(upload_file_schema.$id, {
31+
method: upload_file_schema.methods[0],
32+
handler: () => ({}),
33+
})
34+
registerEndpoint(upload_file_schema.$id, {
35+
method: upload_file_schema.methods[1],
36+
handler: () => ({}),
37+
})
38+
39+
const files = [new File(["fake_file"], "fake_file.txt")]
40+
41+
describe(`Upload file`, async () => {
42+
test(`prop auto_upload false`, async () => {
43+
const wrapper = await mountSuspended(FileUploader, {
44+
global: {
45+
plugins: [vuetify, pinia],
46+
},
47+
props: { multiple: false, accept: "*.txt" },
48+
})
49+
50+
const v_file_input = wrapper.findComponent(components.VFileInput)
51+
await v_file_input.trigger("click")
52+
53+
await v_file_input.setValue(files)
54+
await v_file_input.trigger("change")
55+
const v_btn = wrapper.findComponent(components.VBtn)
56+
57+
await v_btn.trigger("click")
58+
await flushPromises()
59+
expect(wrapper.emitted().files_uploaded[0][0]).toEqual(files)
5160
})
5261

53-
await v_btn.trigger("click")
54-
await flushPromises()
55-
expect(wrapper.emitted().files_uploaded[0][0]).toEqual(files)
62+
test(`prop auto_upload true`, async () => {
63+
const wrapper = await mountSuspended(FileUploader, {
64+
global: {
65+
plugins: [vuetify, pinia],
66+
},
67+
props: { multiple: false, accept: "*.txt", files, auto_upload: true },
68+
})
69+
await flushPromises()
70+
expect(wrapper.emitted().files_uploaded[0][0]).toEqual(files)
71+
})
5672
})
5773
})

0 commit comments

Comments
 (0)