BusyBird

 view release on metacpan or  search on metacpan

xt/js/status_container.html  view on Meta::CPAN

            error: null, count: 5
        }));
        return ack_response_promise;
    }).then(function(response_data) {
        deepEqual(response_data, {error: null, count: 5}, "ack response object OK");
        query_deferred = Q.defer();
        ack_response_promise = con._ackStatuses(acked_statuses, false);
        return query_deferred.promise;
    }).then(function(req) {
        var request_object = JSON.parse(req.requestBody);
        deepEqual(request_object, {ids: ["0","1","2","3","4"]}, "ack request object OK (without max_id)");
        req.respond(200, {"Content-Type": "application/json"}, JSON.stringify({
            error: null, count: 5
        }));
        return ack_response_promise;
    }).then(function(response_data) {
        deepEqual(response_data, {error: null, count: 5}, "ack response object OK");
    }).fail(function(reason) {
        ok(false, "should not fail");
        ok(true, "reason: " + reason);
    }).then(function() {
        fakexhr.restore();
        start();
    });
});

asyncTest("listenOnThresholdLevelChanged", function() {
    var con = setupContainer();
    var event_deferred = Q.defer();
    con.listenOnThresholdLevelChanged(function(new_threshold) {
        if(!event_deferred.promise.isPending()) {
            throw "unexpected event";
        }
        event_deferred.resolve(new_threshold);
    });
    Q.fcall(function() {
        con.setThresholdLevel(0);
        con.setThresholdLevel(1);
        return event_deferred.promise;
    }).then(function(new_threshold) {
        is(new_threshold, 1, "new threshold is 1");
        event_deferred = Q.defer();
        con.setThresholdLevel(1);
        con.setThresholdLevel(1);
        con.setThresholdLevel(-10);
        return event_deferred.promise;
    }).then(function(new_threshold) {
        is(new_threshold, -10, "new threshold is -10");
    }).fail(function(reason) {
        ok(false, "should not fail");
        ok(true, "reason: " + reason);
    }).then(function() {
        start();
    });
});

(function() {
    var con;
    var old_animation_duration;
    var getCursorId = function() {
        var $cursor_status = $("#bb-status-container").find(".bb-status-cursor");
        if($cursor_status.size() === 0) return null;
        return $cursor_status.find(".bb-status-id").text();
    };
    var setCursorToIndex = function(index) {
        var next_cursor = $("#bb-status-container").find(".bb-status").get(index);
        con.setCursor(next_cursor);
    };
    module("move cursor as threshold level changes", {
        setup: function() {
            stop();
            con = setupContainer();
            old_animation_duration = bb.StatusContainer.ANIMATE_STATUS_DURATION;
            bb.StatusContainer.ANIMATE_STATUS_DURATION = 10;
            appendStatusesFromMeta(con, [
                {id: 0, level: 1},
                {id: 1, level: 0},
                {id: 2, level: 2},
                {id: 3, level: 1},
                {id: 4, level: 0},
                {id: 5, level: 0},
                {id: 6, level: 1},
                {id: 7, level: 2}
            ]).then(function() { start() });
        },
        teardown: function() {
            bb.StatusContainer.ANIMATE_STATUS_DURATION = old_animation_duration;
        },
    });
    asyncTest("initially, cursor is at the top", function() {
        con.setThresholdLevel(0).then(function() {
            is(getCursorId(), "0", "cursor is at the top");
        }, function() { ok(false, "should not fail") }).then(function() {
            start();
        });
    });
    asyncTest("cursor stays when visible", function() {
        setCursorToIndex(6);
        con.setThresholdLevel(1).then(function() {
            is(getCursorId(), "6", "cursor stays at ID = 6");
        }, function() { ok(false, "should not fail") }).then(function() {
            start();
        });
    });
    asyncTest("cursor moves when the status gets invisible (upward)", function() {
        setCursorToIndex(4);
        con.setThresholdLevel(2).then(function() {
            is(getCursorId(), "2", "cursor moves ID 4 -> 2");
        }, function() { ok(false, "should not fail") }).then(function() {
            start();
        });
    });
    asyncTest("cursor moves when the status gets invisible (downward)", function() {
        setCursorToIndex(5);
        con.setThresholdLevel(2).then(function() {
            is(getCursorId(), "7", "cursor moves ID 5 -> 7");
        }, function() { ok(false, "should not fail") }).then(function() {
            start();
        });
    });
    asyncTest("cursor stays when all statuses get invisible", function() {
        setCursorToIndex(4);
        con.setThresholdLevel(100).then(function() {
            is(getCursorId(), "4", "cursor stays at ID = 4");
        }, function() { ok(false, "should not fail") }).then(function() {
            start();
        });
    });
    asyncTest("cursor moves downward when top statuses get invisible", function() {
        setCursorToIndex(0);
        con.setThresholdLevel(2).then(function() {
            is(getCursorId(), "2", "cursor moves ID 0 -> 2");
        }, function() { ok(false, "should not fail") }).then(function() {
            start();
        });
    });
})();

module("StatusesSummary");
test("StatusesSummary", function() {
    var summary;
    var next_id = 0;
    var st = function(level, username) {
        var status_html = createStatusHTMLDetail({
            id: next_id,
            busybird: { level: level  },
            user: { screen_name: username }
        });
        next_id++;
        return $(status_html).get(0);
    };
    var getSummary = function(summary_selector) {
        var result = [];
        var $level_entries = $(summary_selector).children(".bb-summary-level-entry");
        $level_entries.each(function() {
            var $level_entry = $(this);
            var level_result = {
                level: $level_entry.find(".bb-summary-level").text(),
                count_above: $level_entry.find(".bb-summary-count-above-level").text(),
                count_this:  null,
                per_user: [],
            };
            var $this_level_elem = $level_entry.find(".bb-summary-count-this-level");
            if($this_level_elem.size() > 0) {
                level_result.count_this = $this_level_elem.text();
            }
            $level_entry.find(".bb-summary-count-per-user-entry").each(function() {
                var username = $(this).find(".bb-summary-count-username").text();
                var count = $(this).find(".bb-summary-count-per-user").text();
                level_result.per_user.push({"name": username, "count": count});
            });
            result.push(level_result);
        });
        return result;
    };
    var statuses = [
        st(0,  "a"),
        st(0,  "a"),
        st(0,  "b"),
        st(-1, "a"),
        st(3,  "a"),
        st(3,  "c"),
        st(3,  "c"),
        st(-1, "a"),
        st(2,  "d")
    ];
    is($(statuses).filter(".bb-status").size(), 9, "9 statuses generated");
    $("#qunit-fixture").append('<div id="bb-status-summary"></div>');
    summary = new bb.StatusesSummary({selectorContainer: "#bb-status-summary"});
    summary.showSummaryOf($(statuses));
    deepEqual(getSummary("#bb-status-summary"), [
        {level: "3",  count_this: null, count_above: "3", per_user: [{"name": "c", "count": "2"}, {"name": "a", "count": "1"}]},



( run in 1.287 second using v1.01-cache-2.11-cpan-39bf76dae61 )