-
-
Notifications
You must be signed in to change notification settings - Fork 22
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
4 changed files
with
123 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { lobeChat, usePluginSettings } from '@lobehub/chat-plugin-sdk/client'; | ||
import { act, renderHook } from '@testing-library/react'; | ||
import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
||
let getPluginSettingsSpy: any; | ||
let setPluginSettingsSpy: any; | ||
|
||
describe('usePluginSettings', () => { | ||
const initialValue = { theme: 'dark' }; | ||
|
||
beforeEach(() => { | ||
// Spy on lobeChat methods and reset mocks before each test | ||
getPluginSettingsSpy = vi.spyOn(lobeChat, 'getPluginSettings').mockResolvedValue(undefined); | ||
setPluginSettingsSpy = vi.spyOn(lobeChat, 'setPluginSettings').mockResolvedValue(); | ||
}); | ||
|
||
afterEach(() => { | ||
// Reset mocks after each test | ||
vi.restoreAllMocks(); | ||
}); | ||
|
||
it('should initialize with the given initial value', () => { | ||
const { result } = renderHook(() => usePluginSettings(initialValue)); | ||
const [value] = result.current; | ||
|
||
expect(value).toEqual(initialValue); | ||
expect(getPluginSettingsSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should update state with the resolved settings', async () => { | ||
const newSettings = { theme: 'light' }; | ||
|
||
getPluginSettingsSpy.mockResolvedValue(newSettings); | ||
|
||
const { result } = renderHook(() => usePluginSettings(initialValue)); | ||
|
||
// Wait for state to update | ||
await vi.waitFor(() => expect(result.current[0]).toEqual(newSettings)); | ||
}); | ||
|
||
it('should not update state if resolved settings are undefined', async () => { | ||
const { result } = renderHook(() => usePluginSettings(initialValue)); | ||
|
||
await vi.waitFor(() => expect(result.current[0]).toEqual(initialValue)); | ||
}); | ||
|
||
it('should update value and call setPluginSettings when updateValue is called', async () => { | ||
const { result } = renderHook(() => usePluginSettings(initialValue)); | ||
const [, updateValue] = result.current; | ||
|
||
const updatedSettings = { theme: 'blue' }; | ||
|
||
act(() => { | ||
updateValue(updatedSettings); | ||
}); | ||
|
||
const [value] = result.current; | ||
expect(value).toEqual(updatedSettings); | ||
expect(setPluginSettingsSpy).toHaveBeenCalledWith(updatedSettings); | ||
}); | ||
}); |
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,60 @@ | ||
import { lobeChat, usePluginState } from '@lobehub/chat-plugin-sdk/client'; | ||
import { act, renderHook } from '@testing-library/react'; | ||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
||
describe('usePluginState', () => { | ||
const key = 'testKey'; | ||
const initialValue = 'initialValue'; | ||
|
||
let getPluginStateSpy: any; | ||
let setPluginStateSpy: any; | ||
|
||
beforeEach(() => { | ||
// Mock lobeChat methods before each test | ||
getPluginStateSpy = vi.spyOn(lobeChat, 'getPluginState').mockResolvedValue(undefined); | ||
setPluginStateSpy = vi.spyOn(lobeChat, 'setPluginState').mockResolvedValue(); | ||
}); | ||
|
||
afterEach(() => { | ||
// Restore mocks after each test | ||
vi.restoreAllMocks(); | ||
}); | ||
|
||
it('should initialize with the given initial value', () => { | ||
const { result } = renderHook(() => usePluginState(key, initialValue)); | ||
const [value] = result.current; | ||
|
||
expect(value).toBe(initialValue); | ||
expect(getPluginStateSpy).toHaveBeenCalledWith(key); | ||
}); | ||
|
||
it('should update state with the resolved plugin state', async () => { | ||
const resolvedState = 'resolvedState'; | ||
getPluginStateSpy.mockResolvedValue(resolvedState); | ||
|
||
const { result } = renderHook(() => usePluginState(key, initialValue)); | ||
|
||
// Wait for state to update | ||
await vi.waitFor(() => expect(result.current[0]).toBe(resolvedState)); | ||
}); | ||
|
||
it('should not update state if resolved plugin state is undefined', async () => { | ||
const { result } = renderHook(() => usePluginState(key, initialValue)); | ||
|
||
await vi.waitFor(() => expect(result.current[0]).toBe(initialValue)); | ||
}); | ||
|
||
it('should update value and call setPluginState when updateValue is called', async () => { | ||
const { result } = renderHook(() => usePluginState(key, initialValue)); | ||
const [, updateValue] = result.current; | ||
|
||
const newValue = 'newValue'; | ||
|
||
act(() => { | ||
updateValue(newValue); | ||
}); | ||
|
||
expect(result.current[0]).toBe(newValue); | ||
expect(setPluginStateSpy).toHaveBeenCalledWith(key, newValue); | ||
}); | ||
}); |
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