Skip to content

Commit

Permalink
patrol
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron Chong <aaronchongth@gmail.com>
  • Loading branch information
aaronchongth committed Dec 6, 2024
1 parent f38fb48 commit bca4188
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { fireEvent, render, screen } from '@testing-library/react';
import { describe, expect, it, vi } from 'vitest';

import { CustomComposeTaskForm } from './custom-compose';
import {
CustomComposeTaskDefinition,
CustomComposeTaskForm,
isCustomTaskDescriptionValid,
makeCustomComposeTaskBookingLabel,
makeCustomComposeTaskShortDescription,
} from './custom-compose';

describe('Custom compose task form', () => {
it('Renders custom compose task form', async () => {
Expand All @@ -28,4 +34,18 @@ describe('Custom compose task form', () => {
fireEvent.change(textArea, { target: { value: validTaskDesc } });
expect(onValidate).toHaveBeenCalledWith(true);
});

it('Validate description', () => {
expect(isCustomTaskDescriptionValid('invalid json')).not.toBeTruthy();
expect(isCustomTaskDescriptionValid('{"valid": "json"}')).toBeTruthy();
});

it('Booking label', () => {
const bookingLabel = makeCustomComposeTaskBookingLabel();
expect(bookingLabel.task_definition_id).toBe(CustomComposeTaskDefinition.taskDefinitionId);
});

it('Short description', () => {
expect(makeCustomComposeTaskShortDescription('{"valid": "json"}')).toBe('{"valid": "json"}');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function makeCustomComposeTaskShortDescription(desc: CustomComposeTaskDes
return desc;
}

const isCustomTaskDescriptionValid = (taskDescription: string): boolean => {
export const isCustomTaskDescriptionValid = (taskDescription: string): boolean => {
if (taskDescription.length === 0) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Meta, StoryObj } from '@storybook/react';

import { makeDefaultPatrolTaskDescription, PatrolTaskForm } from './patrol';

export default {
title: 'Tasks/PatrolTaskForm',
component: PatrolTaskForm,
} satisfies Meta;

type Story = StoryObj<typeof PatrolTaskForm>;

export const Default: Story = {
args: {
taskDesc: makeDefaultPatrolTaskDescription(),
patrolWaypoints: ['waypoint_1', 'waypoint_2', 'waypoint_3'],
onChange: () => {},
onValidate: () => {},
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { fireEvent, render, screen, within } from '@testing-library/react';
import { describe, expect, it, vi } from 'vitest';

import {
addPlaceToPatrolTaskDescription,
isPatrolTaskDescriptionValid,
makeDefaultPatrolTaskDescription,
makePatrolTaskBookingLabel,
makePatrolTaskShortDescription,
PatrolTaskDefinition,
PatrolTaskForm,
} from './patrol';

const mockWaypoints = ['waypoint_1', 'waypoint_2', 'waypoint_3'];

describe('Patrol task form', () => {
it('PatrolTaskForm renders, changes and validates', async () => {
const onChange = vi.fn();
const onValidate = vi.fn();

render(
<PatrolTaskForm
taskDesc={makeDefaultPatrolTaskDescription()}
patrolWaypoints={mockWaypoints}
onChange={onChange}
onValidate={onValidate}
/>,
);

const autocomplete = screen.getByTestId('place-name');
const input = within(autocomplete).getByLabelText(/place name/i);
autocomplete.focus();
fireEvent.change(input, { target: { value: 'a' } });
fireEvent.keyDown(autocomplete, { key: 'ArrowDown' });
fireEvent.keyDown(autocomplete, { key: 'Enter' });

expect(onChange).toHaveBeenCalled();
expect(onValidate).toHaveBeenCalled();
});

it('PatrolTaskForm renders and has places', async () => {
const onChange = vi.fn();
const onValidate = vi.fn();

const desc = makeDefaultPatrolTaskDescription();
const updatedDesc = addPlaceToPatrolTaskDescription(desc, 'waypoint_1');

const root = render(
<PatrolTaskForm
taskDesc={updatedDesc}
patrolWaypoints={mockWaypoints}
onChange={onChange}
onValidate={onValidate}
/>,
);

expect(root.getByText(/waypoint_1/i));
});

it('booking label', () => {
let desc = makeDefaultPatrolTaskDescription();
desc = addPlaceToPatrolTaskDescription(desc, 'waypoint_1');
let label = makePatrolTaskBookingLabel(desc);
expect(label.task_definition_id).toBe(PatrolTaskDefinition.taskDefinitionId);
expect(label.destination).toBe('waypoint_1');

desc = addPlaceToPatrolTaskDescription(desc, 'waypoint_2');
label = makePatrolTaskBookingLabel(desc);
expect(label.task_definition_id).toBe(PatrolTaskDefinition.taskDefinitionId);
expect(label.destination).toBe('waypoint_2');
});

it('validity', () => {
let desc = makeDefaultPatrolTaskDescription();
expect(isPatrolTaskDescriptionValid(desc)).not.toBeTruthy();

desc = addPlaceToPatrolTaskDescription(desc, 'waypoint_1');
expect(isPatrolTaskDescriptionValid(desc)).toBeTruthy();
});

it('short description', () => {
let desc = makeDefaultPatrolTaskDescription();
desc = addPlaceToPatrolTaskDescription(desc, 'waypoint_1');
desc = addPlaceToPatrolTaskDescription(desc, 'waypoint_2');
expect(makePatrolTaskShortDescription(desc, undefined)).toBe(
'[Patrol] [1] round/s, along [waypoint_1], [waypoint_2]',
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ interface PlaceListProps {

function PlaceList({ places, onClick }: PlaceListProps) {
const theme = useTheme();
console.log(places);
return (
<List
dense
Expand All @@ -88,6 +89,7 @@ function PlaceList({ places, onClick }: PlaceListProps) {
{places.map((value, index) => (
<ListItem
key={`${value}-${index}`}
data-testid={`${value}-${index}`}
secondaryAction={
<IconButton edge="end" aria-label="delete" onClick={() => onClick(index)}>
<DeleteIcon />
Expand All @@ -104,6 +106,17 @@ function PlaceList({ places, onClick }: PlaceListProps) {
);
}

export function addPlaceToPatrolTaskDescription(
taskDesc: PatrolTaskDescription,
place: string,
): PatrolTaskDescription {
const updatedTaskDesc = {
...taskDesc,
places: taskDesc.places.concat(place).filter((el: string) => el),
};
return updatedTaskDesc;
}

interface PatrolTaskFormProps {
taskDesc: PatrolTaskDescription;
patrolWaypoints: string[];
Expand Down Expand Up @@ -132,15 +145,12 @@ export function PatrolTaskForm({
<Grid item xs={10}>
<Autocomplete
id="place-input"
data-testid="place-name"
freeSolo
fullWidth
options={patrolWaypoints.sort()}
onChange={(_ev, newValue) =>
newValue !== null &&
onInputChange({
...taskDesc,
places: taskDesc.places.concat(newValue).filter((el: string) => el),
})
newValue !== null && onInputChange(addPlaceToPatrolTaskDescription(taskDesc, newValue))
}
sx={{
'& .MuiOutlinedInput-root': {
Expand Down

0 comments on commit bca4188

Please sign in to comment.