2017-05-05 17:59:41 +00:00
|
|
|
import AccessorUtilities from '~/lib/utils/accessor';
|
2018-01-10 18:56:15 +00:00
|
|
|
import SigninTabsMemoizer from '~/pages/sessions/new/signin_tabs_memoizer';
|
2017-05-05 17:59:41 +00:00
|
|
|
|
2017-07-06 18:58:47 +00:00
|
|
|
(() => {
|
2016-11-06 18:41:23 +00:00
|
|
|
describe('SigninTabsMemoizer', () => {
|
2016-12-30 21:27:40 +00:00
|
|
|
const fixtureTemplate = 'static/signin_tabs.html.raw';
|
2018-01-07 15:09:42 +00:00
|
|
|
const tabSelector = 'ul.new-session-tabs';
|
2016-11-06 18:41:23 +00:00
|
|
|
const currentTabKey = 'current_signin_tab';
|
|
|
|
let memo;
|
|
|
|
|
|
|
|
function createMemoizer() {
|
2017-07-06 18:58:47 +00:00
|
|
|
memo = new SigninTabsMemoizer({
|
2016-11-06 18:41:23 +00:00
|
|
|
currentTabKey,
|
|
|
|
tabSelector,
|
|
|
|
});
|
|
|
|
return memo;
|
|
|
|
}
|
|
|
|
|
2016-12-31 00:14:33 +00:00
|
|
|
preloadFixtures(fixtureTemplate);
|
2016-11-06 18:41:23 +00:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2016-12-31 00:14:33 +00:00
|
|
|
loadFixtures(fixtureTemplate);
|
2017-05-05 17:59:41 +00:00
|
|
|
|
|
|
|
spyOn(AccessorUtilities, 'isLocalStorageAccessSafe').and.returnValue(true);
|
2016-11-06 18:41:23 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('does nothing if no tab was previously selected', () => {
|
|
|
|
createMemoizer();
|
|
|
|
|
2018-01-07 15:09:42 +00:00
|
|
|
expect(document.querySelector(`${tabSelector} > li.active a`).getAttribute('href')).toEqual('#ldap');
|
2016-11-06 18:41:23 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('shows last selected tab on boot', () => {
|
|
|
|
createMemoizer().saveData('#ldap');
|
|
|
|
const fakeTab = {
|
|
|
|
click: () => {},
|
|
|
|
};
|
|
|
|
spyOn(document, 'querySelector').and.returnValue(fakeTab);
|
|
|
|
spyOn(fakeTab, 'click');
|
|
|
|
|
|
|
|
memo.bootstrap();
|
|
|
|
|
|
|
|
// verify that triggers click on the last selected tab
|
|
|
|
expect(document.querySelector).toHaveBeenCalledWith(`${tabSelector} a[href="#ldap"]`);
|
|
|
|
expect(fakeTab.click).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2018-06-20 21:18:45 +00:00
|
|
|
it('clicks the first tab if value in local storage is bad', () => {
|
|
|
|
createMemoizer().saveData('#bogus');
|
|
|
|
const fakeTab = {
|
|
|
|
click: () => {},
|
|
|
|
};
|
|
|
|
spyOn(document, 'querySelector').and.callFake(selector => (selector === `${tabSelector} a[href="#bogus"]` ? null : fakeTab));
|
|
|
|
spyOn(fakeTab, 'click');
|
|
|
|
|
|
|
|
memo.bootstrap();
|
|
|
|
|
|
|
|
// verify that triggers click on stored selector and fallback
|
|
|
|
expect(document.querySelector.calls.allArgs()).toEqual([['ul.new-session-tabs a[href="#bogus"]'], ['ul.new-session-tabs a']]);
|
|
|
|
expect(fakeTab.click).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2016-11-06 18:41:23 +00:00
|
|
|
it('saves last selected tab on change', () => {
|
|
|
|
createMemoizer();
|
|
|
|
|
2018-01-07 15:09:42 +00:00
|
|
|
document.querySelector('a[href="#login-pane"]').click();
|
2016-11-06 18:41:23 +00:00
|
|
|
|
2018-01-07 15:09:42 +00:00
|
|
|
expect(memo.readData()).toEqual('#login-pane');
|
2016-11-06 18:41:23 +00:00
|
|
|
});
|
2017-05-05 17:59:41 +00:00
|
|
|
|
2018-01-01 22:43:16 +00:00
|
|
|
it('overrides last selected tab with hash tag when given', () => {
|
|
|
|
window.location.hash = '#ldap';
|
|
|
|
createMemoizer();
|
|
|
|
|
|
|
|
expect(memo.readData()).toEqual('#ldap');
|
|
|
|
});
|
|
|
|
|
2017-05-05 17:59:41 +00:00
|
|
|
describe('class constructor', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
memo = createMemoizer();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set .isLocalStorageAvailable', () => {
|
|
|
|
expect(AccessorUtilities.isLocalStorageAccessSafe).toHaveBeenCalled();
|
|
|
|
expect(memo.isLocalStorageAvailable).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('saveData', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
memo = {
|
|
|
|
currentTabKey,
|
|
|
|
};
|
|
|
|
|
|
|
|
spyOn(localStorage, 'setItem');
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('if .isLocalStorageAvailable is `false`', () => {
|
|
|
|
beforeEach(function () {
|
|
|
|
memo.isLocalStorageAvailable = false;
|
|
|
|
|
2017-07-06 18:58:47 +00:00
|
|
|
SigninTabsMemoizer.prototype.saveData.call(memo);
|
2017-05-05 17:59:41 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not call .setItem', () => {
|
|
|
|
expect(localStorage.setItem).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('if .isLocalStorageAvailable is `true`', () => {
|
|
|
|
const value = 'value';
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
memo.isLocalStorageAvailable = true;
|
|
|
|
|
2017-07-06 18:58:47 +00:00
|
|
|
SigninTabsMemoizer.prototype.saveData.call(memo, value);
|
2017-05-05 17:59:41 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should call .setItem', () => {
|
|
|
|
expect(localStorage.setItem).toHaveBeenCalledWith(currentTabKey, value);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('readData', () => {
|
|
|
|
const itemValue = 'itemValue';
|
|
|
|
let readData;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
memo = {
|
|
|
|
currentTabKey,
|
|
|
|
};
|
|
|
|
|
|
|
|
spyOn(localStorage, 'getItem').and.returnValue(itemValue);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('if .isLocalStorageAvailable is `false`', () => {
|
|
|
|
beforeEach(function () {
|
|
|
|
memo.isLocalStorageAvailable = false;
|
|
|
|
|
2017-07-06 18:58:47 +00:00
|
|
|
readData = SigninTabsMemoizer.prototype.readData.call(memo);
|
2017-05-05 17:59:41 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not call .getItem and should return `null`', () => {
|
|
|
|
expect(localStorage.getItem).not.toHaveBeenCalled();
|
|
|
|
expect(readData).toBe(null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('if .isLocalStorageAvailable is `true`', () => {
|
|
|
|
beforeEach(function () {
|
|
|
|
memo.isLocalStorageAvailable = true;
|
|
|
|
|
2017-07-06 18:58:47 +00:00
|
|
|
readData = SigninTabsMemoizer.prototype.readData.call(memo);
|
2017-05-05 17:59:41 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should call .getItem and return the localStorage value', () => {
|
|
|
|
expect(window.localStorage.getItem).toHaveBeenCalledWith(currentTabKey);
|
|
|
|
expect(readData).toBe(itemValue);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-11-06 18:41:23 +00:00
|
|
|
});
|
2017-07-06 18:58:47 +00:00
|
|
|
})();
|