-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(frontend): Add "Test Credentials" button for profiles
This commit introduces unit tests to verify the validity of profile credentials. It also incorporates a "Test Credentials" button on the profile data table that enables users to verify their credentials. The feature employs the `validate_credentials` feature of the backend, which ensures the accuracy of the user's profile details.' Refs: #14
- Loading branch information
Showing
6 changed files
with
187 additions
and
41 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
50 changes: 50 additions & 0 deletions
50
src/sections/profiles/components/test-credentials-button.test.tsx
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,50 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import TestCredentialsButton from './test-credentials-button'; | ||
import { clearMocks, mockIPC } from '@tauri-apps/api/mocks'; | ||
import { SWRConfig } from 'swr'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
describe('<TestCredentialsButton />', () => { | ||
afterEach(() => { | ||
clearMocks(); | ||
}); | ||
|
||
it('should render shield alert icon when profile credentials are invalid', async () => { | ||
mockIPC((cmd) => { | ||
if (cmd === 'validate_credentials') { | ||
return false; | ||
} | ||
}); | ||
render( | ||
<SWRConfig value={{ provider: () => new Map() }}> | ||
<TestCredentialsButton profile='prof1' /> | ||
</SWRConfig> | ||
); | ||
|
||
const button = screen.getByRole('button'); | ||
await userEvent.click(button); | ||
|
||
const shieldSvg = button.querySelector('svg'); | ||
expect(shieldSvg).toHaveClass('lucide-shield-alert'); | ||
}); | ||
|
||
it('should render shield check icon when profile credentials are valid', async () => { | ||
mockIPC((cmd) => { | ||
if (cmd === 'validate_credentials') { | ||
return true; | ||
} | ||
}); | ||
render( | ||
<SWRConfig value={{ provider: () => new Map() }}> | ||
<TestCredentialsButton profile='prof1' /> | ||
</SWRConfig> | ||
); | ||
|
||
const button = screen.getByRole('button'); | ||
await userEvent.click(button); | ||
|
||
const shieldSvg = button.querySelector('svg'); | ||
expect(shieldSvg).toHaveClass('lucide-shield-check'); | ||
}); | ||
}); |
61 changes: 61 additions & 0 deletions
61
src/sections/profiles/components/test-credentials-button.tsx
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 @@ | ||
'use client'; | ||
|
||
import { Shield, ShieldAlert, ShieldCheck } from 'lucide-react'; | ||
import { Button } from '@/components/ui/button'; | ||
import React, { useEffect, useState } from 'react'; | ||
import { invoke } from '@tauri-apps/api/tauri'; | ||
|
||
type TestCredentialsButtonProps = { | ||
profile: string; | ||
}; | ||
|
||
export default function TestCredentialsButton({ | ||
profile, | ||
}: Readonly<TestCredentialsButtonProps>) { | ||
const [validated, setValidated] = useState<boolean>(false); | ||
const [busy, setBusy] = useState<boolean>(false); | ||
const [valid, setValid] = useState<boolean>(false); | ||
|
||
useEffect(() => { | ||
setValidated(true); | ||
setBusy(true); | ||
|
||
invoke<boolean>('validate_credentials', { | ||
profileName: profile, | ||
}).then((value) => { | ||
setValid(value); | ||
}); | ||
|
||
setBusy(false); | ||
}, []); | ||
Check warning on line 30 in src/sections/profiles/components/test-credentials-button.tsx
|
||
|
||
return ( | ||
<Button | ||
variant='outline' | ||
size='icon' | ||
disabled={busy} | ||
onClick={() => { | ||
setValidated(true); | ||
setBusy(true); | ||
setTimeout(async () => { | ||
invoke<boolean>('validate_credentials', { | ||
profileName: profile, | ||
}).then((value) => { | ||
setValid(value); | ||
}); | ||
setBusy(false); | ||
}, 1000); | ||
}} | ||
> | ||
{validated ? ( | ||
valid ? ( | ||
<ShieldCheck className={`h-4 w-4 ${busy ? 'animate-pulse' : ''}`} /> | ||
) : ( | ||
<ShieldAlert className={`h-4 w-4 ${busy ? 'animate-pulse' : ''}`} /> | ||
) | ||
) : ( | ||
<Shield className='h-4 w-4' /> | ||
)} | ||
</Button> | ||
); | ||
} |