gitlab-org--gitlab-foss/spec/javascripts/signin_tabs_memoizer_spec.js

217 lines
5.7 KiB
JavaScript
Raw Normal View History

2017-05-05 13:59:41 -04:00
import AccessorUtilities from '~/lib/utils/accessor';
2018-01-10 13:56:15 -05:00
import SigninTabsMemoizer from '~/pages/sessions/new/signin_tabs_memoizer';
import trackData from '~/pages/sessions/new/index';
import Tracking from '~/tracking';
2017-05-05 13:59:41 -04:00
2018-10-17 03:16:21 -04:00
describe('SigninTabsMemoizer', () => {
const fixtureTemplate = 'static/signin_tabs.html';
2018-10-17 03:16:21 -04:00
const tabSelector = 'ul.new-session-tabs';
const currentTabKey = 'current_signin_tab';
let memo;
function createMemoizer() {
memo = new SigninTabsMemoizer({
currentTabKey,
tabSelector,
});
return memo;
}
2018-10-17 03:16:21 -04:00
preloadFixtures(fixtureTemplate);
2018-10-17 03:16:21 -04:00
beforeEach(() => {
loadFixtures(fixtureTemplate);
2017-05-05 13:59:41 -04:00
2018-10-17 03:16:21 -04:00
spyOn(AccessorUtilities, 'isLocalStorageAccessSafe').and.returnValue(true);
});
2018-10-17 03:16:21 -04:00
it('does nothing if no tab was previously selected', () => {
createMemoizer();
2018-10-17 03:16:21 -04:00
expect(document.querySelector(`${tabSelector} > li.active a`).getAttribute('href')).toEqual(
'#ldap',
);
});
2018-10-17 03:16:21 -04:00
it('shows last selected tab on boot', () => {
createMemoizer().saveData('#ldap');
const fakeTab = {
click: () => {},
};
spyOn(document, 'querySelector').and.returnValue(fakeTab);
spyOn(fakeTab, 'click');
2018-10-17 03:16:21 -04:00
memo.bootstrap();
2018-10-17 03:16:21 -04:00
// verify that triggers click on the last selected tab
expect(document.querySelector).toHaveBeenCalledWith(`${tabSelector} a[href="#ldap"]`);
expect(fakeTab.click).toHaveBeenCalled();
});
2018-10-17 03:16:21 -04:00
it('clicks the first tab if value in local storage is bad', () => {
createMemoizer().saveData('#bogus');
const fakeTab = {
click: () => {},
};
2018-11-16 15:07:38 -05:00
spyOn(document, 'querySelector').and.callFake(selector =>
selector === `${tabSelector} a[href="#bogus"]` ? null : fakeTab,
2018-10-17 03:16:21 -04:00
);
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'],
]);
2018-10-17 03:21:28 -04:00
2018-10-17 03:16:21 -04:00
expect(fakeTab.click).toHaveBeenCalled();
});
2018-10-17 03:16:21 -04:00
it('saves last selected tab on change', () => {
createMemoizer();
2018-10-17 03:16:21 -04:00
document.querySelector('a[href="#login-pane"]').click();
2018-10-17 03:16:21 -04:00
expect(memo.readData()).toEqual('#login-pane');
});
2017-05-05 13:59:41 -04:00
2018-10-17 03:16:21 -04:00
it('overrides last selected tab with hash tag when given', () => {
window.location.hash = '#ldap';
createMemoizer();
2018-10-17 03:16:21 -04:00
expect(memo.readData()).toEqual('#ldap');
});
2018-10-17 03:16:21 -04:00
describe('class constructor', () => {
beforeEach(() => {
memo = createMemoizer();
});
2017-05-05 13:59:41 -04:00
2018-10-17 03:16:21 -04:00
it('should set .isLocalStorageAvailable', () => {
expect(AccessorUtilities.isLocalStorageAccessSafe).toHaveBeenCalled();
expect(memo.isLocalStorageAvailable).toBe(true);
2017-05-05 13:59:41 -04:00
});
2018-10-17 03:16:21 -04:00
});
2017-05-05 13:59:41 -04:00
describe('trackData', () => {
beforeEach(() => {
spyOn(Tracking, 'event');
});
describe('with tracking data', () => {
beforeEach(() => {
gon.tracking_data = {
category: 'Growth::Acquisition::Experiment::SignUpFlow',
action: 'start',
label: 'uuid',
property: 'control_group',
};
trackData();
});
it('should track data when the "click" event of the register tab is triggered', () => {
document.querySelector('a[href="#register-pane"]').click();
expect(Tracking.event).toHaveBeenCalledWith(
'Growth::Acquisition::Experiment::SignUpFlow',
'start',
{
label: 'uuid',
property: 'control_group',
},
);
});
});
describe('without tracking data', () => {
beforeEach(() => {
gon.tracking_data = undefined;
trackData();
});
it('should not track data when the "click" event of the register tab is triggered', () => {
document.querySelector('a[href="#register-pane"]').click();
expect(Tracking.event).not.toHaveBeenCalled();
});
});
});
2018-10-17 03:16:21 -04:00
describe('saveData', () => {
beforeEach(() => {
memo = {
currentTabKey,
};
2017-05-05 13:59:41 -04:00
2018-10-17 03:16:21 -04:00
spyOn(localStorage, 'setItem');
});
2017-05-05 13:59:41 -04:00
2018-10-17 03:16:21 -04:00
describe('if .isLocalStorageAvailable is `false`', () => {
beforeEach(function() {
memo.isLocalStorageAvailable = false;
2017-05-05 13:59:41 -04:00
2018-10-17 03:16:21 -04:00
SigninTabsMemoizer.prototype.saveData.call(memo);
});
2017-05-05 13:59:41 -04:00
2018-10-17 03:16:21 -04:00
it('should not call .setItem', () => {
expect(localStorage.setItem).not.toHaveBeenCalled();
2017-05-05 13:59:41 -04:00
});
2018-10-17 03:16:21 -04:00
});
2017-05-05 13:59:41 -04:00
2018-10-17 03:16:21 -04:00
describe('if .isLocalStorageAvailable is `true`', () => {
const value = 'value';
2017-05-05 13:59:41 -04:00
2018-10-17 03:16:21 -04:00
beforeEach(function() {
memo.isLocalStorageAvailable = true;
2017-05-05 13:59:41 -04:00
2018-10-17 03:16:21 -04:00
SigninTabsMemoizer.prototype.saveData.call(memo, value);
});
2017-05-05 13:59:41 -04:00
2018-10-17 03:16:21 -04:00
it('should call .setItem', () => {
expect(localStorage.setItem).toHaveBeenCalledWith(currentTabKey, value);
2017-05-05 13:59:41 -04:00
});
});
2018-10-17 03:16:21 -04:00
});
2017-05-05 13:59:41 -04:00
2018-10-17 03:16:21 -04:00
describe('readData', () => {
const itemValue = 'itemValue';
let readData;
2017-05-05 13:59:41 -04:00
2018-10-17 03:16:21 -04:00
beforeEach(() => {
memo = {
currentTabKey,
};
2017-05-05 13:59:41 -04:00
2018-10-17 03:16:21 -04:00
spyOn(localStorage, 'getItem').and.returnValue(itemValue);
});
2017-05-05 13:59:41 -04:00
2018-10-17 03:16:21 -04:00
describe('if .isLocalStorageAvailable is `false`', () => {
beforeEach(function() {
memo.isLocalStorageAvailable = false;
2017-05-05 13:59:41 -04:00
2018-10-17 03:16:21 -04:00
readData = SigninTabsMemoizer.prototype.readData.call(memo);
});
2017-05-05 13:59:41 -04:00
2018-10-17 03:16:21 -04:00
it('should not call .getItem and should return `null`', () => {
expect(localStorage.getItem).not.toHaveBeenCalled();
expect(readData).toBe(null);
2017-05-05 13:59:41 -04:00
});
2018-10-17 03:16:21 -04:00
});
2017-05-05 13:59:41 -04:00
2018-10-17 03:16:21 -04:00
describe('if .isLocalStorageAvailable is `true`', () => {
beforeEach(function() {
memo.isLocalStorageAvailable = true;
2017-05-05 13:59:41 -04:00
2018-10-17 03:16:21 -04:00
readData = SigninTabsMemoizer.prototype.readData.call(memo);
});
2017-05-05 13:59:41 -04:00
2018-10-17 03:16:21 -04:00
it('should call .getItem and return the localStorage value', () => {
expect(window.localStorage.getItem).toHaveBeenCalledWith(currentTabKey);
expect(readData).toBe(itemValue);
2017-05-05 13:59:41 -04:00
});
});
});
2018-10-17 03:16:21 -04:00
});