Skip to content
Snippets Groups Projects
Commit 34904d26 authored by Stephanie Freitag's avatar Stephanie Freitag
Browse files

HOLI-11059: clean up old guest mode on web

parent f889f9ea
No related branches found
No related tags found
No related merge requests found
import { getGuestMode, storeGuestMode } from '@holi/core/screens/onboarding/helpers/guestModeStorage.web' import { getGuestMode, storeGuestMode } from '@holi/core/screens/onboarding/helpers/guestModeStorage.web'
import { getCookie, setCookie, deleteCookie } from 'cookies-next' import { getCookie, setCookie, deleteCookie } from 'cookies-next'
import { logError } from '@holi/core/errors/helpers' import { logError } from '@holi/core/errors/helpers'
import AsyncStorage from '@react-native-async-storage/async-storage'
jest.mock('@holi/core/errors/helpers', () => ({ jest.mock('@holi/core/errors/helpers', () => ({
logError: jest.fn(), logError: jest.fn(),
...@@ -17,7 +18,18 @@ jest.mock('cookies-next', () => { ...@@ -17,7 +18,18 @@ jest.mock('cookies-next', () => {
} }
}) })
const mockDeleteStorage = AsyncStorage.removeItem as jest.Mock
jest.mock('@react-native-async-storage/async-storage', () => {
return {
removeItem: jest.fn(),
}
})
describe('guestModeStorage', () => { describe('guestModeStorage', () => {
beforeEach(() => {
mockDeleteStorage.mockResolvedValue({})
})
it('should return true when guest mode is enabled', async () => { it('should return true when guest mode is enabled', async () => {
mockGetCookie.mockReturnValueOnce('true') mockGetCookie.mockReturnValueOnce('true')
...@@ -74,4 +86,15 @@ describe('guestModeStorage', () => { ...@@ -74,4 +86,15 @@ describe('guestModeStorage', () => {
location: 'guestModeStorage.storeGuestMode', location: 'guestModeStorage.storeGuestMode',
}) })
}) })
it('should clean up old guest mode', async () => {
jest.useFakeTimers()
await getGuestMode()
jest.runOnlyPendingTimers()
expect(mockDeleteStorage).toHaveBeenCalledWith('guest_mode')
jest.useRealTimers()
})
}) })
import { logError } from '@holi/core/errors/helpers' import { logError } from '@holi/core/errors/helpers'
import { getCookie, setCookie, deleteCookie } from 'cookies-next' import { getCookie, setCookie, deleteCookie } from 'cookies-next'
import AsyncStore from '@react-native-async-storage/async-storage'
export const GUEST_MODE_COOKIE_NAME = 'guest_mode' export const GUEST_MODE_COOKIE_NAME = 'guest_mode'
const one_year_in_seconds: number = 60 * 60 * 24 * 365 const one_year_in_seconds: number = 60 * 60 * 24 * 365
...@@ -17,10 +18,12 @@ export const storeGuestMode = async (guestMode?: boolean): Promise<void> => { ...@@ -17,10 +18,12 @@ export const storeGuestMode = async (guestMode?: boolean): Promise<void> => {
}) })
} }
} }
export const getGuestMode = async (): Promise<boolean> => { export const getGuestMode = async (): Promise<boolean> => {
try { try {
const userData = getCookie(GUEST_MODE_COOKIE_NAME) const guestMode = getCookie(GUEST_MODE_COOKIE_NAME)
return userData ? true : false cleanUpOldGuestMode().catch(() => {})
return !!guestMode
} catch (error) { } catch (error) {
logError(error, 'Failed to get guest mode', { logError(error, 'Failed to get guest mode', {
location: 'guestModeStorage.getGuestMode', location: 'guestModeStorage.getGuestMode',
...@@ -30,3 +33,6 @@ export const getGuestMode = async (): Promise<boolean> => { ...@@ -30,3 +33,6 @@ export const getGuestMode = async (): Promise<boolean> => {
} }
export const clearGuestMode = () => storeGuestMode() export const clearGuestMode = () => storeGuestMode()
// Deprecated guest mode stored in AsyncStorage in 1.53
const cleanUpOldGuestMode = () => AsyncStore.removeItem('guest_mode')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment