- {loading.isLoading &&
}
- {!loading.isLoading && !loading.error && (
- <>
-
{t('header.title')}
-
-
-
- {vehicleTypes.map((v) => (
- setSelected(v)}
- />
- ))}
-
+ return (
+
+ {loading.isLoading &&
}
+ {!loading.isLoading && !loading.error && (
+ <>
+
{t('header.title')}
+
+
+
+ {vehicleTypes.map((v) => (
+ setSelected(v)}
+ />
+ ))}
- >
- )}
-
- );
- },
- i18nVehicleTypeSelection,
-);
+
+ >
+ )}
+
+ );
+}, i18nVehicleTypeSelection);
diff --git a/packages/common-ui-web/src/components/VehicleTypeSelection/utils.ts b/packages/common-ui-web/src/components/VehicleTypeSelection/utils.ts
index e54a66a9d..1d9058694 100644
--- a/packages/common-ui-web/src/components/VehicleTypeSelection/utils.ts
+++ b/packages/common-ui-web/src/components/VehicleTypeSelection/utils.ts
@@ -1,3 +1,5 @@
+import { GetInspectionResponse } from '@monkvision/network';
+import { sights, vehicles } from '@monkvision/sights';
import { VehicleType } from '@monkvision/types';
export const VEHICLE_TYPE_ORDER = [
@@ -40,3 +42,21 @@ export function getInitialSelectedVehicleType(
}
return vehicleTypes[Math.floor(vehicleTypes.length / 2)];
}
+
+export function getVehicleTypeFromInspection(
+ response: GetInspectionResponse,
+): VehicleType | undefined {
+ const imageWithSightId = response.entities.images.find((image) => image.sightId);
+ if (!imageWithSightId) {
+ return undefined;
+ }
+
+ const sight = Object.values(sights).find((s) => s.id === imageWithSightId.sightId);
+ if (!sight) {
+ return undefined;
+ }
+
+ const vehicle = Object.values(vehicles).find((v) => v.id === sight.vehicle);
+
+ return vehicle?.type;
+}
diff --git a/packages/common-ui-web/test/components/VehicleTypeSelection/VehicleTypeSelection.test.tsx b/packages/common-ui-web/test/components/VehicleTypeSelection/VehicleTypeSelection.test.tsx
index a09c839d1..3abf334fb 100644
--- a/packages/common-ui-web/test/components/VehicleTypeSelection/VehicleTypeSelection.test.tsx
+++ b/packages/common-ui-web/test/components/VehicleTypeSelection/VehicleTypeSelection.test.tsx
@@ -1,24 +1,31 @@
+const { getVehicleTypes, getInitialSelectedVehicleType } = jest.requireActual(
+ '../../../src/components/VehicleTypeSelection/utils',
+);
jest.mock('../../../src/components/Button', () => ({
Button: jest.fn(() => <>>),
}));
jest.mock('../../../src/components/VehicleTypeSelection/VehicleTypeSelectionCard', () => ({
VehicleTypeSelectionCard: jest.fn(() => <>>),
}));
+jest.mock('@monkvision/sights', () => [{ id: 'test-sight-id' }]);
+jest.mock('../../../src/components/VehicleTypeSelection/utils', () => ({
+ getVehicleTypeFromInspection: jest.fn(() => 'test-vehicle-type'),
+ getVehicleTypes,
+ getInitialSelectedVehicleType,
+}));
import { act, render } from '@testing-library/react';
-import { VehicleType } from '@monkvision/types';
import { expectPropsOnChildMock } from '@monkvision/test-utils';
import { Button, VehicleTypeSelection } from '../../../src';
import { VehicleTypeSelectionCard } from '../../../src/components/VehicleTypeSelection/VehicleTypeSelectionCard';
+import { VehicleType } from '@monkvision/types';
import { useMonkAppState } from '@monkvision/common';
import { useMonkApi } from '@monkvision/network';
const appState = {
authToken: 'test-auth-token',
inspectionId: 'test-inspection-id',
- config: {
- apiDomain: 'test-api-domain',
- },
+ apiDomain: 'test-api-domain',
};
describe('VehicleTypeSelection component', () => {
@@ -102,46 +109,26 @@ describe('VehicleTypeSelection component', () => {
unmount();
});
- it('should update the vehicle in the inspection when user confirms his vehicle choice', () => {
- const onSelectVehicleType = jest.fn();
- const updateInspectionVehicle = jest.fn(() => Promise.resolve());
- (useMonkAppState as jest.Mock).mockImplementation(() => appState);
- (useMonkApi as jest.Mock).mockImplementation(() => ({ updateInspectionVehicle }));
- const { unmount } = render(
);
-
- const { vehicleType } = (VehicleTypeSelectionCard as jest.Mock).mock.calls.find(
- (args) => args[0].isSelected,
- )[0];
- expectPropsOnChildMock(Button, {
- children: 'header.confirm',
- });
- const { onClick } = (Button as unknown as jest.Mock).mock.calls.find(
- (args) => args[0].children === 'header.confirm',
- )[0];
-
- onClick();
- expect(updateInspectionVehicle).toHaveBeenCalled();
- expect(updateInspectionVehicle).toHaveBeenCalledWith({
- inspectionId: appState.inspectionId,
- vehicle: { type: vehicleType },
- });
-
- unmount();
- });
-
it('should trigger the onSelectVehicle if VehicleType is found in the fetched inspection', async () => {
const onSelectVehicleType = jest.fn();
(useMonkAppState as jest.Mock).mockImplementation(() => appState);
- const vehicleMock = {
- entities: { vehicles: [{ inspectionId: 'test-inspection-id', type: 'van' }] },
+ const imageMock = {
+ entities: { images: [{ sightId: 'test-sight-id' }] },
};
- const getInspection = jest.fn(() => Promise.resolve(vehicleMock));
+ const availableVehicleTypesMock = ['test-vehicle-type'] as unknown as VehicleType[];
+ const getInspection = jest.fn(() => Promise.resolve(imageMock));
(useMonkApi as jest.Mock).mockImplementation(() => ({ getInspection }));
await act(async () => {
- render(
);
+ render(
+
,
+ );
});
expect(onSelectVehicleType).toHaveBeenCalled();
- expect(onSelectVehicleType).toHaveBeenCalledWith(vehicleMock.entities.vehicles[0].type);
+ expect(onSelectVehicleType).toHaveBeenCalledWith('test-vehicle-type');
});
});