Commit Graph

42 Commits

Author SHA1 Message Date
Anders Kaseorg b0dba411d9 js: Skip redundant jQuery object reconstruction.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2022-08-18 16:11:13 -07:00
Anders Kaseorg c520890f54 node_tests: Remove low-hanging uses of __Rewire__.
When we were preparing the conversion to ES modules in 2019, the
primary obstacle was that the Node tests extensively relied on the
ability to reach into modules and mutate their CommonJS exports in
order to mock things.  ES module bindings are not mutable, so in
commit 173c9cee42 we added
babel-plugin-rewire-ts as a kludgy transpilation-based workaround for
this to unblock the conversion.

However, babel-plugin-rewire-ts is slow, buggy, nonstandard,
confusing, and unmaintained.  It’s incompatible with running our ES
modules as native ES modules, and prevents us from taking advantage of
modern tools for ES modules.  So we want to excise all use of
__Rewire__ (and the disallow_rewire, override_rewire helper functions
that rely on it) from the tests and remove babel-plugin-rewire-ts.

Commits 64abdc199e and
e17ba5260a (#20730) prepared for this by
letting us see where __Rewire__ is being used.  Now we go through and
remove most of the uses that are easy to remove without modifying the
production code at all.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
2022-07-13 16:27:30 -07:00
Anders Kaseorg f84a2c08d5 js: Prefix jQuery object variable names with $.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2022-03-16 12:52:07 -07:00
Austin Riba 1c2fd19d69 lightbox: Revert state of lightbox when closing the overlay.
This commit attempts to revert the lightbox state (at least
the pan/zoom part of it) when the lightbox is closed. The open()
function has been turned into a factory function so that the zoom
control which is instantiated on initialization of the lightbox can be
passed in to the on_close handler of overlay.open().
2022-02-04 14:58:36 -08:00
Anders Kaseorg 64abdc199e zjsunit: Split deprecated __Rewire__ functionality out of override.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2022-01-08 10:01:27 -05:00
Steve Howell d5c4495fa5 zjsunit: Wire $ to zjquery by default.
I introduce `mock_jquery` for modules that
want their own mock implementation.
2021-06-16 12:55:53 -04:00
Steve Howell 9e657dd795 node tests: Use {override} in run_test. 2021-06-16 12:55:53 -04:00
Steve Howell 92e611a787 node tests: Add test() wrapper for lightbox. 2021-03-14 10:46:45 -04:00
Anders Kaseorg ea9ca6b7d0 js: Use jQuery as a module.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-03-12 10:08:25 -08:00
Anders Kaseorg 876806eb4d zjsunit: Lift restriction against mocking third party modules.
Use fully resolvable request paths because we need to be able to refer
to third party modules, and to increase uniformity and explicitness.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-03-12 10:06:30 -08:00
Steve Howell 9c8fa3930f node tests: Eliminate many __Rewire__ calls.
For most functions that we were using __Rewire__ for,
it's better to just use the override helper, which
use __Rewire__ under the hood, but also resets
the reference at the end of run_tests.

Another nice thing about override() is that it reports
when you never actually needed the mock, and this
commit fixes the instances found here.

I didn't replace every call to __Rewire__. The
remaining ones fall under these categories:

    * I looked for ") =>" in my code sweep,
      so I missed stuff like "noop" helpers.

    * Sometimes we directly update something
      in a module that's not a function. This
      is generally evil, and we should use setters.

    * Some tests have setup() helpers or similar
      that complicated this code sweep, so I
      simply punted.

    * Somes modules rely on intra-test leaks. We
      should fix those, but I just punted for the
      main code sweep.
2021-03-07 11:19:33 -05:00
Steve Howell f54f7cfc33 node tests: Introduce mock_module helper. 2021-03-06 13:15:04 -05:00
Steve Howell 167fda142c node tests: Remove __esModule cruft.
We just set __esModule in our rewiremock helper.
2021-03-06 12:36:07 -05:00
Steve Howell 30c7108955 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 11:10:57 -05:00
Anders Kaseorg 7145cb4a0d js: Convert static/js/message_store.js to ES6 module.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-02-28 14:23:00 -08:00
Anders Kaseorg 45b8e0244e js: Convert static/js/overlays.js to ES6 module.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-02-28 14:23:00 -08:00
Anders Kaseorg b3f74af090 js: Convert static/js/popovers.js to ES6 module.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-02-28 14:23:00 -08:00
Anders Kaseorg 592cd780f5 js: Convert static/js/rows.js to ES6 module.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-02-28 14:23:00 -08:00
Anders Kaseorg 54c97c4457 node_tests: Consistently move set_global mocks before zrequire calls.
This way, as we convert them to rewiremock, they will become available
before they are imported.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-02-26 07:55:53 -08:00
Steve Howell ac69450681 zjsunit: Clear $ elements in run_test.
We now call $.clear_all_elements at the top
of run_test.

We have to exempt two modules from the new regime:

    compose
    settings_user_groups

Also, if modules do set_global("$", ...) we don't
try to call the non-existent function.

It's possible we'll want to move to something like
this, but we might want to clean up the two
sloppy_$ modules first:

    // AVOID THIS:
    // const $ = require("zjquery")

    run_test("test widget", ({override, $}) => {
        override(foo, "bar", ...);
        $.create(...);
        // do stuff
    });
2021-02-23 07:55:43 -05:00
Steve Howell ff3175f8a2 lightbox: Export method for testing purposes.
We weren't exercising this method in any
meaningful way during the tests, and when
do add coverage, we probably want to just
test it directly.

We also kill off stub_selector(), which was
never well-documented.
2021-02-22 12:38:07 -05:00
Steve Howell 42c2c9fb2d zjquery: Avoid array-related hacks.
Callers can either explicitly pass in children,
stub out $(...)[0] as needed, or just
circumvent jQuery complications with override.

Note the reactions test was broken before,
since $(...)[0] was always returning the same
stub.
2021-02-22 12:38:07 -05:00
Steve Howell 64c7eb67eb zjquery: Make zjquery a singleton.
We no longer export make_zjquery().

We now instead have a singleton zjquery instance
that we attach to global.$ in index.js.

We call $.clear_all_elements() before each module.
(We will soon get even more aggressive about doing
it in run_test.)

Test functions can still override $ with set_global.
A good example of this is copy_and_paste using the
real jquery module.

We no longer exempt $ as a global variable, so
test modules that use the zjquery $ need to do:

    const $ = require("../zjsunit/zjquery");
2021-02-21 17:34:55 -05:00
Anders Kaseorg 89aa3155a9 node_tests: Don’t read from most deprecated global variables.
We still need to write to these globals with set_global because the
code being tested reads from them, but the tests themselves should
never need to read from them.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-02-10 07:40:22 -08:00
Anders Kaseorg 21d432e12c zjsunit: Deglobalize run_test.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2020-12-01 07:14:00 -05:00
Anders Kaseorg 3715e68598 zjsunit: Deglobalize zjquery.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2020-12-01 07:14:00 -05:00
Anders Kaseorg 425f1789e2 zjsunit: Deglobalize namespace.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2020-12-01 07:14:00 -05:00
Anders Kaseorg 7b03d48798 zjsunit: Deglobalize assert.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2020-12-01 07:14:00 -05:00
Anders Kaseorg 6ec808b8df js: Add "use strict" directive to CommonJS files.
ES and TypeScript modules are strict by default and don’t need this
directive.  ESLint will remind us to add it to new CommonJS files and
remove it from ES and TypeScript modules.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
2020-07-31 22:09:46 -07:00
Anders Kaseorg f3726db89a js: Normalize strings to double quotes.
Prettier would do this anyway, but it’s separated out for a more
reviewable diff.  Generated by ESLint.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
2020-07-17 14:31:24 -07:00
Steve Howell e64059de79 node tests: Remove most test_log length assertions.
For all the places where we just make zero or one
blueslip call, asserting for length is either
unnecessary or overkill.
2020-04-08 11:37:27 -04:00
Hashir Sarwar 9a6d83e731 lightbox: Use `$preview_src` as key for `asset_map`.
In case of video embeds, the previous logic used
`data-src-fullsize` or `src` as a key to look
for the metadata of video in `lightbox.open()`,
but while parsing, the key used while storing
the metadata was the video ID.

This doesn't make any sense because video's data
could never be accessed from `asset_map` and we
always needed to lookup the DOM for this.

This commit fixes this by using $img.attr('src')
as a key for `asset_map` for both, images and
videos. Since `src` is the link of preview image
in case of video embeds, it will always uniquely
determine the video ID and we won't loose
anything with the change in how videos handle
things.

Part of #14152.
2020-04-05 13:13:02 -07:00
Steve Howell f7b432afec node tests: Auto-include zblueslip for node tests.
We already use blueslip stubs in ~45 tests, so we
may as well just auto-include it.
2020-04-03 12:56:49 -04:00
Steve Howell 02947d6b41 blueslip: Protect against drafts pitfall for rows.id.
If folks use an overly broad selector for message rows,
they will accidentally include drafts from the drafts
dialog, which won't have zids.  More specific selectors
will be more efficient and possibly prevent strange
behaviors.

For testing convenience, we extract the message.
2020-04-02 14:44:35 -07:00
Steve Howell 531cafb501 rows.js: Add error handling to rows.id().
We get random blueslip errors from code that
calls rows.id(), and the error messages are
rarely helpful.
2020-02-24 12:19:10 -08:00
Anders Kaseorg 2834b051d2 js: Read zid with rows.id.
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-12 10:39:01 -08:00
Steve Howell 8f0018ee7c node tests: Use stubs in lightbox tests.
We stub out jquery elements rather than giving
the illusion of having real DOM.

Also, we make it so that the message_store
interaction has an assertion attached to it.
2020-02-11 14:19:03 -05:00
Chris Heald fb09bd5df6 node: Fix lightbox tests to use message_store.get().
While there are still a number of problems with these tests, they now
more effectively exercise the real code.
2020-02-10 14:00:10 -08:00
Anders Kaseorg 28f3dfa284 js: Automatically convert var to let and const in most files.
This commit was originally automatically generated using `tools/lint
--only=eslint --fix`.  It was then modified by tabbott to contain only
changes to a set of files that are unlikely to result in significant
merge conflicts with any open pull request, excluding about 20 files.
His plan is to merge the remaining changes with more precise care,
potentially involving merging parts of conflicting pull requests
before running the `eslint --fix` operation.

Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2019-11-03 12:42:39 -08:00
Puneeth Chaganti 75635844db lightbox: Set URL in payload instead of computing from video IDs.
Lightbox previews for youtube playlists use the "current" video in the playlist
for the preview. The open link for such previews is incorrectly set to the first
video alone, and not the playlist. This commit fixes the bug by linking to the
original URL for lightbox preview is being shown, instead of computing the URL.
2019-05-29 23:01:54 -07:00
Aditya Bansal 5b5d8bb310 thumbnails: Rename data-original to data-src-fullsize. 2018-07-30 13:00:23 -07:00
Marco Burstein 11cbe2bf6a lightbox: Fix a "Pan & Zoom" causing an error with thumbnailed images.
Images on the new thumbnailing system generate an error when
"Pan & Zoom" is enabled:

    Browser Error: Lightbox for unknown message undefined
    39648: http://localhost:9991/webpack/app.js
        | Object.blueslip_error [as error]
    50743: http://localhost:9991/webpack/app.js
        | Object../static/js/lightbox.js.exports.open
    50897: http://localhost:9991/webpack/app.js
        | HTMLDivElement.
    39474: http://localhost:9991/webpack/app.js
        | HTMLDivElement.blueslip_wrapper
    16299: http://localhost:9991/webpack/app.js
        | HTMLDivElement.dispatch
    16107: http://localhost:9991/webpack/app.js
        | HTMLDivElement.elemData.handle

To fix this, instead of using the `src` attribute as the key for
`asset_map`, which can be either the original or thumbnailed version
depending on the situation, always use the original version.

Also, create `frontend_tests/node_tests/lightbox.js` to test this
functionality.

Fix #9955.
2018-07-18 10:19:56 -07:00