Commit 8d29b435 authored by Martin Wortschack's avatar Martin Wortschack Committed by Fatih Acet

Update onboarding utils

- Add more constants
- Add specs for onboarding utils
parent 840b6f01
const STORAGE_KEY = 'onboarding_state';
export const ONBOARDING_DISMISSED_COOKIE_NAME = 'onboarding_dismissed';
export default STORAGE_KEY;
export const STORAGE_KEY = 'onboarding_state';
export const AVAILABLE_TOURS = {
GUIDED_GITLAB_TOUR: 1,
CREATE_PROJECT_TOUR: 2,
INVITE_COLLEAGUES_TOUR: 3,
};
export const ONBOARDING_PROPS_DEFAULTS = {
tourKey: AVAILABLE_TOURS.GUIDED_GITLAB_TOUR,
lastStepIndex: -1,
createdProjectPath: '',
};
......@@ -3,7 +3,7 @@ import { __ } from '~/locale';
import { GlLink, GlPopover } from '@gitlab/ui';
import UserAvatarImage from '~/vue_shared/components/user_avatar/user_avatar_image.vue';
import { redirectTo } from '~/lib/utils/url_utility';
import { resetOnboardingLocalStorage, updateLocalStorage } from './../../utils';
import onboardingUtils from './../../utils';
export default {
components: {
......@@ -48,11 +48,12 @@ export default {
},
methods: {
startTour() {
resetOnboardingLocalStorage();
onboardingUtils.resetOnboardingLocalStorage();
onboardingUtils.updateOnboardingDismissed(false);
redirectTo(this.projectFullPath);
},
skipTour() {
updateLocalStorage({ dismissed: true });
onboardingUtils.updateOnboardingDismissed(true);
redirectTo(this.skipUrl);
},
},
......
import STORAGE_KEY from './constants';
import Cookies from 'js-cookie';
import AccessorUtilities from '~/lib/utils/accessor';
import {
ONBOARDING_DISMISSED_COOKIE_NAME,
STORAGE_KEY,
ONBOARDING_PROPS_DEFAULTS,
} from './constants';
const isOnboardingDismissed = () => Cookies.get(ONBOARDING_DISMISSED_COOKIE_NAME) === 'true';
const updateOnboardingDismissed = dismissed =>
Cookies.set(ONBOARDING_DISMISSED_COOKIE_NAME, dismissed);
const resetOnboardingLocalStorage = () => {
if (AccessorUtilities.isLocalStorageAccessSafe()) {
localStorage.setItem(STORAGE_KEY, JSON.stringify(ONBOARDING_PROPS_DEFAULTS));
}
};
/**
* onboarding_state:
* activeTour: Number
* dismissed: Boolean
*/
const getOnboardingLocalStorageState = () => {
if (AccessorUtilities.isLocalStorageAccessSafe()) {
return JSON.parse(localStorage.getItem(STORAGE_KEY));
}
const ONBOARDING_PROPS_DEFAULTS = {
activeTourKey: 1,
dismissed: false,
return ONBOARDING_PROPS_DEFAULTS;
};
export const resetOnboardingLocalStorage = () => {
localStorage.setItem(STORAGE_KEY, JSON.stringify(ONBOARDING_PROPS_DEFAULTS));
};
const updateLocalStorage = updatedProps => {
if (AccessorUtilities.isLocalStorageAccessSafe()) {
let currentState = getOnboardingLocalStorageState();
export const getOnboardingLocalStorageState = () => JSON.parse(localStorage.getItem(STORAGE_KEY));
if (!currentState) {
currentState = resetOnboardingLocalStorage();
}
export const updateLocalStorage = updatedProps => {
let currentState = getOnboardingLocalStorageState();
const onboardingState = {
...currentState,
...updatedProps,
};
if (!currentState) {
currentState = resetOnboardingLocalStorage();
localStorage.setItem(STORAGE_KEY, JSON.stringify(onboardingState));
}
};
const onboardingState = {
...currentState,
...updatedProps,
};
localStorage.setItem(STORAGE_KEY, JSON.stringify(onboardingState));
const onboardingUtils = {
isOnboardingDismissed,
updateOnboardingDismissed,
resetOnboardingLocalStorage,
getOnboardingLocalStorageState,
updateLocalStorage,
};
export default onboardingUtils;
import Cookies from 'js-cookie';
import AccessorUtilities from '~/lib/utils/accessor';
import {
ONBOARDING_DISMISSED_COOKIE_NAME,
STORAGE_KEY,
ONBOARDING_PROPS_DEFAULTS,
} from 'ee/onboarding/constants';
import onboardingUtils from 'ee/onboarding/utils';
describe('User onboarding utils', () => {
beforeEach(() => {
Cookies.remove(ONBOARDING_DISMISSED_COOKIE_NAME);
onboardingUtils.resetOnboardingLocalStorage();
spyOn(AccessorUtilities, 'isLocalStorageAccessSafe').and.returnValue(true);
});
describe('isOnboardingDismissed', () => {
it('return true if the cookie value is true', () => {
Cookies.set(ONBOARDING_DISMISSED_COOKIE_NAME, true);
expect(onboardingUtils.isOnboardingDismissed()).toBe(true);
});
it('return false if the cookie is not set', () => {
expect(onboardingUtils.isOnboardingDismissed()).toBe(false);
});
});
describe('updateOnboardingDismissed', () => {
it('set the dismissed state on the cookie', () => {
const dismissed = true;
Cookies.set(ONBOARDING_DISMISSED_COOKIE_NAME, dismissed);
expect(Cookies.get(ONBOARDING_DISMISSED_COOKIE_NAME)).toBe(`${dismissed}`);
});
});
describe('resetOnboardingLocalStorage', () => {
it('resets the onboarding props in the localStorage to the default', () => {
const modified = {
tourKey: 2,
lastStepIndex: 5,
createdProjectPath: 'foo',
};
localStorage.setItem(STORAGE_KEY, JSON.stringify(modified));
onboardingUtils.resetOnboardingLocalStorage();
expect(JSON.parse(localStorage.getItem(STORAGE_KEY))).toEqual(ONBOARDING_PROPS_DEFAULTS);
});
});
describe('getOnboardingLocalStorageState', () => {
it('retrieves the proper values from localStorage', () => {
const modified = {
tourKey: 2,
lastStepIndex: 5,
createdProjectPath: 'foo',
};
localStorage.setItem(STORAGE_KEY, JSON.stringify(modified));
expect(onboardingUtils.getOnboardingLocalStorageState()).toEqual(modified);
});
});
describe('updateLocalStorage', () => {
it('updates the onboarding state on the localStorage', () => {
spyOn(localStorage, 'setItem');
const modified = {
tourKey: 2,
lastStepIndex: 5,
createdProjectPath: 'foo',
};
onboardingUtils.updateLocalStorage(modified);
expect(localStorage.setItem).toHaveBeenCalledWith(STORAGE_KEY, JSON.stringify(modified));
});
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment