-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
177 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { renderHook, act } from '@testing-library/react-hooks' | ||
import useUserSettings from './useUserSettings' | ||
import * as userSettingsService from '../services/userSettingsService' | ||
|
||
// Mock the userSettingsService module | ||
jest.mock('../services/userSettingsService') | ||
|
||
const mockUserSettings = userSettingsService as jest.Mocked< | ||
typeof userSettingsService | ||
> | ||
|
||
describe('useUserSettings', () => { | ||
it('should initialize with default settings and allow updates', async () => { | ||
mockUserSettings.getUserSettings.mockImplementation((cb) => { | ||
cb({ isPreserveLogsActive: true }) | ||
}) | ||
|
||
const { result } = renderHook(() => useUserSettings()) | ||
|
||
// Expect initial settings to be loaded into state | ||
expect(result.current[0]).toEqual({ | ||
isPreserveLogsActive: true, | ||
isInvertFilterActive: false, | ||
isRegexActive: false, | ||
}) | ||
|
||
// Update the state | ||
act(() => { | ||
result.current[1]({ | ||
isPreserveLogsActive: false, | ||
isInvertFilterActive: true, | ||
}) | ||
}) | ||
|
||
// Expect state to be updated | ||
expect(result.current[0]).toEqual({ | ||
isPreserveLogsActive: false, | ||
isInvertFilterActive: true, | ||
isRegexActive: false, | ||
}) | ||
|
||
// Expect setUserSettings was called with the new settings | ||
expect(userSettingsService.setUserSettings).toHaveBeenCalledWith({ | ||
isPreserveLogsActive: false, | ||
isInvertFilterActive: true, | ||
isRegexActive: false, | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { useEffect, useState } from 'react' | ||
import { | ||
getUserSettings, | ||
IUserSettings, | ||
setUserSettings, | ||
} from '../services/userSettingsService' | ||
|
||
const useUserSettings = () => { | ||
const [settings, setSettings] = useState<IUserSettings>({ | ||
isPreserveLogsActive: false, | ||
isInvertFilterActive: false, | ||
isRegexActive: false, | ||
}) | ||
|
||
// Load initial settings on component mount | ||
useEffect(() => { | ||
getUserSettings((userSettings) => { | ||
setSettings((prevSettings) => { | ||
return { ...prevSettings, ...userSettings } | ||
}) | ||
}) | ||
}, []) | ||
|
||
const setSettingsProxy = (newSettings: Partial<IUserSettings>) => { | ||
setUserSettings({ ...settings, ...newSettings }) | ||
setSettings({ ...settings, ...newSettings }) | ||
} | ||
|
||
return [settings, setSettingsProxy] as const | ||
} | ||
|
||
export default useUserSettings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { chromeProvider } from './chromeProvider' | ||
|
||
export interface IUserSettings { | ||
isPreserveLogsActive: boolean | ||
isInvertFilterActive: boolean | ||
isRegexActive: boolean | ||
} | ||
|
||
export const getUserSettings = ( | ||
cb: (settings: Partial<IUserSettings>) => void | ||
) => { | ||
const chrome = chromeProvider() | ||
chrome.storage.local.get('userSettings', (result) => { | ||
cb(result.userSettings || {}) | ||
}) | ||
} | ||
|
||
export const setUserSettings = (userSettings: Partial<IUserSettings>): void => { | ||
const chrome = chromeProvider() | ||
chrome.storage.local.set({ userSettings }) | ||
} |