Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
compose_validate test: Use FakeComposeBox for size limits.
Steve Howella1217fbc5a32
diff --git a/web/tests/compose_validate.test.cjs b/web/tests/compose_validate.test.cjsindex 274374b00ae..e70dc1eddbf 100644--- a/web/tests/compose_validate.test.cjs+++ b/web/tests/compose_validate.test.cjs@@ -553,42 +553,31 @@ test_ui("test_stream_posting_permission", ({mock_template, override}) => {assert.ok(!banner_rendered);});-test_ui("test_check_overflow_text", ({mock_template, override}) => {+test_ui("test_check_overflow_text", ({override}) => {+ const fake_compose_box = new FakeComposeBox();+override(realm, "max_message_length", 10000);- const $elem = $("#send_message_form");- const $textarea = $(".message-textarea");- const $indicator = $(".message-limit-indicator");- stub_message_row($textarea);- $elem.set_find_results(".message-textarea", $textarea);- $elem.set_find_results(".message-limit-indicator", $indicator);+ // RED+ {+ fake_compose_box.set_textarea_val("a".repeat(10005));+ compose_validate.check_overflow_text(fake_compose_box.$send_message_form);+ fake_compose_box.assert_message_size_is_over_the_limit("-5\n");+ }- // Indicator should show red colored text- let limit_indicator_html;- mock_template("compose_limit_indicator.hbs", true, (_data, html) => {- limit_indicator_html = html;- });- $textarea.val("a".repeat(10000 + 1));- compose_validate.check_overflow_text($elem);- assert.ok($indicator.hasClass("textarea-over-limit"));- assert.equal(limit_indicator_html, "-1\n");- assert.ok($textarea.hasClass("textarea-over-limit"));- assert.ok($(".message-send-controls").hasClass("disabled-message-send-controls"));-- // Indicator should show orange colored text- $textarea.val("a".repeat(9100));- compose_validate.check_overflow_text($elem);- assert.ok(!$indicator.hasClass("textarea-over-limit"));- assert.equal(limit_indicator_html, "900\n");- assert.ok(!$textarea.hasClass("textarea-over-limit"));- assert.ok(!$(".message-send-controls").hasClass("disabled-message-send-controls"));-- // Indicator must be empty- $textarea.val("a".repeat(9100 - 1));- compose_validate.check_overflow_text($elem);- assert.ok(!$indicator.hasClass("textarea-over-limit"));- assert.equal($indicator.text(), "");- assert.ok(!$textarea.hasClass("textarea-over-limit"));+ // ORANGE+ {+ fake_compose_box.set_textarea_val("a".repeat(9100));+ compose_validate.check_overflow_text(fake_compose_box.$send_message_form);+ fake_compose_box.assert_message_size_is_under_the_limit("900\n");+ }++ // ALL CLEAR+ {+ fake_compose_box.set_textarea_val("a".repeat(9100 - 1));+ compose_validate.check_overflow_text(fake_compose_box.$send_message_form);+ fake_compose_box.assert_message_size_is_under_the_limit();+ }});test_ui("needs_subscribe_warning", () => {diff --git a/web/tests/lib/compose_helpers.cjs b/web/tests/lib/compose_helpers.cjsindex f9f243c601c..bf10388a275 100644--- a/web/tests/lib/compose_helpers.cjs+++ b/web/tests/lib/compose_helpers.cjs@@ -7,15 +7,16 @@ const $ = require("./zjquery.cjs");class FakeComposeBox {constructor() {+ this.$send_message_form = $("#send_message_form");this.$content_textarea = $("textarea#compose-textarea");this.$preview_message_area = $("#compose .preview_message_area");// Simulate DOM relationships- $("#send_message_form").set_find_results(".message-textarea", this.$content_textarea);+ this.$send_message_form.set_find_results(".message-textarea", this.$content_textarea);- $("#send_message_form").set_find_results(+ this.$send_message_form.set_find_results(".message-limit-indicator",- $.create("limit-indicator-stub"),+ $(".message-limit-indicator"),);const $message_row_stub = $.create("message_row_stub");@@ -29,6 +30,9 @@ class FakeComposeBox {}reset() {+ $(".message-limit-indicator").html("");+ $(".message-limit-indicator").text("");+$("#compose_banners .user_not_subscribed").length = 0;this.$content_textarea.toggleClass = noop;@@ -135,6 +139,30 @@ class FakeComposeBox {assert.ok($("#compose .undo_markdown_preview").visible());assert.ok($("#compose").hasClass("preview_mode"));}++ assert_message_size_is_over_the_limit(desired_html) {+ // Indicator should show red colored text+ assert.equal($(".message-limit-indicator").html(), desired_html);++ assert.ok(this.$content_textarea.hasClass("textarea-over-limit"));+ assert.ok($(".message-limit-indicator").hasClass("textarea-over-limit"));+ assert.ok($(".message-send-controls").hasClass("disabled-message-send-controls"));+ }++ assert_message_size_is_under_the_limit(desired_html) {+ // Work around the quirk that our validation code+ // arbitrarily switches between html() and text(),+ // and zjquery doesn't unify text and html.+ if (desired_html) {+ assert.equal($(".message-limit-indicator").html(), desired_html);+ } else {+ assert.equal($(".message-limit-indicator").text(), "");+ }++ assert.ok(!this.$content_textarea.hasClass("textarea-over-limit"));+ assert.ok(!$(".message-limit-indicator").hasClass("textarea-over-limit"));+ assert.ok(!$(".message-send-controls").hasClass("disabled-message-send-controls"));+ }}module.exports = {FakeComposeBox};