Skip to content

Commit

Permalink
Integrate root location wizard and test it
Browse files Browse the repository at this point in the history
  • Loading branch information
peterMuriuki committed Nov 30, 2023
1 parent ac85c68 commit 39b0c95
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from '../../ducks/location-tree-state';
import { useTranslation } from '../../mls';
import { RbacCheck } from '@opensrp/rbac';
import { RootLocationWizard } from '../RootLocationWizard';

reducerRegistry.register(reducerName, reducer);

Expand Down Expand Up @@ -68,6 +69,7 @@ export const LocationUnitList: React.FC<LocationUnitListProps> = (props: Locatio
const dispatch = useDispatch();
const { t } = useTranslation();
const history = useHistory();
const [showWizard, setShowWizard] = useState(false);

// get the root locations. the root node is the opensrp root location, its immediate children
// are the user-defined root locations.
Expand All @@ -76,12 +78,23 @@ export const LocationUnitList: React.FC<LocationUnitListProps> = (props: Locatio
isLoading: treeIsLoading,
error: treeError,
isFetching: treeIsFetching,
} = useGetLocationHierarchy(fhirBaseURL, fhirRootLocationId);
} = useGetLocationHierarchy(fhirBaseURL, fhirRootLocationId, {
enabled: !showWizard,
onError: (error) => {
if (error.statusCode === 404) {
setShowWizard(true);
}
},
});

if (treeIsLoading) {
return <Spin size="large" className="custom-spinner" />;
}

if (showWizard) {
return <RootLocationWizard fhirBaseUrl={fhirBaseURL} rootLocationId={fhirRootLocationId} />;
}

if (treeError && !treeData) {
return <BrokenPage errorMessage={`${treeError.message}`} />;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ import { RoleContext } from '@opensrp/rbac';
import { superUserRole } from '@opensrp/react-utils';
import { locationHierarchyResourceType } from '../../../constants';
import { locationResourceType } from '../../../constants';
import { locationSData } from '../../../ducks/tests/fixtures';
import userEvent from '@testing-library/user-event';
import { rootlocationFixture } from '../../RootLocationWizard/tests/fixtures';
import * as notifications from '@opensrp/notifications';

const history = createBrowserHistory();

Expand Down Expand Up @@ -84,8 +85,8 @@ describe('location-management/src/components/LocationUnitList', () => {
);
});

nock.cleanAll();
afterEach(() => {
nock.cleanAll();
cleanup();
});

Expand Down Expand Up @@ -208,14 +209,9 @@ describe('location-management/src/components/LocationUnitList', () => {

it('Passes selected node as the parent location when adding location clicked', async () => {
nock(props.fhirBaseURL)
.get(`/${locationResourceType}/_search`)
.query({ _summary: 'count' })
.reply(200, { total: 1000 });

nock(props.fhirBaseURL)
.get(`/${locationResourceType}/_search`)
.query({ _count: 1000 })
.reply(200, locationSData)
.get(`/${locationHierarchyResourceType}/_search`)
.query({ _id: props.fhirRootLocationId })
.reply(200, fhirHierarchy)
.persist();

render(<AppWrapper {...props} />);
Expand All @@ -239,4 +235,57 @@ describe('location-management/src/components/LocationUnitList', () => {
// check where we redirected to
expect(history.location.search).toEqual('?parentId=Location%2F303');
});

it('Root location wizard works correclty', async () => {
const notificationSuccessMock = jest.spyOn(notifications, 'sendSuccessNotification');

nock(props.fhirBaseURL)
.get(`/${locationHierarchyResourceType}/_search`)
.query({ _id: props.fhirRootLocationId })
.reply(404, {
resourceType: 'OperationOutcome',
issue: [
{
severity: 'error',
code: 'processing',
diagnostics: `HAPI-2001: Resource Location/${props.fhirRootLocationId} is not known`,
},
],
});

nock(props.fhirBaseURL)
.get(`/${locationResourceType}/_search`)
.query({ _summary: 'count' })
.reply(200, { total: 0 })
.persist();

nock(props.fhirBaseURL)
.put(`/Location/${rootlocationFixture.id}`, rootlocationFixture)
.reply(201, {})
.persist();

render(<AppWrapper {...props} />);

await waitForElementToBeRemoved(document.querySelector('.ant-spin'));

expect(screen.getByText(/Location Unit Management/)).toBeInTheDocument();
expect(screen.getByText(/Root location was not found/)).toBeInTheDocument();

await waitFor(() => {
expect(screen.queryByText(/No locations have been uploaded yet./)).toBeInTheDocument();
});

const locationCreate = screen.getByRole('button', { name: 'Create root location.' });

fireEvent.click(locationCreate);

expect(screen.getByText(/This action will create a new location with id/)).toBeInTheDocument();
const confirmBtn = screen.getByRole('button', { name: 'Proceed' });

fireEvent.click(confirmBtn);

await waitFor(() => {
expect(notificationSuccessMock).toHaveBeenCalledWith('Root location uploaded to the server.');
});
});
});

0 comments on commit 39b0c95

Please sign in to comment.