Skip to content

Commit

Permalink
PSP-8868 Display file marker locations for Lease/Licence (#4206)
Browse files Browse the repository at this point in the history
* PSP-8868 Display file marker location for Lease/Licence

* Test updates
  • Loading branch information
asanchezr authored Jul 17, 2024
1 parent 1377a11 commit a194db4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,48 @@ import { Formik } from 'formik';
import { createMemoryHistory } from 'history';
import noop from 'lodash/noop';

import { useMapStateMachine } from '@/components/common/mapFSM/MapStateMachineContext';
import { mockLeaseProperty } from '@/mocks/filterData.mock';
import { mapMachineBaseMock } from '@/mocks/mapFSM.mock';
import { ApiGen_Concepts_Lease } from '@/models/api/generated/ApiGen_Concepts_Lease';
import { getEmptyLease } from '@/models/defaultInitializers';
import { toTypeCode } from '@/utils/formUtils';
import { render, RenderOptions } from '@/utils/test-utils';
import { render, RenderOptions, waitForEffects } from '@/utils/test-utils';

import PropertiesInformation, { IPropertiesInformationProps } from './PropertiesInformation';

const history = createMemoryHistory();

describe('PropertiesInformation component', () => {
const customSetFilePropertyLocations = vi.fn();

describe('LeasePropertiesInformation component', () => {
// render component under test
const setup = (
renderOptions: RenderOptions &
IPropertiesInformationProps & { lease?: ApiGen_Concepts_Lease } = {},
) => {
// render component under test
const component = render(
const utils = render(
<Formik onSubmit={noop} initialValues={renderOptions.lease ?? getEmptyLease()}>
<PropertiesInformation nameSpace={renderOptions.nameSpace} />
</Formik>,
{
...renderOptions,
history,
mockMapMachine: {
...mapMachineBaseMock,
setFilePropertyLocations: customSetFilePropertyLocations,
},
},
);

return {
component,
};
return { ...utils };
};

beforeEach(() => {
vi.resetAllMocks();
});

it('renders as expected', () => {
const { component } = setup({
const { asFragment } = setup({
lease: {
...getEmptyLease(),
fileProperties: [
Expand All @@ -54,10 +57,11 @@ describe('PropertiesInformation component', () => {
],
},
});
expect(component.asFragment()).toMatchSnapshot();
expect(asFragment()).toMatchSnapshot();
});
it('renders one Property Information section per property', () => {
const { component } = setup({

it('renders one Property Information section for all properties', () => {
const { getAllByText } = setup({
lease: {
...getEmptyLease(),
fileProperties: [
Expand All @@ -80,19 +84,38 @@ describe('PropertiesInformation component', () => {
],
},
});
const { getAllByText } = component;
const propertyHeaders = getAllByText('Property Information');

const propertyHeaders = getAllByText('Property Information');
expect(propertyHeaders).toHaveLength(1);
});

it('renders no property information section if there are no properties', () => {
const { component } = setup({
const { queryByText } = setup({
lease: { ...getEmptyLease(), fileProperties: [] },
});
const { queryByText } = component;
const propertyHeader = queryByText('Property Information');

const propertyHeader = queryByText('Property Information');
expect(propertyHeader).toBeNull();
});

it('renders draft markers with provided lat/lng', async () => {
setup({
lease: {
...getEmptyLease(),
fileProperties: [
{
...mockLeaseProperty(1),
areaUnitType: toTypeCode('test'),
leaseArea: 123,
file: null,
fileId: 1,
location: { coordinate: { x: 123, y: 48 } },
},
],
},
});

await waitForEffects();
expect(customSetFilePropertyLocations).toHaveBeenCalledWith([{ lat: 48, lng: 123 }]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Section } from '@/components/common/Section/Section';
import { PropertyInformation } from '@/features/leases';
import { ApiGen_Concepts_Lease } from '@/models/api/generated/ApiGen_Concepts_Lease';
import { ApiGen_Concepts_PropertyLease } from '@/models/api/generated/ApiGen_Concepts_PropertyLease';
import { exists } from '@/utils';
import { exists, getLatLng, locationFromFileProperty } from '@/utils';
import { withNameSpace } from '@/utils/formUtils';

export interface IPropertiesInformationProps {
Expand All @@ -32,17 +32,14 @@ export const PropertiesInformation: React.FunctionComponent<
const { setFilePropertyLocations } = useMapStateMachine();

const locations: LatLngLiteral[] = useMemo(() => {
return (
properties
.map<LatLngLiteral | undefined>(x => {
if (exists(x.property?.latitude) && exists(x.property?.longitude)) {
return { lat: x.property!.latitude, lng: x.property!.longitude };
} else {
return undefined;
}
})
.filter(exists) || []
);
if (exists(properties)) {
return properties
.map(x => locationFromFileProperty(x))
.map(y => getLatLng(y))
.filter(exists);
} else {
return [];
}
}, [properties]);

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`PropertiesInformation component > renders as expected 1`] = `
exports[`LeasePropertiesInformation component > renders as expected 1`] = `
<DocumentFragment>
<div
class="Toastify"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import noop from 'lodash/noop';
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';

import { useMapStateMachine } from '@/components/common/mapFSM/MapStateMachineContext';
import { mapMachineBaseMock } from '@/mocks/mapFSM.mock';
import { act, render, RenderOptions, userEvent, waitFor } from '@/utils/test-utils';

Expand Down

0 comments on commit a194db4

Please sign in to comment.