refactor: fixed unsafe roles dialog and hook logic, added tests

This commit is contained in:
Dan Ditomaso
2025-03-10 20:30:04 -04:00
parent 97f2abb582
commit f1a58f0434
15 changed files with 460 additions and 274 deletions
@@ -0,0 +1,46 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { render } 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';
// Mock dependencies
vi.mock('@core/stores/deviceStore', () => ({
useDevice: vi.fn()
}));
vi.mock('@components/Dialog/UnsafeRolesDialog/useUnsafeRolesDialog', () => ({
useUnsafeRolesDialog: vi.fn()
}));
describe('Device component with UnsafeRolesDialog', () => {
const setWorkingConfigMock = vi.fn();
const validateRoleDialogResultMock = vi.fn();
beforeEach(() => {
vi.resetAllMocks();
// Mock useDevice hook
(useDevice as any).mockReturnValue({
config: {
device: {}
},
setWorkingConfig: setWorkingConfigMock
});
// Mock useUnsafeRolesDialog hook
(useUnsafeRolesDialog as any).mockReturnValue({
validateRoleDialogResult: validateRoleDialogResultMock
});
});
it('should use the validateRoleDialogResult from the hook', () => {
render(<Device />);
// 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
});
});