2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
zjsunit: Remove rewiremock dependency.
We now just use a module._load hook to inject
stubs into our code.
For conversion purposes I temporarily maintain
the API of rewiremock, apart from the enable/disable
pieces, but I will make a better wrapper in an
upcoming commit.
We can detect when rewiremock is called after
zrequire now, and I fix all the violations in
this commit, mostly by using override.
We can also detect when a mock is needlessly
created, and I fix all the violations in this
commit.
The one minor nuisance that this commit introduces
is that you can only stub out modules in the Zulip
source tree, which is now static/js. This should
not really be a problem--there are usually better
techniques to deal with third party depenencies.
In the prior commit I show a typical workaround,
which is to create a one-line wrapper in your
test code. It's often the case that you can simply
use override(), as well.
In passing I kill off `reset_modules`, and I
eliminated the second argument to zrequire,
which dates back to pre-es6 days.
2021-03-06 12:47:54 +01:00
|
|
|
const {rewiremock, set_global, zrequire} = require("../zjsunit/namespace");
|
2020-12-01 00:39:47 +01:00
|
|
|
const {run_test} = require("../zjsunit/test");
|
2021-02-21 15:38:51 +01:00
|
|
|
const $ = require("../zjsunit/zjquery");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("Image", class Image {});
|
2021-02-28 01:03:46 +01:00
|
|
|
rewiremock("../../static/js/overlays").with({
|
2018-07-16 20:50:57 +02:00
|
|
|
close_overlay: () => {},
|
2021-03-06 17:37:51 +01:00
|
|
|
|
2018-07-16 20:50:57 +02:00
|
|
|
close_active: () => {},
|
|
|
|
open_overlay: () => {},
|
|
|
|
});
|
2021-02-28 01:03:09 +01:00
|
|
|
rewiremock("../../static/js/popovers").with({
|
2018-07-16 20:50:57 +02:00
|
|
|
hide_all: () => {},
|
|
|
|
});
|
2019-05-21 17:36:39 +02:00
|
|
|
|
2021-03-06 17:37:51 +01:00
|
|
|
const message_store = rewiremock("../../static/js/message_store").with({});
|
2020-12-01 23:21:38 +01:00
|
|
|
const rows = zrequire("rows");
|
2021-03-06 17:37:51 +01:00
|
|
|
|
2020-12-01 23:21:38 +01:00
|
|
|
const lightbox = zrequire("lightbox");
|
|
|
|
|
2021-02-28 00:42:30 +01:00
|
|
|
rows.__Rewire__("is_draft_row", () => false);
|
2020-04-02 15:13:23 +02:00
|
|
|
|
2021-02-22 17:01:29 +01:00
|
|
|
run_test("pan_and_zoom", (override) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
const img = $.create("img-stub");
|
|
|
|
const link = $.create("link-stub");
|
|
|
|
const msg = $.create("msg-stub");
|
2020-02-10 23:21:06 +01:00
|
|
|
|
|
|
|
$(img).closest = () => [];
|
2020-02-10 17:59:40 +01:00
|
|
|
|
2020-02-10 23:21:06 +01:00
|
|
|
img.set_parent(link);
|
|
|
|
link.closest = () => msg;
|
2021-02-22 17:01:29 +01:00
|
|
|
|
|
|
|
override(rows, "id", (row) => {
|
|
|
|
assert.equal(row, msg);
|
|
|
|
return 1234;
|
|
|
|
});
|
|
|
|
|
2020-04-02 15:07:30 +02:00
|
|
|
img.attr("src", "example");
|
2020-02-10 23:21:06 +01:00
|
|
|
|
|
|
|
let fetched_zid;
|
|
|
|
|
|
|
|
message_store.get = (zid) => {
|
|
|
|
fetched_zid = zid;
|
2020-07-15 01:29:15 +02:00
|
|
|
return "message-stub";
|
2020-02-10 23:21:06 +01:00
|
|
|
};
|
2019-05-21 17:36:39 +02:00
|
|
|
|
2021-02-22 18:03:43 +01:00
|
|
|
override(lightbox, "render_lightbox_list_images", () => {});
|
2019-05-21 17:36:39 +02:00
|
|
|
|
|
|
|
lightbox.open(img);
|
2020-02-10 23:21:06 +01:00
|
|
|
|
|
|
|
assert.equal(fetched_zid, 1234);
|
2018-07-16 20:50:57 +02:00
|
|
|
});
|
2019-05-21 17:36:39 +02:00
|
|
|
|
2021-02-22 17:01:29 +01:00
|
|
|
run_test("youtube", (override) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
const href = "https://youtube.com/some-random-clip";
|
|
|
|
const img = $.create("img-stub");
|
|
|
|
const link = $.create("link-stub");
|
|
|
|
const msg = $.create("msg-stub");
|
2020-02-10 23:21:06 +01:00
|
|
|
|
2021-02-22 17:01:29 +01:00
|
|
|
override(rows, "id", (row) => {
|
|
|
|
assert.equal(row, msg);
|
|
|
|
return 4321;
|
|
|
|
});
|
2020-02-21 17:29:23 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
$(img).attr("src", href);
|
2020-02-10 23:21:06 +01:00
|
|
|
|
|
|
|
$(img).closest = (sel) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
if (sel === ".youtube-video") {
|
2020-02-10 23:21:06 +01:00
|
|
|
// We just need a nonempty array to
|
|
|
|
// set is_youtube_video to true.
|
2020-07-15 01:29:15 +02:00
|
|
|
return ["whatever"];
|
2020-02-10 23:21:06 +01:00
|
|
|
}
|
|
|
|
return [];
|
|
|
|
};
|
|
|
|
|
|
|
|
img.set_parent(link);
|
|
|
|
link.closest = () => msg;
|
2020-07-15 01:29:15 +02:00
|
|
|
link.attr("href", href);
|
2019-05-21 17:36:39 +02:00
|
|
|
|
2021-02-22 18:03:43 +01:00
|
|
|
override(lightbox, "render_lightbox_list_images", () => {});
|
2019-05-21 17:36:39 +02:00
|
|
|
|
|
|
|
lightbox.open(img);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal($(".image-actions .open").attr("href"), href);
|
2019-05-21 17:36:39 +02:00
|
|
|
});
|