Commit Graph

317 Commits

Author SHA1 Message Date
Anders Kaseorg 4df3fef88f zjsunit: Add babel-plugin-lazy-require.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-02-28 14:23:00 -08:00
Anders Kaseorg b93e00f213 zjsunit: Enable Babel lazy imports.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-02-28 14:23:00 -08:00
Anders Kaseorg aee94f141d zjsunit: Remove _patched_with_set_global check.
Most of our set_global usage will disappear as our globals do, anyway.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-02-28 14:23:00 -08:00
Anders Kaseorg db50451a5a zjsunit: Use __Rewire__ for ES modules in with_field.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-02-28 14:23:00 -08:00
Anders Kaseorg 8985b5ffc7 zjsunit: Use __Rewire__ for ES modules in override.
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 5437a16602 zjquery: Remove bogus remove(). 2021-02-25 13:42:47 -05: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
Anders Kaseorg 3ccb4391d8 zjsunit: Restore missing fields correctly in with_field.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-02-22 20:21:41 -08:00
Anders Kaseorg 58b18fd499 zjsunit: Handle exceptions in with_field, with_overrides.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-02-22 20:21:41 -08:00
Anders Kaseorg 0ca543396a zjsunit: Use clear_zulip_refs as the main way to undo zrequire.
Move clear_zulip_refs into restore, and rewrite it without lodash.  We
no longer need the requires array, and zrequire is now nothing more
than a wrapper around require.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-02-22 10:53:49 -08: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
Aman Agrawal 2b8921bf67 zjquery: Tell user why using tags as selector is invalid.
Like using $('input') is too broad a selector and shouldn't
be used in the codebase. With this error messages, contributors
can easily understand that now.
2021-02-22 10:24:59 -05:00
Aman Agrawal 269457c58b zjquery: Extract func to verify selector. 2021-02-22 10:24:59 -05:00
Steve Howell 914533c8bc zjsunit: Prevent common mistakes with overrides.
We only allow you to replace functions with functions,
unless you have used set_global() or are dealing
with $ from zjquery.
2021-02-22 10:04:29 -05:00
Steve Howell 5caee967d3 zjquery: Remove bogus after/before functions. 2021-02-22 10:04:29 -05:00
Steve Howell 132e67cb28 zjquery: Prohibit extensions to $.fn.
We make an exception for the popovers code.

Luckily it's pretty rare that we extend $.fn
in our real code.
2021-02-22 10:04:29 -05:00
Steve Howell 89ab76920f zjquery: Prevent direct patches to $.
We prohibit code from making direct patches to
$ unless you use override().
2021-02-22 10:04:29 -05:00
Steve Howell b40d8d5790 node tests: Inline setup for settings_bots.
I also use override in more places, and I add
$.validator to zjquery.

In passing I clean up some IIFEs that aren't
really needed.
2021-02-22 10:04:29 -05:00
Steve Howell ffe1043bcc zjsunit: Remove stub_out_jquery.
We now either use zjquery or do whatever gross
things we need to do to work past document.trigger(...)
calls or $.now calls explicitly in the tests.
2021-02-21 17:34:55 -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
Steve Howell 2e3406f0b8 node tests: Eliminate "silent" option for zjquery.
The "silent" option was kind of evil, as it had
$(...).find(...) passing back "self" instead of a stub.

Now we just use $(...).set_find_results(...) or
override(...) to simulate/bypass drawing code.

(It turns out hash_util didn't even need this option.)
2021-02-21 15:11:31 -05:00
Steve Howell 5bd73ce190 zjsunit: Show more of the traceback.
We still reduce a lot of clutter, but this
shows wrappers around `run_test` now.
2021-02-20 12:00:24 -05:00
Abhijeet Prasad Bodas c183076a91 minor: Suggest with_field instead of override for non-functions. 2021-02-13 07:28:26 -05:00
Abhijeet Prasad Bodas a24d8272e8 zjsunit: Remove with_stub helper.
This doesn't add much clarity to the tests.
Using scoped stub objects is preferred instead.
2021-02-13 06:48:59 -05:00
Steve Howell 1598344fa7 zjsunit: Rename `module` argument to `obj`.
Due to recent changes, `with_overrides` is no longer
constrained to overriding module methods.  You can
override methods on any type of object.
2021-02-12 12:11:23 -05:00
Steve Howell 21cb0d1547 minor: Suggest using with_field instead of override. 2021-02-12 12:11:23 -05:00
Steve Howell 16ad65dd66 zjsunit: Restrict overrides to once-per-function.
We can relax this restriction in the future, but
basically every time it came up for me, the test
code was just disorganized, or it had an easy
workaround.
2021-02-11 11:29:21 -05:00
Anders Kaseorg 2f6c9b8d0e zjsunit: Change override API to work with non-global modules.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-02-11 07:35:28 -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
Steve Howell c1b499ea9e node tests: Simplify poll_widget test.
This is aided by adding a "toggle" helper to zjquery.
2021-02-09 10:42:33 -05:00
Steve Howell 66eaa1698c zjquery: Alphabetize methods. 2021-02-09 10:42:33 -05:00
Steve Howell 131375a26e zjquery: Allow passing children to $.create().
It's actually pretty rare in our codebase to
call methods like `$(...).map` or `$(...).each`,
but we now support them better in zjquery.

You can pass a list of child elements now to
`$.create(...)`.
2021-02-09 10:42:33 -05:00
Steve Howell 31f7f26a22 zjquery: Add set_height helper. 2021-02-09 10:42:33 -05:00
Anders Kaseorg aa650a4c88 js: Escape strings interpolated into CSS selectors with CSS.escape.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-02-04 11:00:06 -08:00
Anders Kaseorg 08fe6e4a8f blueslip: Pass through the return value in measure_time.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-02-04 09:11:04 -05:00
Steve Howell 016038dcd1 blueslip: Add measure_time wrapper.
Now when we want to measure how long a block
of code takes to execute, we just wrap it with
`blueslip.measure_time`, instead of the awkward
idiom from my original commit of getting a callback
function.

My rationale for the original scheme was that I
wanted to minimize diffs and avoid changing
`const` to `let` in a few cases, but I believe
now that the function wrapper is nicer.

In a few cases I just removed the blueslip timing
code, since I was able to confirm on czo that
the times were pretty minimal.
2021-02-03 15:23:17 -08:00
Anders Kaseorg 6cd694b8e3 eslint: Fix unicorn/no-array-callback-reference.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-01-25 14:53:19 -08:00
Anders Kaseorg 552f4e3d22 eslint: Fix unicorn/no-array-for-each.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-01-25 14:53:19 -08:00
Anders Kaseorg 2c5e9f65f8 eslint: Fix new-cap errors.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2020-12-10 19:52:22 -08:00
Anders Kaseorg f84fd0839d zjquery: Allow $.Event as a constructor.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2020-12-10 19:17:28 -08:00
Anders Kaseorg 5a87c34705 zjsunit: Fix trigger mock argument handling.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2020-12-10 19:07:08 -08:00
Anders Kaseorg d100d67d4c zjsunit: Set window with set_global.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2020-12-01 07:14:00 -05: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 22166e8ea9 zjsunit: Deglobalize read_fixture_data.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2020-12-01 07:14:00 -05:00
Anders Kaseorg cc162a9067 zjsunit: Deglobalize markdown_assert.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2020-12-01 07:14:00 -05:00
Anders Kaseorg 83fedb4057 zjsunit: Deglobalize handlebars.
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 59124c7559 zjsunit: Deglobalize stub.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2020-12-01 07:14:00 -05:00