@virtuous/conductor
Version:
186 lines (154 loc) • 4.84 kB
JavaScript
;
var _index = _interopRequireDefault(require("./index"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
describe('Stack', function () {
beforeEach(function () {
_index["default"].constructor();
});
describe('add()', function () {
it('should correctly add an entry', function () {
var route = {
pathname: '/myroute'
};
_index["default"].add('123', route);
expect(_index["default"].getAll().size).toBe(1);
expect(_index["default"].get('123')).toEqual(route);
});
it('should not add when params are missing', function () {
_index["default"].add();
expect(_index["default"].getAll().size).toBe(0);
});
});
describe('first()', function () {
it('should correctly return the first entry', function () {
_index["default"].add('123', {
a: 1
});
_index["default"].add('456', {
a: 2
});
expect(_index["default"].first()).toEqual(['123', {
a: 1
}]);
});
it('should return null when stack is empty', function () {
expect(_index["default"].first()).toBeNull();
});
});
describe('get()', function () {
it('should correctly get an entry', function () {
var route = {
pathname: '/myroute'
};
_index["default"].add('123', route);
expect(_index["default"].get('123')).toEqual(route);
});
it('should return null when entry is not found', function () {
expect(_index["default"].get('invalid')).toBe(null);
});
});
describe('getByIndex()', function () {
it('should return null when no index is given', function () {
expect(_index["default"].getByIndex()).toBeNull();
});
});
describe('getAll()', function () {
it('should correctly return the complete map', function () {
_index["default"].add('123', {});
var entries = _index["default"].getAll();
expect(entries instanceof Map).toBeTruthy();
expect(entries.size).toBe(1);
});
});
describe('last()', function () {
it('should correctly return the last entry', function () {
_index["default"].add('123', {
a: 1
});
_index["default"].add('456', {
a: 2
});
expect(_index["default"].last()).toEqual(['456', {
a: 2
}]);
});
it('should return null when stack is empty', function () {
expect(_index["default"].last()).toBeNull();
});
});
describe('remove()', function () {
it('should correctly remove an entry', function () {
_index["default"].add('123', {});
_index["default"].add('456', {});
_index["default"].remove('123');
expect(_index["default"].get('123')).toBeNull();
expect(_index["default"].get('456')).toBeTruthy();
expect(_index["default"].getAll().size).toBe(1);
});
});
describe('remove()', function () {
it('should correctly reset the stack to the first entry', function () {
_index["default"].add('123', {});
_index["default"].add('456', {});
_index["default"].add('789', {});
_index["default"].reset();
expect(_index["default"].get('123')).toBeTruthy();
expect(_index["default"].get('456')).toBeFalsy();
expect(_index["default"].getAll().size).toBe(1);
});
it('should correctly reset to a given id and entry', function () {
_index["default"].add('123', {});
_index["default"].add('456', {});
_index["default"].reset(['789', {
a: 1
}]);
expect(_index["default"].get('123')).toBeFalsy();
expect(_index["default"].get('789')).toEqual({
a: 1
});
expect(_index["default"].getAll().size).toBe(1);
});
});
describe('update()', function () {
it('should correctly update an entry', function () {
_index["default"].add('123', {
a: 1
});
_index["default"].update('123', {
a: 2
});
expect(_index["default"].get('123')).toEqual({
a: 2
});
});
it('should not update when id is missing', function () {
_index["default"].add('123', {
a: 1
});
_index["default"].update();
expect(_index["default"].get('123')).toEqual({
a: 1
});
});
it('should not update when id is not inside stack', function () {
_index["default"].add('123', {
a: 1
});
_index["default"].update('456', {
a: 2
});
expect(_index["default"].get('123')).toEqual({
a: 1
});
});
});
describe('clear()', function () {
it('should remove all entries', function () {
_index["default"].add('123', {
a: 1
});
_index["default"].clear();
expect(_index["default"].getAll().size).toBe(0);
});
});
});