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.
|
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const {zrequire} = require("./lib/namespace");
|
|
|
|
const {run_test} = require("./lib/test");
|
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");
|
|
|
|
|
|
|
|
// Let's start with testing a function from util.js.
|
|
|
|
//
|
|
|
|
// The most basic unit tests load up code, call functions,
|
|
|
|
// and assert truths:
|
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!util.find_wildcard_mentions("boring text"));
|
|
|
|
assert.ok(util.find_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
|
|
|
|
|
|
|
|
const isaac = {
|
|
|
|
email: "isaac@example.com",
|
|
|
|
user_id: 30,
|
|
|
|
full_name: "Isaac Newton",
|
|
|
|
};
|
|
|
|
|
|
|
|
// The `people`object is a very fundamental object in the
|
|
|
|
// 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.)
|
|
|
|
|
|
|
|
const denmark_stream = {
|
|
|
|
color: "blue",
|
|
|
|
name: "Denmark",
|
|
|
|
stream_id: 101,
|
|
|
|
subscribed: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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");
|
|
|
|
assert.equal(sub.color, "blue");
|
|
|
|
});
|
2023-02-22 23:04:10 +01:00
|
|
|
// See example2.test.js in this directory.
|