Skip to content

Commit

Permalink
✅ test: add test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Dec 29, 2023
1 parent 8f22e00 commit 23c9e08
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = {
},
rules: {
...config.rules,
'unicorn/no-useless-undefined': 0,
'unicorn/prefer-string-replace-all': 0,
'unicorn/switch-case-braces': 0,
},
Expand Down
61 changes: 61 additions & 0 deletions tests/client/usePluginSettings.test.ts
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);
});
});
60 changes: 60 additions & 0 deletions tests/client/usePluginState.test.ts
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);
});
});
1 change: 1 addition & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default defineConfig({
'@lobehub/chat-plugin-sdk/openapi': path.join(__dirname, './src/openapi'),
},
coverage: {
include: ['src'],
reporter: ['text', 'text-summary', 'json', 'lcov'],
},
environment: 'jsdom',
Expand Down

0 comments on commit 23c9e08

Please sign in to comment.