mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 14:03:30 +00:00 
			
		
		
		
	I remove an obsolete comment--we use get_streams()
for the `n` key now.
I also remove a guard statement from sort_groups()
that returned `undefined` for empty lists.
That guard statement would break this code:
    const stream_groups = stream_sort.sort_groups(streams, get_search_term());
    if (stream_groups.same_as_before && ...
The calling code prevents the situation anyway:
    const streams = stream_data.subscribed_stream_ids();
    if (streams.length === 0) {
        return;
    }
I modify the "no_subscribed_streams" test to test
the new behavior.  (Even though stream_list currently
short-circuits the call here, that may change in the future.)
I also introduce the test() wrapper to explicitly clear
our data.
		
	
		
			
				
	
	
		
			117 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			117 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
"use strict";
 | 
						|
 | 
						|
const {strict: assert} = require("assert");
 | 
						|
 | 
						|
const {zrequire} = require("../zjsunit/namespace");
 | 
						|
const {run_test} = require("../zjsunit/test");
 | 
						|
 | 
						|
const stream_data = zrequire("stream_data");
 | 
						|
const stream_sort = zrequire("stream_sort");
 | 
						|
 | 
						|
const scalene = {
 | 
						|
    subscribed: true,
 | 
						|
    name: "scalene",
 | 
						|
    stream_id: 1,
 | 
						|
    pin_to_top: true,
 | 
						|
};
 | 
						|
const fast_tortoise = {
 | 
						|
    subscribed: true,
 | 
						|
    name: "fast tortoise",
 | 
						|
    stream_id: 2,
 | 
						|
    pin_to_top: false,
 | 
						|
};
 | 
						|
const pneumonia = {
 | 
						|
    subscribed: true,
 | 
						|
    name: "pneumonia",
 | 
						|
    stream_id: 3,
 | 
						|
    pin_to_top: false,
 | 
						|
};
 | 
						|
const clarinet = {
 | 
						|
    subscribed: true,
 | 
						|
    name: "clarinet",
 | 
						|
    stream_id: 4,
 | 
						|
    pin_to_top: false,
 | 
						|
};
 | 
						|
const weaving = {
 | 
						|
    subscribed: false,
 | 
						|
    name: "weaving",
 | 
						|
    stream_id: 5,
 | 
						|
    pin_to_top: false,
 | 
						|
};
 | 
						|
 | 
						|
function sort_groups(query) {
 | 
						|
    const streams = stream_data.subscribed_stream_ids();
 | 
						|
    return stream_sort.sort_groups(streams, query);
 | 
						|
}
 | 
						|
 | 
						|
function test(label, f) {
 | 
						|
    run_test(label, (override) => {
 | 
						|
        stream_data.clear_subscriptions();
 | 
						|
        f(override);
 | 
						|
    });
 | 
						|
}
 | 
						|
 | 
						|
test("no_subscribed_streams", () => {
 | 
						|
    const sorted = sort_groups("");
 | 
						|
    assert.deepEqual(sorted, {
 | 
						|
        dormant_streams: [],
 | 
						|
        normal_streams: [],
 | 
						|
        pinned_streams: [],
 | 
						|
        same_as_before: sorted.same_as_before,
 | 
						|
    });
 | 
						|
    assert.equal(stream_sort.first_stream_id(), undefined);
 | 
						|
});
 | 
						|
 | 
						|
test("basics", (override) => {
 | 
						|
    stream_data.add_sub(scalene);
 | 
						|
    stream_data.add_sub(fast_tortoise);
 | 
						|
    stream_data.add_sub(pneumonia);
 | 
						|
    stream_data.add_sub(clarinet);
 | 
						|
    stream_data.add_sub(weaving);
 | 
						|
 | 
						|
    override(stream_data, "is_active", (sub) => sub.name !== "pneumonia");
 | 
						|
 | 
						|
    // Test sorting into categories/alphabetized
 | 
						|
    let sorted = sort_groups("");
 | 
						|
    assert.deepEqual(sorted.pinned_streams, [scalene.stream_id]);
 | 
						|
    assert.deepEqual(sorted.normal_streams, [clarinet.stream_id, fast_tortoise.stream_id]);
 | 
						|
    assert.deepEqual(sorted.dormant_streams, [pneumonia.stream_id]);
 | 
						|
 | 
						|
    // Test cursor helpers.
 | 
						|
    assert.equal(stream_sort.first_stream_id(), scalene.stream_id);
 | 
						|
 | 
						|
    assert.equal(stream_sort.prev_stream_id(scalene.stream_id), undefined);
 | 
						|
    assert.equal(stream_sort.prev_stream_id(clarinet.stream_id), scalene.stream_id);
 | 
						|
 | 
						|
    assert.equal(stream_sort.next_stream_id(fast_tortoise.stream_id), pneumonia.stream_id);
 | 
						|
    assert.equal(stream_sort.next_stream_id(pneumonia.stream_id), undefined);
 | 
						|
 | 
						|
    // Test filtering
 | 
						|
    sorted = sort_groups("s");
 | 
						|
    assert.deepEqual(sorted.pinned_streams, [scalene.stream_id]);
 | 
						|
    assert.deepEqual(sorted.normal_streams, []);
 | 
						|
    assert.deepEqual(sorted.dormant_streams, []);
 | 
						|
 | 
						|
    assert.equal(stream_sort.prev_stream_id(clarinet.stream_id), undefined);
 | 
						|
 | 
						|
    assert.equal(stream_sort.next_stream_id(clarinet.stream_id), undefined);
 | 
						|
 | 
						|
    // Test searching entire word, case-insensitive
 | 
						|
    sorted = sort_groups("PnEuMoNiA");
 | 
						|
    assert.deepEqual(sorted.pinned_streams, []);
 | 
						|
    assert.deepEqual(sorted.normal_streams, []);
 | 
						|
    assert.deepEqual(sorted.dormant_streams, [pneumonia.stream_id]);
 | 
						|
 | 
						|
    // Test searching part of word
 | 
						|
    sorted = sort_groups("tortoise");
 | 
						|
    assert.deepEqual(sorted.pinned_streams, []);
 | 
						|
    assert.deepEqual(sorted.normal_streams, [fast_tortoise.stream_id]);
 | 
						|
    assert.deepEqual(sorted.dormant_streams, []);
 | 
						|
 | 
						|
    // Test searching stream with spaces
 | 
						|
    sorted = sort_groups("fast t");
 | 
						|
    assert.deepEqual(sorted.pinned_streams, []);
 | 
						|
    assert.deepEqual(sorted.normal_streams, [fast_tortoise.stream_id]);
 | 
						|
    assert.deepEqual(sorted.dormant_streams, []);
 | 
						|
});
 |