2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
2019-01-08 18:06:06 +01:00
|
|
|
const fs = require("fs");
|
2023-02-24 02:47:55 +01:00
|
|
|
const path = require("path");
|
2019-01-08 18:06:06 +01:00
|
|
|
|
2020-07-24 06:02:07 +02:00
|
|
|
const {JSDOM} = require("jsdom");
|
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const {mock_esm, set_global, zrequire} = require("./lib/namespace");
|
|
|
|
const {run_test} = require("./lib/test");
|
|
|
|
const $ = require("./lib/zjquery");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2023-02-24 02:47:55 +01:00
|
|
|
const template = fs.readFileSync(
|
|
|
|
path.resolve(__dirname, "../../templates/corporate/billing.html"),
|
|
|
|
"utf8",
|
|
|
|
);
|
2020-07-16 22:40:18 +02:00
|
|
|
const dom = new JSDOM(template, {pretendToBeVisual: true});
|
2019-01-08 18:06:06 +01:00
|
|
|
const document = dom.window.document;
|
2021-10-14 00:23:21 +02:00
|
|
|
const location = set_global("location", {});
|
2019-01-08 18:06:06 +01:00
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const helpers = mock_esm("../src/billing/helpers", {
|
2022-11-17 23:33:43 +01:00
|
|
|
set_tab() {},
|
2023-02-13 20:40:51 +01:00
|
|
|
set_sponsorship_form() {},
|
2021-03-07 13:57:14 +01:00
|
|
|
});
|
2019-01-08 18:06:06 +01:00
|
|
|
|
2022-07-10 01:06:33 +02:00
|
|
|
zrequire("billing/billing");
|
2021-03-13 15:49:01 +01:00
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
run_test("initialize", ({override}) => {
|
2020-04-24 17:38:13 +02:00
|
|
|
let set_tab_called = false;
|
2021-02-23 14:10:56 +01:00
|
|
|
override(helpers, "set_tab", (page_name) => {
|
2019-01-08 18:06:06 +01:00
|
|
|
assert.equal(page_name, "billing");
|
2020-04-24 17:38:13 +02:00
|
|
|
set_tab_called = true;
|
2021-02-23 14:10:56 +01:00
|
|
|
});
|
2023-02-13 20:40:51 +01:00
|
|
|
let set_sponsorship_form_called = false;
|
|
|
|
override(helpers, "set_sponsorship_form", () => {
|
|
|
|
set_sponsorship_form_called = true;
|
|
|
|
});
|
2021-08-29 15:33:29 +02:00
|
|
|
$.get_initialize_function()();
|
|
|
|
assert.ok(set_tab_called);
|
2023-02-13 20:40:51 +01:00
|
|
|
assert.ok(set_sponsorship_form_called);
|
2021-08-29 15:33:29 +02:00
|
|
|
});
|
2019-01-08 18:06:06 +01:00
|
|
|
|
2021-08-29 15:33:29 +02:00
|
|
|
run_test("card_update", ({override}) => {
|
|
|
|
override(helpers, "set_tab", () => {});
|
2020-04-24 17:38:13 +02:00
|
|
|
let create_ajax_request_called = false;
|
2021-08-29 15:33:29 +02:00
|
|
|
function card_change_ajax(url, form_name, ignored_inputs, method, success_callback) {
|
|
|
|
assert.equal(url, "/json/billing/session/start_card_update_session");
|
2019-01-08 18:06:06 +01:00
|
|
|
assert.equal(form_name, "cardchange");
|
2020-09-03 15:34:20 +02:00
|
|
|
assert.deepEqual(ignored_inputs, []);
|
|
|
|
assert.equal(method, "POST");
|
2021-10-14 00:23:21 +02:00
|
|
|
location.replace = (new_location) => {
|
2021-08-29 15:33:29 +02:00
|
|
|
assert.equal(new_location, "https://stripe_session_url");
|
2020-09-03 15:34:20 +02:00
|
|
|
};
|
2021-08-29 15:33:29 +02:00
|
|
|
success_callback({stripe_session_url: "https://stripe_session_url"});
|
2020-04-24 17:38:13 +02:00
|
|
|
create_ajax_request_called = true;
|
2021-02-23 14:10:56 +01:00
|
|
|
}
|
2019-01-08 18:06:06 +01:00
|
|
|
|
2021-03-13 15:49:01 +01:00
|
|
|
$.get_initialize_function()();
|
2019-01-08 18:06:06 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const update_card_click_handler = $("#update-card-button").get_on_handler("click");
|
2022-07-09 23:25:05 +02:00
|
|
|
override(helpers, "create_ajax_request", card_change_ajax);
|
2022-11-17 23:33:43 +01:00
|
|
|
update_card_click_handler({preventDefault() {}});
|
2022-07-09 23:25:05 +02:00
|
|
|
assert.ok(create_ajax_request_called);
|
2021-08-29 15:33:29 +02:00
|
|
|
});
|
2020-04-24 17:38:13 +02:00
|
|
|
|
2021-08-29 15:33:29 +02:00
|
|
|
run_test("planchange", ({override}) => {
|
|
|
|
override(helpers, "set_tab", () => {});
|
|
|
|
let create_ajax_request_called = false;
|
|
|
|
function plan_change_ajax(url, form_name, ignored_inputs, method, success_callback) {
|
2020-12-10 18:15:09 +01:00
|
|
|
assert.equal(url, "/json/billing/plan");
|
2020-04-24 17:38:13 +02:00
|
|
|
assert.equal(form_name, "planchange");
|
2020-09-03 15:34:20 +02:00
|
|
|
assert.deepEqual(ignored_inputs, []);
|
2020-12-10 18:15:09 +01:00
|
|
|
assert.equal(method, "PATCH");
|
2021-10-14 00:23:21 +02:00
|
|
|
location.replace = (new_location) => {
|
2023-03-23 04:28:36 +01:00
|
|
|
assert.equal(new_location, "/billing/");
|
2020-09-03 15:34:20 +02:00
|
|
|
};
|
|
|
|
success_callback();
|
2020-04-24 17:38:13 +02:00
|
|
|
create_ajax_request_called = true;
|
2021-02-23 14:10:56 +01:00
|
|
|
}
|
2020-04-24 17:38:13 +02:00
|
|
|
|
2021-08-29 15:33:29 +02:00
|
|
|
$.get_initialize_function()();
|
2020-07-15 01:29:15 +02:00
|
|
|
const change_plan_status_click_handler = $("#change-plan-status").get_on_handler("click");
|
2021-02-23 14:10:56 +01:00
|
|
|
|
2022-07-09 23:25:05 +02:00
|
|
|
override(helpers, "create_ajax_request", plan_change_ajax);
|
2022-11-17 23:33:43 +01:00
|
|
|
change_plan_status_click_handler({preventDefault() {}});
|
2022-07-09 23:25:05 +02:00
|
|
|
assert.ok(create_ajax_request_called);
|
2021-08-29 15:33:29 +02:00
|
|
|
});
|
2020-12-23 17:08:27 +01:00
|
|
|
|
2022-07-10 01:06:33 +02:00
|
|
|
run_test("licensechange", ({override}) => {
|
2021-08-29 15:33:29 +02:00
|
|
|
override(helpers, "set_tab", () => {});
|
|
|
|
let create_ajax_request_called = false;
|
|
|
|
function license_change_ajax(url, form_name, ignored_inputs, method, success_callback) {
|
2020-12-23 17:08:27 +01:00
|
|
|
assert.equal(url, "/json/billing/plan");
|
|
|
|
assert.equal(form_name, "licensechange");
|
|
|
|
assert.deepEqual(ignored_inputs, ["licenses_at_next_renewal"]);
|
|
|
|
assert.equal(method, "PATCH");
|
2021-10-14 00:23:21 +02:00
|
|
|
location.replace = (new_location) => {
|
2023-03-23 04:28:36 +01:00
|
|
|
assert.equal(new_location, "/billing/");
|
2020-09-03 15:34:20 +02:00
|
|
|
};
|
|
|
|
success_callback();
|
2020-12-23 17:08:27 +01:00
|
|
|
create_ajax_request_called = true;
|
|
|
|
}
|
2022-07-09 23:25:05 +02:00
|
|
|
override(helpers, "create_ajax_request", license_change_ajax);
|
2020-12-23 17:08:27 +01:00
|
|
|
|
2021-08-29 15:33:29 +02:00
|
|
|
$.get_initialize_function()();
|
|
|
|
|
2020-12-23 17:08:27 +01:00
|
|
|
const confirm_license_update_click_handler = $("#confirm-license-update-button").get_on_handler(
|
|
|
|
"click",
|
|
|
|
);
|
2022-11-17 23:33:43 +01:00
|
|
|
confirm_license_update_click_handler({preventDefault() {}});
|
2022-07-10 01:06:33 +02:00
|
|
|
assert.ok(create_ajax_request_called);
|
2020-12-23 17:08:27 +01:00
|
|
|
|
|
|
|
let confirm_license_modal_shown = false;
|
|
|
|
override(helpers, "is_valid_input", () => true);
|
|
|
|
$("#confirm-licenses-modal").modal = (action) => {
|
2021-06-03 16:42:03 +02:00
|
|
|
assert.equal(action, "show");
|
2020-12-23 17:08:27 +01:00
|
|
|
confirm_license_modal_shown = true;
|
|
|
|
};
|
|
|
|
$("#licensechange-input-section").data = (key) => {
|
2021-06-03 16:42:03 +02:00
|
|
|
assert.equal(key, "licenses");
|
2020-12-23 17:08:27 +01:00
|
|
|
return 20;
|
|
|
|
};
|
|
|
|
$("#new_licenses_input").val = () => 15;
|
2022-07-10 01:06:33 +02:00
|
|
|
create_ajax_request_called = false;
|
2020-12-23 17:08:27 +01:00
|
|
|
const update_licenses_button_click_handler =
|
|
|
|
$("#update-licenses-button").get_on_handler("click");
|
2022-11-17 23:33:43 +01:00
|
|
|
update_licenses_button_click_handler({preventDefault() {}});
|
2022-07-10 01:06:33 +02:00
|
|
|
assert.ok(create_ajax_request_called);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!confirm_license_modal_shown);
|
2020-12-23 17:08:27 +01:00
|
|
|
|
|
|
|
$("#new_licenses_input").val = () => 25;
|
2022-07-10 01:06:33 +02:00
|
|
|
create_ajax_request_called = false;
|
2022-11-17 23:33:43 +01:00
|
|
|
update_licenses_button_click_handler({preventDefault() {}});
|
2022-07-10 01:06:33 +02:00
|
|
|
assert.ok(!create_ajax_request_called);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(confirm_license_modal_shown);
|
2020-12-23 17:08:27 +01:00
|
|
|
|
|
|
|
override(helpers, "is_valid_input", () => false);
|
|
|
|
const event = {
|
2022-11-17 23:33:43 +01:00
|
|
|
/* istanbul ignore next */
|
|
|
|
preventDefault() {
|
2022-04-09 23:44:38 +02:00
|
|
|
throw new Error("unexpected preventDefault call");
|
2020-12-23 17:08:27 +01:00
|
|
|
},
|
|
|
|
};
|
|
|
|
update_licenses_button_click_handler(event);
|
|
|
|
|
|
|
|
const update_next_renewal_licenses_button_click_handler = $(
|
|
|
|
"#update-licenses-at-next-renewal-button",
|
|
|
|
).get_on_handler("click");
|
|
|
|
create_ajax_request_called = false;
|
|
|
|
function licenses_at_next_renewal_change_ajax(
|
|
|
|
url,
|
|
|
|
form_name,
|
|
|
|
ignored_inputs,
|
|
|
|
method,
|
2020-09-03 15:34:20 +02:00
|
|
|
success_callback,
|
2020-12-23 17:08:27 +01:00
|
|
|
) {
|
|
|
|
assert.equal(url, "/json/billing/plan");
|
|
|
|
assert.equal(form_name, "licensechange");
|
|
|
|
assert.deepEqual(ignored_inputs, ["licenses"]);
|
|
|
|
assert.equal(method, "PATCH");
|
2021-10-14 00:23:21 +02:00
|
|
|
location.replace = (new_location) => {
|
2023-03-23 04:28:36 +01:00
|
|
|
assert.equal(new_location, "/billing/");
|
2020-09-03 15:34:20 +02:00
|
|
|
};
|
|
|
|
success_callback();
|
2020-12-23 17:08:27 +01:00
|
|
|
create_ajax_request_called = true;
|
|
|
|
}
|
2022-07-09 23:25:05 +02:00
|
|
|
override(helpers, "create_ajax_request", licenses_at_next_renewal_change_ajax);
|
2022-11-17 23:33:43 +01:00
|
|
|
update_next_renewal_licenses_button_click_handler({preventDefault() {}});
|
2022-07-09 23:25:05 +02:00
|
|
|
assert.ok(create_ajax_request_called);
|
2019-01-08 18:06:06 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
run_test("billing_template", () => {
|
|
|
|
// Elements necessary for create_ajax_request
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(document.querySelector("#cardchange-error"));
|
|
|
|
assert.ok(document.querySelector("#cardchange-loading"));
|
|
|
|
assert.ok(document.querySelector("#cardchange_loading_indicator"));
|
|
|
|
assert.ok(document.querySelector("#cardchange-success"));
|
|
|
|
|
|
|
|
assert.ok(document.querySelector("#licensechange-error"));
|
|
|
|
assert.ok(document.querySelector("#licensechange-loading"));
|
|
|
|
assert.ok(document.querySelector("#licensechange_loading_indicator"));
|
|
|
|
assert.ok(document.querySelector("#licensechange-success"));
|
|
|
|
|
|
|
|
assert.ok(document.querySelector("#planchange-error"));
|
|
|
|
assert.ok(document.querySelector("#planchange-loading"));
|
|
|
|
assert.ok(document.querySelector("#planchange_loading_indicator"));
|
|
|
|
assert.ok(document.querySelector("#planchange-success"));
|
|
|
|
|
|
|
|
assert.ok(document.querySelector("input[name=csrfmiddlewaretoken]"));
|
2019-01-08 18:06:06 +01:00
|
|
|
});
|