behave-history
Version:
A better browser history manager
295 lines (234 loc) • 7.91 kB
JavaScript
;
var _this = this;
var _interopRequire = function (obj) {
return obj && (obj["default"] || obj);
};
var BehaveHistory = _interopRequire(require("../../src/index"));
var sinon = _interopRequire(require("sinon"));
describe("BehaveHistory", function () {
beforeEach(function () {
_this.dispatcher = {
register: sinon.spy(),
dispatch: sinon.spy()
};
_this.history = new BehaveHistory({
dispatcher: _this.dispatcher
});
});
describe(".start()", function () {
it("should be defined", function (done) {
expect(_this.history.start).toBeDefined();
done();
});
it("should set the `_isStarted` flag to `true`", function (done) {
_this.history.start();
expect(_this.history._started).toEqual(true);
done();
});
it("should add listener to window for hashchange/popstate events", function (done) {
sinon.spy(window, "addEventListener");
_this.history.start();
var call = window.addEventListener.getCall(0);
var args = call.args.slice();
var evtType = window.history.pushState ? "popstate" : "hashchange";
expect(args[0]).toEqual(evtType);
window.addEventListener.restore();
done();
});
});
describe(".stop()", function () {
it("should be defined", function (done) {
expect(_this.history.stop).toBeDefined();
done();
});
it("should set the `_isStarted` flag to `false`", function (done) {
_this.history._started = true;
_this.history.stop();
expect(_this.history._started).toEqual(false);
done();
});
it("should remove listener on window for hashchange/popstate events", function (done) {
sinon.spy(window, "removeEventListener");
_this.history.stop();
var call = window.removeEventListener.getCall(0);
var args = call.args.slice();
var evtType = window.history.pushState ? "popstate" : "hashchange";
expect(args[0]).toEqual(evtType);
done();
});
});
describe("._update(evt)", function () {
beforeEach(function () {
spyOn(_this.history, "_pushState");
spyOn(_this.history, "_updateHash");
});
it("should be defined", function (done) {
expect(_this.history._update).toBeDefined();
done();
});
it("should exit out if `evt.type` is not `this._eventType`", function (done) {
var evt = {
type: "INCORRECT_TYPE",
route: "some/url",
data: { test: "data" },
options: {}
};
_this.history._update(evt);
expect(_this.history._pushState.calls.count()).toEqual(0);
expect(_this.history._updateHash.calls.count()).toEqual(0);
done();
});
it("should exit out if `evt.route` matches result of `this._getFragment()`", function (done) {
var evt = {
type: "ROUTE",
route: "some/url",
data: { test: "data" },
options: {}
};
spyOn(_this.history, "_getFragment").and.callFake(function () {
return "some/url";
});
_this.history._update(evt);
expect(_this.history._pushState.calls.count()).toEqual(0);
expect(_this.history._updateHash.calls.count()).toEqual(0);
done();
});
});
describe("._pushState(data, route, replace)", function () {
beforeEach(function () {
spyOn(window.history, "pushState");
});
it("should be defined", function (done) {
expect(_this.history._pushState).toBeDefined();
done();
});
it("should update url to match specified route", function (done) {
var evt = {
type: "ROUTE",
route: "some/url",
data: { test: "data" },
options: {}
};
if (!window.history.pushState) done();
_this.history._update(evt);
expect(window.history.pushState).toHaveBeenCalledWith(evt.data, document.title, window.location.protocol + "//" + window.location.host + "/" + evt.route);
window.history.pushState.calls.reset();
done();
});
it("should send any data in event to window state if in supporting browsers", function (done) {
var evt = {
type: "ROUTE",
route: "some/url",
data: { test: "data" },
options: {}
};
if (!window.history.pushState) done();
_this.history._update(evt);
expect(window.history.pushState).toHaveBeenCalledWith(evt.data, document.title, window.location.protocol + "//" + window.location.host + "/" + evt.route);
done();
});
});
describe("._updateHash(route, replace)", function () {
beforeEach(function () {
_this.history._wantsHashChange = true;
});
afterEach(function () {
_this.history._wantsHashChange = false;
});
it("should be defined", function (done) {
expect(_this.history._updateHash).toBeDefined();
done();
});
it("should update url to match specified route", function (done) {
var evt = {
type: "ROUTE",
route: "some/url",
data: { test: "data" },
options: {}
};
_this.history._location = { href: "", hash: "" };
_this.history._update(evt);
expect(_this.history._location.hash).toBe("#/" + evt.route);
done();
});
});
describe("._getFragment(fragment)", function () {
it("should be defined", function (done) {
expect(_this.history._getFragment).toBeDefined();
done();
});
it("should get url fragment if one is not provided in params", function (done) {
spyOn(_this.history, "_getPath").and.callFake(function () {
return "/";
});
_this.history._isPushState = true;
_this.history._getFragment();
expect(_this.history._getPath).toHaveBeenCalled();
done();
});
it("should clean up the fragment (remove whitespace and #'s)", function (done) {
expect(_this.history._getFragment("#some/path ")).toEqual("some/path");
done();
});
});
describe("._getHash()", function () {
it("should be defined", function (done) {
expect(_this.history._getHash).toBeDefined();
done();
});
it("should get url fragment", function (done) {
_this.history._location = { href: "/#/some/path" };
expect(_this.history._getHash()).toEqual("/some/path");
done();
});
});
describe("._getPath()", function () {
beforeEach(function () {
_this.history._location = { pathname: "/some/path" };
spyOn(_this.history, "_getSearch").and.callFake(function () {
return "";
});
});
it("should be defined", function (done) {
expect(_this.history._getPath).toBeDefined();
done();
});
it("should get url fragment", function (done) {
expect(_this.history._getPath()).toEqual("some/path");
done();
});
});
describe("._getSearch()", function () {
beforeEach(function () {
_this.history._location = { href: "/some/path?some=param" };
});
it("should be defined", function (done) {
expect(_this.history._getSearch).toBeDefined();
done();
});
it("should get url fragment", function (done) {
expect(_this.history._getSearch()).toEqual("?some=param");
done();
});
});
describe("._handleHistoryEvent(e)", function () {
beforeEach(function () {
spyOn(_this.history, "_getFragment").and.callFake(function () {
return "some/url";
});
});
it("should be defined", function (done) {
expect(_this.history._handleHistoryEvent).toBeDefined();
done();
});
it("should call it's dispatcher's `dispatch` event, passing on any state if any", function (done) {
var e = {
type: "popstate",
state: { example: "state" }
};
_this.history._handleHistoryEvent(e);
expect(_this.history._dispatcher.dispatch.called).toBe(true);
done();
});
});
});