2020-10-24 23:57:45 +05:30
|
|
|
import { useLocalStorageSpy } from 'helpers/local_storage_helper';
|
2017-08-17 22:00:37 +05:30
|
|
|
import AccessorUtilities from '~/lib/utils/accessor';
|
2018-03-17 18:26:18 +05:30
|
|
|
import SigninTabsMemoizer from '~/pages/sessions/new/signin_tabs_memoizer';
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
useLocalStorageSpy();
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
describe('SigninTabsMemoizer', () => {
|
2019-07-07 11:18:12 +05:30
|
|
|
const fixtureTemplate = 'static/signin_tabs.html';
|
2018-12-13 13:39:08 +05:30
|
|
|
const tabSelector = 'ul.new-session-tabs';
|
|
|
|
const currentTabKey = 'current_signin_tab';
|
|
|
|
let memo;
|
|
|
|
|
|
|
|
function createMemoizer() {
|
|
|
|
memo = new SigninTabsMemoizer({
|
|
|
|
currentTabKey,
|
|
|
|
tabSelector,
|
|
|
|
});
|
|
|
|
return memo;
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
preloadFixtures(fixtureTemplate);
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
beforeEach(() => {
|
|
|
|
loadFixtures(fixtureTemplate);
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
jest.spyOn(AccessorUtilities, 'isLocalStorageAccessSafe').mockReturnValue(true);
|
2018-12-13 13:39:08 +05:30
|
|
|
});
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
it('does nothing if no tab was previously selected', () => {
|
|
|
|
createMemoizer();
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
expect(document.querySelector(`${tabSelector} > li.active a`).getAttribute('href')).toEqual(
|
|
|
|
'#ldap',
|
|
|
|
);
|
|
|
|
});
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
it('shows last selected tab on boot', () => {
|
|
|
|
createMemoizer().saveData('#ldap');
|
|
|
|
const fakeTab = {
|
|
|
|
click: () => {},
|
|
|
|
};
|
2020-06-23 00:09:42 +05:30
|
|
|
jest.spyOn(document, 'querySelector').mockReturnValue(fakeTab);
|
|
|
|
jest.spyOn(fakeTab, 'click').mockImplementation(() => {});
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
memo.bootstrap();
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
// verify that triggers click on the last selected tab
|
|
|
|
expect(document.querySelector).toHaveBeenCalledWith(`${tabSelector} a[href="#ldap"]`);
|
|
|
|
expect(fakeTab.click).toHaveBeenCalled();
|
|
|
|
});
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
it('clicks the first tab if value in local storage is bad', () => {
|
|
|
|
createMemoizer().saveData('#bogus');
|
|
|
|
const fakeTab = {
|
2020-06-23 00:09:42 +05:30
|
|
|
click: jest.fn().mockName('fakeTab_click'),
|
2018-12-13 13:39:08 +05:30
|
|
|
};
|
2020-06-23 00:09:42 +05:30
|
|
|
jest
|
|
|
|
.spyOn(document, 'querySelector')
|
|
|
|
.mockImplementation(selector =>
|
|
|
|
selector === `${tabSelector} a[href="#bogus"]` ? null : fakeTab,
|
|
|
|
);
|
2018-12-13 13:39:08 +05:30
|
|
|
|
|
|
|
memo.bootstrap();
|
|
|
|
|
|
|
|
// verify that triggers click on stored selector and fallback
|
2020-06-23 00:09:42 +05:30
|
|
|
expect(document.querySelector.mock.calls).toEqual([
|
2018-12-13 13:39:08 +05:30
|
|
|
['ul.new-session-tabs a[href="#bogus"]'],
|
|
|
|
['ul.new-session-tabs a'],
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(fakeTab.click).toHaveBeenCalled();
|
|
|
|
});
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
it('saves last selected tab on change', () => {
|
|
|
|
createMemoizer();
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
document.querySelector('a[href="#login-pane"]').click();
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
expect(memo.readData()).toEqual('#login-pane');
|
|
|
|
});
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
it('overrides last selected tab with hash tag when given', () => {
|
|
|
|
window.location.hash = '#ldap';
|
|
|
|
createMemoizer();
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
expect(memo.readData()).toEqual('#ldap');
|
|
|
|
});
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
describe('class constructor', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
memo = createMemoizer();
|
|
|
|
});
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
it('should set .isLocalStorageAvailable', () => {
|
|
|
|
expect(AccessorUtilities.isLocalStorageAccessSafe).toHaveBeenCalled();
|
|
|
|
expect(memo.isLocalStorageAvailable).toBe(true);
|
2018-03-17 18:26:18 +05:30
|
|
|
});
|
2018-12-13 13:39:08 +05:30
|
|
|
});
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
describe('saveData', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
memo = {
|
|
|
|
currentTabKey,
|
|
|
|
};
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
describe('if .isLocalStorageAvailable is `false`', () => {
|
2020-06-23 00:09:42 +05:30
|
|
|
beforeEach(() => {
|
2018-12-13 13:39:08 +05:30
|
|
|
memo.isLocalStorageAvailable = false;
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
SigninTabsMemoizer.prototype.saveData.call(memo);
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
it('should not call .setItem', () => {
|
|
|
|
expect(localStorage.setItem).not.toHaveBeenCalled();
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
2018-12-13 13:39:08 +05:30
|
|
|
});
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
describe('if .isLocalStorageAvailable is `true`', () => {
|
|
|
|
const value = 'value';
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
beforeEach(() => {
|
2018-12-13 13:39:08 +05:30
|
|
|
memo.isLocalStorageAvailable = true;
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
SigninTabsMemoizer.prototype.saveData.call(memo, value);
|
|
|
|
});
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
it('should call .setItem', () => {
|
|
|
|
expect(localStorage.setItem).toHaveBeenCalledWith(currentTabKey, value);
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
});
|
2018-12-13 13:39:08 +05:30
|
|
|
});
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
describe('readData', () => {
|
|
|
|
const itemValue = 'itemValue';
|
|
|
|
let readData;
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
beforeEach(() => {
|
|
|
|
memo = {
|
|
|
|
currentTabKey,
|
|
|
|
};
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
localStorage.getItem.mockReturnValue(itemValue);
|
2018-12-13 13:39:08 +05:30
|
|
|
});
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
describe('if .isLocalStorageAvailable is `false`', () => {
|
2020-06-23 00:09:42 +05:30
|
|
|
beforeEach(() => {
|
2018-12-13 13:39:08 +05:30
|
|
|
memo.isLocalStorageAvailable = false;
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
readData = SigninTabsMemoizer.prototype.readData.call(memo);
|
|
|
|
});
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
it('should not call .getItem and should return `null`', () => {
|
|
|
|
expect(localStorage.getItem).not.toHaveBeenCalled();
|
|
|
|
expect(readData).toBe(null);
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
2018-12-13 13:39:08 +05:30
|
|
|
});
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
describe('if .isLocalStorageAvailable is `true`', () => {
|
2020-06-23 00:09:42 +05:30
|
|
|
beforeEach(() => {
|
2018-12-13 13:39:08 +05:30
|
|
|
memo.isLocalStorageAvailable = true;
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
readData = SigninTabsMemoizer.prototype.readData.call(memo);
|
|
|
|
});
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
it('should call .getItem and return the localStorage value', () => {
|
|
|
|
expect(window.localStorage.getItem).toHaveBeenCalledWith(currentTabKey);
|
|
|
|
expect(readData).toBe(itemValue);
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-12-13 13:39:08 +05:30
|
|
|
});
|