Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rewards page local storage loader (uplift to 1.38.x) #12912

Merged
merged 1 commit into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions components/brave_rewards/resources/android_page/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,29 @@ export const defaultState: Rewards.State = {

export const load = (): Rewards.State => {
const data = window.localStorage.getItem(keyName)
let state: Rewards.State = defaultState
if (data) {
try {
state = JSON.parse(data)
if (!state || state.version !== defaultState.version) {
throw new Error('State versions do not match')
}
state.initializing = true
} catch (e) {
console.error('Could not parse local storage data: ', e)
}
if (!data) {
return defaultState
}

let parsedData: any
try {
parsedData = JSON.parse(data)
} catch {
parsedData = null
}

if (!parsedData || typeof parsedData !== 'object') {
console.error('Local storage data is not an object')
return defaultState
}

if (parsedData.version !== defaultState.version) {
console.error('Local storage state version does not match')
return defaultState
}
return state

parsedData.initializing = true
return parsedData as Rewards.State
}

export const debouncedSave = debounce((data: Rewards.State) => {
Expand Down
39 changes: 39 additions & 0 deletions components/brave_rewards/resources/page/storage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

import { load, defaultState } from './storage'

describe('local storage', () => {
describe('loading', () => {
it('returns default state if local storage data is not present', () => {
expect(load()).toEqual(defaultState)
})

it('returns default state if local storage is not JSON', () => {
localStorage['rewards-data'] = 'abc'
expect(load()).toEqual(defaultState)
})

it('returns default state if local storage is not JSON object', () => {
localStorage['rewards-data'] = '[]'
expect(load()).toEqual(defaultState)
})

it('returns default state if local storage is empty JSON object', () => {
localStorage['rewards-data'] = '{}'
expect(load()).toEqual(defaultState)
})

it('returns default state if local storage version does not match', () => {
localStorage['rewards-data'] = '{ "version": 0 }'
expect(load()).toEqual(defaultState)
})

it('returns loaded state with initializing flag set', () => {
const mockData = { version: defaultState.version }
localStorage['rewards-data'] = JSON.stringify(mockData)
expect(load()).toEqual({ ...mockData, initializing: true })
})
})
})
34 changes: 22 additions & 12 deletions components/brave_rewards/resources/page/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,29 @@ export const defaultState: Rewards.State = {

export const load = (): Rewards.State => {
const data = window.localStorage.getItem(keyName)
let state: Rewards.State = defaultState
if (data) {
try {
state = JSON.parse(data)
if (!state || state.version !== defaultState.version) {
throw new Error('State versions do not match')
}
state.initializing = true
} catch (e) {
console.error('Could not parse local storage data: ', e)
}
if (!data) {
return defaultState
}

let parsedData: any
try {
parsedData = JSON.parse(data)
} catch {
parsedData = null
}

if (!parsedData || typeof parsedData !== 'object') {
console.error('Local storage data is not an object')
return defaultState
}

if (parsedData.version !== defaultState.version) {
console.error('Local storage state version does not match')
return defaultState
}
return state

parsedData.initializing = true
return parsedData as Rewards.State
}

export const debouncedSave = debounce((data: Rewards.State) => {
Expand Down