2021-03-15 13:03:00 +01:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
// This is a general tour of how to write node tests that
|
|
|
|
// may also give you some quick insight on how the Zulip
|
|
|
|
// browser app is constructed.
|
|
|
|
|
|
|
|
// The statements below are pretty typical for most node
|
|
|
|
// tests. The reason we need these helpers will hopefully
|
|
|
|
// become clear as you keep reading.
|
2024-10-09 00:25:41 +02:00
|
|
|
const assert = require("node:assert/strict");
|
2021-03-15 13:03:00 +01:00
|
|
|
|
2024-11-12 03:59:37 +01:00
|
|
|
const {make_stream} = require("./lib/example_stream.js");
|
|
|
|
const {make_user} = require("./lib/example_user.js");
|
|
|
|
const {zrequire} = require("./lib/namespace.js");
|
|
|
|
const {run_test} = require("./lib/test.js");
|
2021-03-15 13:03:00 +01:00
|
|
|
|
|
|
|
// We will use our special zrequire helper to import the
|
|
|
|
// Zulip code. We use zrequire instead of require,
|
|
|
|
// because it has some magic to clear state when we move
|
|
|
|
// on to the next test.
|
|
|
|
const people = zrequire("people");
|
|
|
|
const stream_data = zrequire("stream_data");
|
|
|
|
const util = zrequire("util");
|
|
|
|
|
2023-03-11 08:13:37 +01:00
|
|
|
// Let's start with testing a function from util.ts.
|
2021-03-15 13:03:00 +01:00
|
|
|
//
|
|
|
|
// The most basic unit tests load up code, call functions,
|
|
|
|
// and assert truths:
|
|
|
|
|
2023-11-21 07:04:19 +01:00
|
|
|
assert.ok(!util.find_stream_wildcard_mentions("boring text"));
|
|
|
|
assert.ok(util.find_stream_wildcard_mentions("mention @**everyone**"));
|
2021-03-15 13:03:00 +01:00
|
|
|
|
|
|
|
// Let's test with people.js next. We'll show this technique:
|
|
|
|
// * get a false value
|
|
|
|
// * change the data
|
|
|
|
// * get a true value
|
|
|
|
|
2024-08-02 19:50:39 +02:00
|
|
|
const isaac = make_user({
|
2021-03-15 13:03:00 +01:00
|
|
|
email: "isaac@example.com",
|
|
|
|
user_id: 30,
|
|
|
|
full_name: "Isaac Newton",
|
2024-08-02 19:50:39 +02:00
|
|
|
});
|
2021-03-15 13:03:00 +01:00
|
|
|
|
2023-12-14 08:28:24 +01:00
|
|
|
// The `people` object is a very fundamental object in the
|
2021-03-15 13:03:00 +01:00
|
|
|
// Zulip app. You can learn a lot more about it by reading
|
2023-02-22 23:04:10 +01:00
|
|
|
// the tests in people.test.js in the same directory as this file.
|
2021-03-15 13:03:00 +01:00
|
|
|
|
|
|
|
// Let's exercise the code and use assert to verify it works!
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!people.is_known_user_id(isaac.user_id));
|
2021-03-15 13:03:00 +01:00
|
|
|
people.add_active_user(isaac);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(people.is_known_user_id(isaac.user_id));
|
2021-03-15 13:03:00 +01:00
|
|
|
|
|
|
|
// Let's look at stream_data next, and we will start by putting
|
|
|
|
// some data at module scope. (You could also declare this inside
|
|
|
|
// the test, if you prefer.)
|
|
|
|
|
2024-07-11 13:54:09 +02:00
|
|
|
// We use make_stream to create a complete stream object with select
|
|
|
|
// fields explicitly specified, and all other fields populated with
|
|
|
|
// reasonable defaults.
|
|
|
|
const denmark_stream = make_stream({
|
|
|
|
color: "a1a1a1",
|
2021-03-15 13:03:00 +01:00
|
|
|
name: "Denmark",
|
|
|
|
subscribed: false,
|
2024-07-11 13:54:09 +02:00
|
|
|
});
|
2021-03-15 13:03:00 +01:00
|
|
|
|
|
|
|
// We introduce the run_test helper, which mostly just causes
|
|
|
|
// a line of output to go to the console. It does a little more than
|
|
|
|
// that, which we will see later.
|
|
|
|
|
|
|
|
run_test("verify stream_data persists stream color", () => {
|
|
|
|
stream_data.clear_subscriptions();
|
|
|
|
assert.equal(stream_data.get_sub_by_name("Denmark"), undefined);
|
|
|
|
stream_data.add_sub(denmark_stream);
|
|
|
|
const sub = stream_data.get_sub_by_name("Denmark");
|
2024-07-11 13:54:09 +02:00
|
|
|
assert.equal(sub.color, "a1a1a1");
|
2021-03-15 13:03:00 +01:00
|
|
|
});
|
2023-02-22 23:04:10 +01:00
|
|
|
// See example2.test.js in this directory.
|