Skip to content

Commit

Permalink
IS-2853: Enable search only on fodselsdato (#557)
Browse files Browse the repository at this point in the history
  • Loading branch information
vetlesolgaard authored Dec 3, 2024
1 parent 2cc0b10 commit c842f02
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
24 changes: 12 additions & 12 deletions src/components/sokperson/SokPerson.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function logSokPersonResults(amount: number) {
}

export default function SokPerson() {
const [nameInitials, setNameInitials] = useState<string>('');
const [initials, setInitials] = useState<string>('');
const [birthdate, setBirthdate] = useState<string>('');
const {
mutate,
Expand All @@ -72,15 +72,15 @@ export default function SokPerson() {
}
};

const validInitials = (initials: string): boolean => {
return initials.length <= 3 && initials.length > 1;
const isValidInitials = (initials: string): boolean => {
return initials === '' || (initials.length <= 3 && initials.length > 1);
};

const handleSubmit = () => {
const parsedBirthdate = parseBirthdate(birthdate);
if (validInitials(nameInitials) && !!parsedBirthdate) {
if (isValidInitials(initials) && !!parsedBirthdate) {
const requestDTO: SokDTO = {
initials: nameInitials.toLowerCase(),
initials: initials.toLowerCase(),
birthdate: parsedBirthdate,
};
mutate(requestDTO, {
Expand All @@ -92,8 +92,8 @@ export default function SokPerson() {
}
};

const invalidInitials = isFormError && !validInitials(nameInitials);
const invalidBirthdate = isFormError && parseBirthdate(birthdate) === null;
const isInvalidInitials = isFormError && !isValidInitials(initials);
const isInvalidBirthdate = isFormError && parseBirthdate(birthdate) === null;

return (
<>
Expand All @@ -115,16 +115,16 @@ export default function SokPerson() {
description="AB"
htmlSize={10}
type="text"
onChange={(e) => setNameInitials(e.target.value)}
error={invalidInitials}
onChange={(e) => setInitials(e.target.value)}
error={isFormError && !isValidInitials(initials)}
/>
<TextField
label="Fødselsdato"
description="ddmmåå"
htmlSize={14}
type="text"
onChange={(e) => setBirthdate(e.target.value)}
error={invalidBirthdate}
error={isInvalidBirthdate}
/>
<Button
loading={isLoading}
Expand All @@ -134,12 +134,12 @@ export default function SokPerson() {
Søk
</Button>
</HStack>
{invalidInitials && (
{isInvalidInitials && (
<ErrorMessage size="small">
{texts.validation.initials}
</ErrorMessage>
)}
{invalidBirthdate && (
{isInvalidBirthdate && (
<ErrorMessage size="small">
{texts.validation.birthdate}
</ErrorMessage>
Expand Down
2 changes: 1 addition & 1 deletion src/mocks/data/personoversiktEnhetMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export const personoversiktEnhetMock: PersonOversiktStatusDTO[] = [
{
...behandletPerson,
fnr: '59999933333',
navn: '',
navn: 'Navn Navnesen',
fodselsdato: new Date('1990-01-01'),
enhet: '0316',
veilederIdent: 'Z101010',
Expand Down
13 changes: 11 additions & 2 deletions test/components/SokPersonTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,24 @@ describe('SokPerson', () => {
expect(screen.getByRole('button', { name: 'Søk' })).to.exist;
});

it('should render validation errors for fields', async () => {
it('should render validation error for fodselsdato not initials', async () => {
renderSokPerson();

await userEvent.click(screen.getByRole('button', { name: 'Søk' }));

expect(screen.getByText('Vennligst angi gyldige initialer')).to.exist;
expect(screen.getByText('Vennligst angi en gyldig fødselsdato')).to.exist;
expect(screen.queryByText('Vennligst angi gyldige initialer')).to.not.exist;
});
it('should render validation error for initialer when blank and too many characters', async () => {
renderSokPerson();

const initialsInput = screen.getByRole('textbox', { name: 'Initialer' });
fireEvent.change(initialsInput, { target: { value: ' ' } });
await userEvent.click(screen.getByRole('button', { name: 'Søk' }));
expect(screen.getByText('Vennligst angi gyldige initialer')).to.exist;
fireEvent.change(initialsInput, { target: { value: 'ABCD' } });
expect(screen.getByText('Vennligst angi gyldige initialer')).to.exist;
});
it('should send correct parameters', async () => {
renderSokPerson();

Expand Down

0 comments on commit c842f02

Please sign in to comment.