diff --git a/.eslintrc.json b/.eslintrc.json index 7f352ef371..2353192134 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -135,7 +135,6 @@ "buddy_data": false, "buddy_list": false, "click_handlers": false, - "color_data": false, "compose_actions": false, "compose_ui": false, "composebox_typeahead": false, diff --git a/frontend_tests/node_tests/color_data.js b/frontend_tests/node_tests/color_data.js index 40a6b9edbb..db58305b2b 100644 --- a/frontend_tests/node_tests/color_data.js +++ b/frontend_tests/node_tests/color_data.js @@ -8,7 +8,7 @@ const {run_test} = require("../zjsunit/test"); const color_data = zrequire("color_data"); run_test("pick_color", () => { - color_data.colors = ["blue", "orange", "red", "yellow"]; + color_data.__Rewire__("colors", ["blue", "orange", "red", "yellow"]); color_data.reset(); diff --git a/frontend_tests/node_tests/stream_data.js b/frontend_tests/node_tests/stream_data.js index 52037cc01b..651da9841e 100644 --- a/frontend_tests/node_tests/stream_data.js +++ b/frontend_tests/node_tests/stream_data.js @@ -653,7 +653,7 @@ run_test("create_sub", () => { color: "#76ce90", }; - color_data.pick_color = () => "#bd86e5"; + color_data.__Rewire__("pick_color", () => "#bd86e5"); const india_sub = stream_data.create_sub_from_server_data(india); assert(india_sub); diff --git a/frontend_tests/node_tests/stream_events.js b/frontend_tests/node_tests/stream_events.js index 511659433f..11049f676c 100644 --- a/frontend_tests/node_tests/stream_events.js +++ b/frontend_tests/node_tests/stream_events.js @@ -2,6 +2,8 @@ const {strict: assert} = require("assert"); +const rewiremock = require("rewiremock/node"); + const {set_global, zrequire} = require("../zjsunit/namespace"); const {make_stub} = require("../zjsunit/stub"); const {run_test} = require("../zjsunit/test"); @@ -13,7 +15,8 @@ const _settings_notifications = { }; set_global("settings_notifications", _settings_notifications); -const color_data = set_global("color_data", {}); +const color_data = {__esModule: true}; +rewiremock("../../static/js/color_data").with(color_data); set_global("current_msg_list", {}); const message_util = set_global("message_util", {}); const stream_color = set_global("stream_color", {}); @@ -35,6 +38,8 @@ set_global("message_list", { subs = set_global("subs", {update_settings_for_subscribed: noop}); set_global("overlays", {streams_open: () => true}); +rewiremock.enable(); + const peer_data = zrequire("peer_data"); const people = zrequire("people"); const stream_data = zrequire("stream_data"); @@ -397,3 +402,4 @@ run_test("remove_deactivated_user_from_all_streams", () => { // verify that we issue a call to update subscriber count/list UI assert.equal(subs_stub.num_calls, 1); }); +rewiremock.disable(); diff --git a/frontend_tests/node_tests/ui_init.js b/frontend_tests/node_tests/ui_init.js index 4feed0b72d..ebea603c7d 100644 --- a/frontend_tests/node_tests/ui_init.js +++ b/frontend_tests/node_tests/ui_init.js @@ -110,7 +110,6 @@ const util = zrequire("util"); zrequire("hash_util"); zrequire("stream_color"); zrequire("stream_edit"); -zrequire("color_data"); zrequire("stream_data"); zrequire("condense"); zrequire("lightbox"); diff --git a/static/js/archive.js b/static/js/archive.js index 947dd2fe2f..85c20ea468 100644 --- a/static/js/archive.js +++ b/static/js/archive.js @@ -2,6 +2,8 @@ import _ from "lodash"; import render_archive_message_group from "../templates/archive_message_group.hbs"; +import * as color_data from "./color_data"; + const {format, isSameDay} = require("date-fns"); function should_separate_into_groups(current_msg_time, next_msg_time) { diff --git a/static/js/bundles/app.js b/static/js/bundles/app.js index 053a65c7d5..9fceb292c0 100644 --- a/static/js/bundles/app.js +++ b/static/js/bundles/app.js @@ -25,7 +25,6 @@ import "../message_list"; import "../narrow"; import "../reload"; import "../compose_actions"; -import "../color_data"; import "../stream_color"; import "../stream_data"; import "../stream_topic_history"; diff --git a/static/js/color_data.js b/static/js/color_data.js index a0fa34125d..5ecc99d913 100644 --- a/static/js/color_data.js +++ b/static/js/color_data.js @@ -1,6 +1,6 @@ -"use strict"; +import _ from "lodash"; -const _ = require("lodash"); +export let unused_colors; // These colors are used now for streams. const stream_colors = [ "#76ce90", @@ -31,41 +31,39 @@ const stream_colors = [ // Shuffle our colors on page load to prevent // bias toward "early" colors. -exports.colors = _.shuffle(stream_colors); +export const colors = _.shuffle(stream_colors); -exports.reset = function () { - exports.unused_colors = exports.colors.slice(); -}; +export function reset() { + unused_colors = colors.slice(); +} -exports.reset(); +reset(); -exports.claim_color = function (color) { - const i = exports.unused_colors.indexOf(color); +export function claim_color(color) { + const i = unused_colors.indexOf(color); if (i < 0) { return; } - exports.unused_colors.splice(i, 1); + unused_colors.splice(i, 1); - if (exports.unused_colors.length === 0) { - exports.reset(); + if (unused_colors.length === 0) { + reset(); } -}; +} -exports.claim_colors = function (subs) { +export function claim_colors(subs) { const colors = new Set(subs.map((sub) => sub.color)); for (const color of colors) { - exports.claim_color(color); + claim_color(color); } -}; +} -exports.pick_color = function () { - const color = exports.unused_colors[0]; +export function pick_color() { + const color = unused_colors[0]; - exports.claim_color(color); + claim_color(color); return color; -}; - -window.color_data = exports; +} diff --git a/static/js/global.d.ts b/static/js/global.d.ts index ae321a903a..5254c14942 100644 --- a/static/js/global.d.ts +++ b/static/js/global.d.ts @@ -13,7 +13,6 @@ declare let bot_data: any; declare let buddy_data: any; declare let buddy_list: any; declare let click_handlers: any; -declare let color_data: any; declare let compose_actions: any; declare let composebox_typeahead: any; declare let compose_ui: any; diff --git a/static/js/stream_data.js b/static/js/stream_data.js index 98ef48b97e..a742f5ccb6 100644 --- a/static/js/stream_data.js +++ b/static/js/stream_data.js @@ -1,5 +1,6 @@ "use strict"; +const color_data = require("./color_data"); const {FoldDict} = require("./fold_dict"); const peer_data = require("./peer_data"); const people = require("./people"); diff --git a/static/js/stream_events.js b/static/js/stream_events.js index 6db9f7e882..0880f9d6ba 100644 --- a/static/js/stream_events.js +++ b/static/js/stream_events.js @@ -1,5 +1,6 @@ "use strict"; +const color_data = require("./color_data"); const narrow_state = require("./narrow_state"); const peer_data = require("./peer_data");