This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/tests/unit/lib/uppy-media-optimization-plugin-test.js
Martin Brennan 2215cc2547
DEV: Refactor uppy plugin interfaces (#14275)
This abstracts interaction with uppy for uppy plugin classes
into base classes for Preprocessor  plugins, so anyone
making these uppy plugins doesn't have to think as much about uppy
underneath the hood. This also makes the logging and validation
nicer, and provides a more consistent way to emit progress and
completion events.

In a future commit, we will introduce another base class for
`UploadUploaderPlugin` which will be used to be able to hijack
the upload process to go to a different provider (e.g. for discourse-video)
2021-09-10 09:09:47 +10:00

165 lines
5.6 KiB
JavaScript

import UppyMediaOptimization from "discourse/lib/uppy-media-optimization-plugin";
import { module, test } from "qunit";
import { Promise } from "rsvp";
class FakeUppy {
constructor() {
this.preprocessors = [];
this.emitted = [];
this.files = {
"uppy-test/file/vv2/xvejg5w/blah/jpg-1d-1d-2v-1d-1e-image/jpeg-9043429-1624921727764": {
data: "old file state",
},
"uppy-test/file/blah1/ads37x2/blah1/jpg-1d-1d-2v-1d-1e-image/jpeg-99999-1837921727764": {
data: "old file state 1",
},
};
}
addPreProcessor(fn) {
this.preprocessors.push(fn);
}
getFile(id) {
return this.files[id];
}
emit(event, file, data) {
this.emitted.push({ event, file, data });
}
setFileState(fileId, state) {
this.files[fileId] = state;
}
}
module("Unit | Utility | UppyMediaOptimization Plugin", function () {
test("sets the options passed in", function (assert) {
const fakeUppy = new FakeUppy();
const plugin = new UppyMediaOptimization(fakeUppy, {
runParallel: true,
optimizeFn: function () {
return "wow such optimized";
},
});
assert.equal(plugin.id, "uppy-media-optimization");
assert.equal(plugin.runParallel, true);
assert.equal(plugin.optimizeFn(), "wow such optimized");
});
test("installation uses the correct function", function (assert) {
const fakeUppy = new FakeUppy();
const plugin = new UppyMediaOptimization(fakeUppy, {
runParallel: true,
});
plugin._optimizeParallel = function () {
return "using parallel";
};
plugin._optimizeSerial = function () {
return "using serial";
};
plugin.install();
assert.equal(plugin.uppy.preprocessors[0](), "using parallel");
plugin.runParallel = false;
plugin.uppy.preprocessors = [];
plugin.install();
assert.equal(plugin.uppy.preprocessors[0](), "using serial");
});
test("sets the file state when successfully optimizing the file and emits events", function (assert) {
const fakeUppy = new FakeUppy();
const plugin = new UppyMediaOptimization(fakeUppy, {
runParallel: true,
optimizeFn: () => {
return Promise.resolve("new file state");
},
});
plugin.install();
const done = assert.async();
const fileId =
"uppy-test/file/vv2/xvejg5w/blah/jpg-1d-1d-2v-1d-1e-image/jpeg-9043429-1624921727764";
plugin.uppy.preprocessors[0]([fileId]).then(() => {
assert.equal(plugin.uppy.emitted[0].event, "preprocess-progress");
assert.equal(plugin.uppy.emitted[1].event, "preprocess-complete");
assert.equal(plugin.uppy.getFile(fileId).data, "new file state");
done();
});
});
test("handles optimizer errors gracefully by leaving old file state and calling preprocess-complete", function (assert) {
const fakeUppy = new FakeUppy();
const plugin = new UppyMediaOptimization(fakeUppy, {
runParallel: true,
optimizeFn: () => {
return new Promise(() => {
throw new Error("bad stuff");
});
},
});
plugin.install();
const done = assert.async();
const fileId =
"uppy-test/file/vv2/xvejg5w/blah/jpg-1d-1d-2v-1d-1e-image/jpeg-9043429-1624921727764";
plugin.uppy.preprocessors[0]([fileId]).then(() => {
assert.equal(plugin.uppy.emitted[0].event, "preprocess-progress");
assert.equal(plugin.uppy.emitted[1].event, "preprocess-complete");
assert.equal(plugin.uppy.getFile(fileId).data, "old file state");
done();
});
});
test("handles serial file optimization successfully", function (assert) {
const fakeUppy = new FakeUppy();
const plugin = new UppyMediaOptimization(fakeUppy, {
runParallel: false,
optimizeFn: () => {
return Promise.resolve("new file state");
},
});
plugin.install();
const done = assert.async();
const fileIds = [
"uppy-test/file/vv2/xvejg5w/blah/jpg-1d-1d-2v-1d-1e-image/jpeg-9043429-1624921727764",
"uppy-test/file/blah1/ads37x2/blah1/jpg-1d-1d-2v-1d-1e-image/jpeg-99999-1837921727764",
];
plugin.uppy.preprocessors[0](fileIds).then(() => {
assert.equal(plugin.uppy.emitted[0].event, "preprocess-progress");
assert.equal(plugin.uppy.emitted[1].event, "preprocess-complete");
assert.equal(plugin.uppy.emitted[2].event, "preprocess-progress");
assert.equal(plugin.uppy.emitted[3].event, "preprocess-complete");
assert.equal(plugin.uppy.getFile(fileIds[0]).data, "new file state");
assert.equal(plugin.uppy.getFile(fileIds[1]).data, "new file state");
done();
});
});
test("handles parallel file optimization successfully", function (assert) {
const fakeUppy = new FakeUppy();
const plugin = new UppyMediaOptimization(fakeUppy, {
runParallel: true,
optimizeFn: () => {
return Promise.resolve("new file state");
},
});
plugin.install();
const done = assert.async();
const fileIds = [
"uppy-test/file/vv2/xvejg5w/blah/jpg-1d-1d-2v-1d-1e-image/jpeg-9043429-1624921727764",
"uppy-test/file/blah1/ads37x2/blah1/jpg-1d-1d-2v-1d-1e-image/jpeg-99999-1837921727764",
];
plugin.uppy.preprocessors[0](fileIds).then(() => {
assert.equal(plugin.uppy.emitted[0].event, "preprocess-progress");
assert.equal(plugin.uppy.emitted[1].event, "preprocess-progress");
assert.equal(plugin.uppy.emitted[2].event, "preprocess-complete");
assert.equal(plugin.uppy.emitted[3].event, "preprocess-complete");
assert.equal(plugin.uppy.getFile(fileIds[0]).data, "new file state");
assert.equal(plugin.uppy.getFile(fileIds[1]).data, "new file state");
done();
});
});
});