diff --git a/src/components/PageComponents/Config/Device/Device.test.tsx b/src/components/PageComponents/Config/Device/Device.test.tsx
index 52a390c5..f9688e90 100644
--- a/src/components/PageComponents/Config/Device/Device.test.tsx
+++ b/src/components/PageComponents/Config/Device/Device.test.tsx
@@ -1,10 +1,10 @@
-import { describe, it, expect, vi, beforeEach } from 'vitest';
-import { render } from '@testing-library/react';
+import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
+import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import { Device } from '@components/PageComponents/Config/Device/index.tsx';
-import { useDevice } from '@core/stores/deviceStore.ts';
-import { useUnsafeRolesDialog } from '@components/Dialog/UnsafeRolesDialog/useUnsafeRolesDialog.ts';
+import { useDevice } from "@core/stores/deviceStore.ts";
+import { useUnsafeRolesDialog } from "@components/Dialog/UnsafeRolesDialog/useUnsafeRolesDialog.ts";
+import { Protobuf } from "@meshtastic/core";
-// Mock dependencies
vi.mock('@core/stores/deviceStore', () => ({
useDevice: vi.fn()
}));
@@ -13,34 +13,117 @@ vi.mock('@components/Dialog/UnsafeRolesDialog/useUnsafeRolesDialog', () => ({
useUnsafeRolesDialog: vi.fn()
}));
-describe('Device component with UnsafeRolesDialog', () => {
+// Mock the DynamicForm component since we're testing the Device component,
+// not the DynamicForm implementation
+vi.mock('@components/Form/DynamicForm', () => ({
+ DynamicForm: vi.fn(({ onSubmit }) => {
+ // Render a simplified version of the form for testing
+ return (
+
+
+
+
+ );
+ })
+}));
+
+describe('Device component', () => {
const setWorkingConfigMock = vi.fn();
- const validateRoleDialogResultMock = vi.fn();
+ const validateRoleSelectionMock = vi.fn();
+ const mockDeviceConfig = {
+ role: "CLIENT",
+ buttonGpio: 0,
+ buzzerGpio: 0,
+ rebroadcastMode: "ALL",
+ nodeInfoBroadcastSecs: 300,
+ doubleTapAsButtonPress: false,
+ disableTripleClick: false,
+ ledHeartbeatDisabled: false,
+ };
beforeEach(() => {
vi.resetAllMocks();
-
- // Mock useDevice hook
+
+ // Mock the useDevice hook
(useDevice as any).mockReturnValue({
config: {
- device: {}
+ device: mockDeviceConfig
},
setWorkingConfig: setWorkingConfigMock
});
-
- // Mock useUnsafeRolesDialog hook
+
+ // Mock the useUnsafeRolesDialog hook
+ validateRoleSelectionMock.mockResolvedValue(true);
(useUnsafeRolesDialog as any).mockReturnValue({
- validateRoleDialogResult: validateRoleDialogResultMock
+ validateRoleSelection: validateRoleSelectionMock
});
});
- it('should use the validateRoleDialogResult from the hook', () => {
+ afterEach(() => {
+ vi.clearAllMocks();
+ });
+
+ it('should render the Device form', () => {
+ render();
+ expect(screen.getByTestId('dynamic-form')).toBeInTheDocument();
+ });
+
+ it('should use the validateRoleSelection from the unsafe roles hook', () => {
render();
-
- // Verify the hook was called
expect(useUnsafeRolesDialog).toHaveBeenCalled();
-
- // Verify the form is using the validation function from the hook
- expect(setWorkingConfigMock).not.toHaveBeenCalled(); // Just ensure the component rendered without errors
+ });
+
+ it('should call setWorkingConfig when form is submitted', async () => {
+ render();
+
+ fireEvent.click(screen.getByTestId('submit-button'));
+
+ await waitFor(() => {
+ expect(setWorkingConfigMock).toHaveBeenCalledWith(
+ expect.objectContaining({
+ payloadVariant: {
+ case: "device",
+ value: expect.objectContaining({ role: "CLIENT" })
+ }
+ })
+ );
+ });
+ });
+
+
+ it('should create config with proper structure', async () => {
+ render();
+
+ // Simulate form submission
+ fireEvent.click(screen.getByTestId('submit-button'));
+
+ await waitFor(() => {
+ expect(setWorkingConfigMock).toHaveBeenCalledWith(
+ expect.objectContaining({
+ payloadVariant: {
+ case: "device",
+ value: expect.any(Object)
+ }
+ })
+ );
+ });
});
});
\ No newline at end of file
diff --git a/vite.config.ts b/vite.config.ts
index 23fcfb8b..b6b90406 100644
--- a/vite.config.ts
+++ b/vite.config.ts
@@ -1,4 +1,4 @@
-import { defineConfig } from 'vitest/config';
+import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { VitePWA } from 'vite-plugin-pwa';
import { execSync } from 'node:child_process';
@@ -51,17 +51,4 @@ export default defineConfig({
optimizeDeps: {
exclude: ['react-scan']
},
-
- test: {
- environment: 'jsdom',
- globals: true,
- mockReset: true,
- clearMocks: true,
- restoreMocks: true,
- root: path.resolve(process.cwd(), './src'),
- include: ['**/*.{test,spec}.{ts,tsx}'],
- setupFiles: ["./src/tests/setupTests.ts"],
-
- }
-
});
\ No newline at end of file
diff --git a/vitest.config.ts b/vitest.config.ts
index e55b9841..73674288 100644
--- a/vitest.config.ts
+++ b/vitest.config.ts
@@ -16,9 +16,13 @@ export default defineConfig({
},
},
test: {
+ environment: 'jsdom',
globals: true,
- include: ['src/**/*.test.tsx', 'src/**/*.test.ts'],
- setupFiles: ['src/tests/setupTests.ts'],
- environment: 'happy-dom',
+ mockReset: true,
+ clearMocks: true,
+ restoreMocks: true,
+ root: path.resolve(process.cwd(), './src'),
+ include: ['**/*.{test,spec}.{ts,tsx}'],
+ setupFiles: ["./src/tests/setupTests.ts"],
},
})
\ No newline at end of file