2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
2019-02-27 12:52:05 +01:00
|
|
|
const fs = require("fs");
|
2020-07-24 06:02:07 +02:00
|
|
|
|
|
|
|
const {JSDOM} = require("jsdom");
|
|
|
|
|
2021-03-11 05:43:45 +01:00
|
|
|
const {mock_cjs, mock_esm, set_global, zrequire} = require("../zjsunit/namespace");
|
|
|
|
const jQueryFactory = require("../zjsunit/real_jquery");
|
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");
|
2021-03-25 22:35:45 +01:00
|
|
|
const {page_params} = require("../zjsunit/zpage_params");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2019-02-27 12:52:05 +01:00
|
|
|
const template = fs.readFileSync("templates/corporate/upgrade.html", "utf-8");
|
2020-07-16 22:40:18 +02:00
|
|
|
const dom = new JSDOM(template, {pretendToBeVisual: true});
|
2020-12-11 04:26:23 +01:00
|
|
|
const jquery = jQueryFactory(dom.window);
|
2019-02-27 12:52:05 +01:00
|
|
|
|
2021-03-11 05:43:45 +01:00
|
|
|
mock_cjs("jquery", $);
|
|
|
|
|
2021-02-10 04:53:22 +01:00
|
|
|
const history = set_global("history", {});
|
2021-03-10 06:10:32 +01:00
|
|
|
const loading = mock_esm("../../static/js/loading");
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("document", {
|
2019-02-27 12:52:05 +01:00
|
|
|
title: "Zulip",
|
|
|
|
});
|
2021-03-13 16:54:47 +01:00
|
|
|
const location = set_global("location", {
|
2019-02-27 12:52:05 +01:00
|
|
|
pathname: "/upgrade/",
|
|
|
|
search: "",
|
|
|
|
hash: "#billing",
|
|
|
|
});
|
2019-01-02 12:00:51 +01:00
|
|
|
|
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 helpers = zrequire("billing/helpers");
|
2019-02-27 12:52:05 +01:00
|
|
|
|
2021-02-22 12:27:42 +01:00
|
|
|
run_test("create_ajax_request", (override) => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const form_loading_indicator = "#autopay_loading_indicator";
|
|
|
|
const form_input_section = "#autopay-input-section";
|
|
|
|
const form_success = "#autopay-success";
|
|
|
|
const form_error = "#autopay-error";
|
|
|
|
const form_loading = "#autopay-loading";
|
2020-05-22 15:42:46 +02:00
|
|
|
const zulip_limited_section = "#zulip-limited-section";
|
2020-05-22 15:30:39 +02:00
|
|
|
const free_trial_alert_message = "#free-trial-alert-message";
|
2019-02-27 12:52:05 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const state = {
|
2019-02-27 12:52:05 +01:00
|
|
|
form_input_section_show: 0,
|
|
|
|
form_input_section_hide: 0,
|
|
|
|
form_error_show: 0,
|
|
|
|
form_error_hide: 0,
|
|
|
|
form_loading_show: 0,
|
|
|
|
form_loading_hide: 0,
|
|
|
|
form_success_show: 0,
|
2020-05-22 15:42:46 +02:00
|
|
|
zulip_limited_section_show: 0,
|
|
|
|
zulip_limited_section_hide: 0,
|
2020-05-22 15:30:39 +02:00
|
|
|
free_trial_alert_message_hide: 0,
|
|
|
|
free_trial_alert_message_show: 0,
|
2019-02-27 12:52:05 +01:00
|
|
|
location_reload: 0,
|
|
|
|
pushState: 0,
|
|
|
|
make_indicator: 0,
|
|
|
|
};
|
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
loading.make_indicator = (loading_indicator, config) => {
|
2019-02-27 12:52:05 +01:00
|
|
|
assert.equal(loading_indicator.selector, form_loading_indicator);
|
|
|
|
assert.equal(config.text, "Processing ...");
|
|
|
|
assert.equal(config.abs_positioned, true);
|
|
|
|
state.make_indicator += 1;
|
|
|
|
};
|
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
$(form_input_section).hide = () => {
|
2019-02-27 12:52:05 +01:00
|
|
|
state.form_input_section_hide += 1;
|
|
|
|
};
|
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
$(form_input_section).show = () => {
|
2019-02-27 12:52:05 +01:00
|
|
|
state.form_input_section_show += 1;
|
|
|
|
};
|
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
$(form_error).hide = () => {
|
2019-02-27 12:52:05 +01:00
|
|
|
state.form_error_hide += 1;
|
|
|
|
};
|
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
$(form_error).show = () => {
|
2019-02-27 12:52:05 +01:00
|
|
|
state.form_error_show += 1;
|
|
|
|
return {
|
|
|
|
text: (msg) => {
|
|
|
|
assert.equal(msg, "response_message");
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
$(form_success).show = () => {
|
2019-02-27 12:52:05 +01:00
|
|
|
state.form_success_show += 1;
|
|
|
|
};
|
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
$(form_loading).show = () => {
|
2019-02-27 12:52:05 +01:00
|
|
|
state.form_loading_show += 1;
|
|
|
|
};
|
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
$(form_loading).hide = () => {
|
2019-02-27 12:52:05 +01:00
|
|
|
state.form_loading_hide += 1;
|
|
|
|
};
|
|
|
|
|
2020-05-22 15:42:46 +02:00
|
|
|
$(zulip_limited_section).show = () => {
|
|
|
|
state.zulip_limited_section_show += 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
$(zulip_limited_section).hide = () => {
|
|
|
|
state.zulip_limited_section_hide += 1;
|
|
|
|
};
|
|
|
|
|
2020-05-22 15:30:39 +02:00
|
|
|
$(free_trial_alert_message).show = () => {
|
|
|
|
state.free_trial_alert_message_show += 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
$(free_trial_alert_message).hide = () => {
|
|
|
|
state.free_trial_alert_message_hide += 1;
|
|
|
|
};
|
|
|
|
|
2020-07-02 01:41:40 +02:00
|
|
|
$("#autopay-form").serializeArray = () => jquery("#autopay-form").serializeArray();
|
2019-02-27 12:52:05 +01:00
|
|
|
|
2020-12-10 18:15:09 +01:00
|
|
|
override($, "ajax", ({type, url, data, success, error}) => {
|
2019-02-27 12:52:05 +01:00
|
|
|
assert.equal(state.form_input_section_hide, 1);
|
|
|
|
assert.equal(state.form_error_hide, 1);
|
|
|
|
assert.equal(state.form_loading_show, 1);
|
2020-05-22 15:42:46 +02:00
|
|
|
assert.equal(state.zulip_limited_section_hide, 1);
|
|
|
|
assert.equal(state.zulip_limited_section_show, 0);
|
2020-05-22 15:30:39 +02:00
|
|
|
assert.equal(state.free_trial_alert_message_hide, 1);
|
|
|
|
assert.equal(state.free_trial_alert_message_show, 0);
|
2019-02-27 12:52:05 +01:00
|
|
|
assert.equal(state.make_indicator, 1);
|
|
|
|
|
2020-12-10 18:15:09 +01:00
|
|
|
assert.equal(type, "PATCH");
|
2019-02-27 12:52:05 +01:00
|
|
|
assert.equal(url, "/json/billing/upgrade");
|
|
|
|
|
|
|
|
assert.equal(Object.keys(data).length, 8);
|
2021-04-09 12:43:44 +02:00
|
|
|
assert.equal(data.stripe_token, "stripe_token_id");
|
|
|
|
assert.equal(data.seat_count, "{{ seat_count }}");
|
|
|
|
assert.equal(data.signed_seat_count, "{{ signed_seat_count }}");
|
|
|
|
assert.equal(data.salt, "{{ salt }}");
|
|
|
|
assert.equal(data.billing_modality, "charge_automatically");
|
|
|
|
assert.equal(data.schedule, "monthly");
|
|
|
|
assert.equal(data.license_management, "automatic");
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(data.licenses, "");
|
2019-02-27 12:52:05 +01:00
|
|
|
|
|
|
|
history.pushState = (state_object, title, path) => {
|
|
|
|
state.pushState += 1;
|
|
|
|
assert.equal(state_object, "");
|
|
|
|
assert.equal(title, "Zulip");
|
|
|
|
assert.equal(path, "/upgrade/");
|
|
|
|
};
|
|
|
|
|
|
|
|
location.reload = () => {
|
|
|
|
state.location_reload += 1;
|
|
|
|
};
|
|
|
|
|
2020-06-09 12:24:32 +02:00
|
|
|
window.location.replace = (reload_to) => {
|
|
|
|
state.location_reload += 1;
|
|
|
|
assert.equal(reload_to, "/billing");
|
|
|
|
};
|
|
|
|
|
2019-02-27 12:52:05 +01:00
|
|
|
success();
|
|
|
|
|
|
|
|
assert.equal(state.location_reload, 1);
|
|
|
|
assert.equal(state.pushState, 1);
|
|
|
|
assert.equal(state.form_success_show, 1);
|
|
|
|
assert.equal(state.form_error_hide, 2);
|
|
|
|
assert.equal(state.form_loading_hide, 1);
|
2020-05-22 15:42:46 +02:00
|
|
|
assert.equal(state.zulip_limited_section_hide, 1);
|
|
|
|
assert.equal(state.zulip_limited_section_show, 0);
|
2020-05-22 15:30:39 +02:00
|
|
|
assert.equal(state.free_trial_alert_message_hide, 1);
|
|
|
|
assert.equal(state.free_trial_alert_message_show, 0);
|
2019-02-27 12:52:05 +01:00
|
|
|
|
|
|
|
error({responseText: '{"msg": "response_message"}'});
|
|
|
|
|
|
|
|
assert.equal(state.form_loading_hide, 2);
|
|
|
|
assert.equal(state.form_error_show, 1);
|
|
|
|
assert.equal(state.form_input_section_show, 1);
|
2020-05-22 15:42:46 +02:00
|
|
|
assert.equal(state.zulip_limited_section_hide, 1);
|
2020-05-22 15:30:39 +02:00
|
|
|
assert.equal(state.free_trial_alert_message_hide, 1);
|
|
|
|
assert.equal(state.free_trial_alert_message_show, 1);
|
2021-02-22 12:27:42 +01:00
|
|
|
});
|
2019-02-27 12:52:05 +01:00
|
|
|
|
2020-12-10 18:15:09 +01:00
|
|
|
helpers.create_ajax_request(
|
|
|
|
"/json/billing/upgrade",
|
|
|
|
"autopay",
|
|
|
|
{id: "stripe_token_id"},
|
|
|
|
undefined,
|
|
|
|
"PATCH",
|
|
|
|
);
|
2019-02-27 12:52:05 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
run_test("format_money", () => {
|
|
|
|
assert.equal(helpers.format_money("100"), "1");
|
|
|
|
assert.equal(helpers.format_money("123.00"), "1.23");
|
|
|
|
assert.equal(helpers.format_money("123.45"), "1.24");
|
|
|
|
assert.equal(helpers.format_money("600"), "6");
|
|
|
|
assert.equal(helpers.format_money("640"), "6.40");
|
|
|
|
assert.equal(helpers.format_money("666.6666666666666"), "6.67");
|
|
|
|
assert.equal(helpers.format_money("7600"), "76");
|
|
|
|
assert.equal(helpers.format_money("8000"), "80");
|
|
|
|
assert.equal(helpers.format_money("123416.323"), "1234.17");
|
|
|
|
assert.equal(helpers.format_money("927268238"), "9272682.38");
|
|
|
|
});
|
|
|
|
|
|
|
|
run_test("update_charged_amount", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const prices = {};
|
2019-02-27 12:52:05 +01:00
|
|
|
prices.annual = 8000;
|
|
|
|
prices.monthly = 800;
|
|
|
|
page_params.seat_count = 35;
|
|
|
|
|
|
|
|
helpers.update_charged_amount(prices, "annual");
|
2020-02-12 11:49:02 +01:00
|
|
|
assert.equal($("#charged_amount").text(), (80 * 35).toString());
|
2019-02-27 12:52:05 +01:00
|
|
|
|
|
|
|
helpers.update_charged_amount(prices, "monthly");
|
2020-02-12 11:49:02 +01:00
|
|
|
assert.equal($("#charged_amount").text(), (8 * 35).toString());
|
2019-02-27 12:52:05 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
run_test("show_license_section", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const state = {
|
2019-02-27 12:52:05 +01:00
|
|
|
show_license_automatic_section: 0,
|
|
|
|
show_license_manual_section: 0,
|
|
|
|
hide_license_automatic_section: 0,
|
|
|
|
hide_license_manual_section: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
$("#license-automatic-section").show = () => {
|
|
|
|
state.show_license_automatic_section += 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
$("#license-manual-section").show = () => {
|
|
|
|
state.show_license_manual_section += 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
$("#license-automatic-section").hide = () => {
|
|
|
|
state.hide_license_automatic_section += 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
$("#license-manual-section").hide = () => {
|
|
|
|
state.hide_license_manual_section += 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
helpers.show_license_section("automatic");
|
|
|
|
|
|
|
|
assert.equal(state.hide_license_automatic_section, 1);
|
|
|
|
assert.equal(state.hide_license_manual_section, 1);
|
|
|
|
assert.equal(state.show_license_automatic_section, 1);
|
|
|
|
assert.equal(state.show_license_manual_section, 0);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal($("#automatic_license_count").prop("disabled"), false);
|
|
|
|
assert.equal($("#manual_license_count").prop("disabled"), true);
|
2019-02-27 12:52:05 +01:00
|
|
|
|
|
|
|
helpers.show_license_section("manual");
|
|
|
|
|
|
|
|
assert.equal(state.hide_license_automatic_section, 2);
|
|
|
|
assert.equal(state.hide_license_manual_section, 2);
|
|
|
|
assert.equal(state.show_license_automatic_section, 1);
|
|
|
|
assert.equal(state.show_license_manual_section, 1);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal($("#automatic_license_count").prop("disabled"), true);
|
|
|
|
assert.equal($("#manual_license_count").prop("disabled"), false);
|
2019-02-27 12:52:05 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
run_test("set_tab", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const state = {
|
2019-02-27 12:52:05 +01:00
|
|
|
show_tab_billing: 0,
|
|
|
|
show_tab_payment_method: 0,
|
|
|
|
scrollTop: 0,
|
|
|
|
};
|
|
|
|
|
2021-02-03 23:23:32 +01:00
|
|
|
$('#upgrade-tabs.nav a[href="\\#billing"]').tab = (action) => {
|
2019-02-27 12:52:05 +01:00
|
|
|
state.show_tab_billing += 1;
|
|
|
|
assert.equal(action, "show");
|
|
|
|
};
|
|
|
|
|
2021-02-03 23:23:32 +01:00
|
|
|
$('#upgrade-tabs.nav a[href="\\#payment-method"]').tab = (action) => {
|
2019-02-27 12:52:05 +01:00
|
|
|
state.show_tab_payment_method += 1;
|
|
|
|
assert.equal(action, "show");
|
|
|
|
};
|
|
|
|
|
|
|
|
$("html").scrollTop = (val) => {
|
|
|
|
state.scrollTop += 1;
|
|
|
|
assert.equal(val, 0);
|
|
|
|
};
|
|
|
|
|
2020-10-07 11:47:20 +02:00
|
|
|
let hash_change_handler;
|
|
|
|
window.addEventListener = (event, handler) => {
|
|
|
|
assert.equal(event, "hashchange");
|
|
|
|
hash_change_handler = handler;
|
|
|
|
};
|
|
|
|
|
2021-03-13 16:54:47 +01:00
|
|
|
location.hash = "#billing";
|
2019-02-27 12:52:05 +01:00
|
|
|
helpers.set_tab("upgrade");
|
|
|
|
assert.equal(state.show_tab_billing, 1);
|
|
|
|
assert.equal(state.scrollTop, 1);
|
|
|
|
|
|
|
|
const click_handler = $("#upgrade-tabs.nav-tabs a").get_on_handler("click");
|
|
|
|
click_handler.call({hash: "#payment-method"});
|
|
|
|
assert.equal(location.hash, "#payment-method");
|
|
|
|
|
|
|
|
hash_change_handler();
|
|
|
|
assert.equal(state.show_tab_payment_method, 1);
|
|
|
|
assert.equal(state.scrollTop, 2);
|
|
|
|
});
|