From a46b2386b65ba231dcb2a440ac21f12e23634e59 Mon Sep 17 00:00:00 2001 From: Vishnu Ks Date: Wed, 27 Feb 2019 11:52:05 +0000 Subject: [PATCH] billing: Add node tests for helpers.js. --- .eslintrc.json | 2 + frontend_tests/node_tests/billing_helpers.js | 233 +++++++++++++++++++ frontend_tests/zjsunit/zjquery.js | 6 + 3 files changed, 241 insertions(+) diff --git a/.eslintrc.json b/.eslintrc.json index fd283e1bc9..a59acbc5b6 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -68,6 +68,7 @@ "hash_util": false, "hashchange": false, "helpers": false, + "history": false, "home_msg_list": false, "hotspots": false, "i18n": false, @@ -85,6 +86,7 @@ "localStorage": false, "local_message": false, "localstorage": false, + "location": false, "markdown": false, "marked": false, "md5": false, diff --git a/frontend_tests/node_tests/billing_helpers.js b/frontend_tests/node_tests/billing_helpers.js index 693db7a28a..c57c0f8182 100644 --- a/frontend_tests/node_tests/billing_helpers.js +++ b/frontend_tests/node_tests/billing_helpers.js @@ -1,4 +1,237 @@ +const { JSDOM } = require("jsdom"); +const fs = require("fs"); +const template = fs.readFileSync("templates/corporate/upgrade.html", "utf-8"); +const dom = new JSDOM(template, { pretendToBeVisual: true }); +var jquery = require('jquery')(dom.window); + set_global('$', global.make_zjquery()); set_global('page_params', {}); +set_global('loading', {}); +set_global('history', {}); +set_global('document', { + title: "Zulip", +}); +set_global('location', { + pathname: "/upgrade/", + search: "", + hash: "#billing", +}); zrequire('helpers', "js/billing/helpers"); + +run_test('create_ajax_request', () => { + var form_loading_indicator = "#autopay_loading_indicator"; + var form_input_section = "#autopay-input-section"; + var form_success = "#autopay-success"; + var form_error = "#autopay-error"; + var form_loading = "#autopay-loading"; + + var state = { + 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, + location_reload: 0, + pushState: 0, + make_indicator: 0, + }; + + loading.make_indicator = function (loading_indicator, config) { + assert.equal(loading_indicator.selector, form_loading_indicator); + assert.equal(config.text, "Processing ..."); + assert.equal(config.abs_positioned, true); + state.make_indicator += 1; + }; + + $(form_input_section).hide = function () { + state.form_input_section_hide += 1; + }; + + $(form_input_section).show = function () { + state.form_input_section_show += 1; + }; + + $(form_error).hide = function () { + state.form_error_hide += 1; + }; + + $(form_error).show = function () { + state.form_error_show += 1; + return { + text: (msg) => { + assert.equal(msg, "response_message"); + }, + }; + }; + + $(form_success).show = function () { + state.form_success_show += 1; + }; + + $(form_loading).show = function () { + state.form_loading_show += 1; + }; + + $(form_loading).hide = function () { + state.form_loading_hide += 1; + }; + + $("#autopay-form").serializeArray = () => { + return jquery("#autopay-form").serializeArray(); + }; + + $.post = ({url, data, success, error}) => { + assert.equal(state.form_input_section_hide, 1); + assert.equal(state.form_error_hide, 1); + assert.equal(state.form_loading_show, 1); + assert.equal(state.make_indicator, 1); + + assert.equal(url, "/json/billing/upgrade"); + + assert.equal(Object.keys(data).length, 8); + 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, '"annual"'); + assert.equal(data.license_management, '"automatic"'); + assert.equal(data.licenses, ''); + + 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; + }; + + 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); + + 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); + }; + + helpers.create_ajax_request("/json/billing/upgrade", "autopay", {id: "stripe_token_id"}); + +}); + +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", () => { + var prices = {}; + prices.annual = 8000; + prices.monthly = 800; + page_params.seat_count = 35; + + helpers.update_charged_amount(prices, "annual"); + assert.equal($("#charged_amount").text(), 80 * 35); + + helpers.update_charged_amount(prices, "monthly"); + assert.equal($("#charged_amount").text(), 8 * 35); +}); + +run_test("show_license_section", () => { + var state = { + 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); + assert.equal($("#automatic_license_count").prop('disabled'), false); + assert.equal($("#manual_license_count").prop('disabled'), true); + + 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); + assert.equal($("#automatic_license_count").prop('disabled'), true); + assert.equal($("#manual_license_count").prop('disabled'), false); +}); + +run_test("set_tab", () => { + var state = { + show_tab_billing: 0, + show_tab_payment_method: 0, + scrollTop: 0, + }; + + $('#upgrade-tabs.nav a[href="#billing"]').tab = (action) => { + state.show_tab_billing += 1; + assert.equal(action, "show"); + }; + + $('#upgrade-tabs.nav a[href="#payment-method"]').tab = (action) => { + state.show_tab_payment_method += 1; + assert.equal(action, "show"); + }; + + $("html").scrollTop = (val) => { + state.scrollTop += 1; + assert.equal(val, 0); + }; + + 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"); + + const hash_change_handler = window.onhashchange; + hash_change_handler(); + assert.equal(state.show_tab_payment_method, 1); + assert.equal(state.scrollTop, 2); +}); diff --git a/frontend_tests/zjsunit/zjquery.js b/frontend_tests/zjsunit/zjquery.js index 67efd76ac0..9e1efaf950 100644 --- a/frontend_tests/zjsunit/zjquery.js +++ b/frontend_tests/zjsunit/zjquery.js @@ -309,6 +309,9 @@ exports.make_new_elem = function (selector, opts) { replaceWith: function () { return self; }, + scrollTop: function () { + return self; + }, select: function (arg) { event_store.generic_event('select', arg); return self; @@ -320,6 +323,9 @@ exports.make_new_elem = function (selector, opts) { shown = true; return self; }, + serializeArray: function () { + return self; + }, set_parent: function (parent_elem) { my_parent = parent_elem; },