Skip to content

Commit

Permalink
Merge pull request #328 from adhocteam/TTAHUB-105/al-increase-unit-te…
Browse files Browse the repository at this point in the history
…st-coverage

Ttahub 105/al increase unit test coverage
  • Loading branch information
AdamAdHocTeam committed Jun 9, 2021
2 parents d51d2d9 + 90f7d02 commit dac56f9
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 1 deletion.
18 changes: 17 additions & 1 deletion frontend/src/components/__tests__/DeleteReportModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('DeleteReportModal', () => {
expect(buttons.length).toBe(2);
});

it('exits when escapse key is pressed', async () => {
it('exits when escape key is pressed', async () => {
// Given a page with a modal
render(<SomeComponent />);

Expand All @@ -56,4 +56,20 @@ describe('DeleteReportModal', () => {
userEvent.type(modal, '{esc}', { skipClick: true });
expect(screen.queryByTestId('modal')).not.toBeTruthy();
});

it('does not escape when any other key is pressed', async () => {
// Given a page with a modal
render(<SomeComponent />);

// When the modal is triggered
const button = await screen.findByText('Open');
userEvent.click(button);

const modal = await screen.findByTestId('modal');
expect(modal).toBeVisible();

// And the modal can closeclose the modal via the escape key
userEvent.type(modal, '{enter}', { skipClick: true });
expect(screen.queryByTestId('modal')).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,28 @@ describe('Goal', () => {
}]);
});

it('cant be removed', async () => {
const onUpdate = jest.fn();
const objectives = [
{
id: 'a', title: 'first', ttaProvided: '<p>This is the TTA Desc</p>', status: 'Not Started',
},
];
render(<RenderGoal onUpdateObjectives={onUpdate} name="test goal" objectives={objectives} />);

const optionsObjBtn = screen.getByRole('button', { name: /edit or delete objective 1 on goal 1/i });
fireEvent.click(optionsObjBtn);

const deleteObjBtn = await screen.findByRole('button', { name: 'Delete' });
fireEvent.click(deleteObjBtn);

const objName = screen.getByText(/first/i);
expect(objName).toBeVisible();

const objDesc = screen.getByText(/this is the tta desc/i);
expect(objDesc).toBeVisible();
});

it('can be updated', async () => {
const onUpdate = jest.fn();
const objectives = [{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,80 @@ describe('GoalPicker', () => {
expect(screen.queryByText('test goal edited')).toBeVisible();
expect(screen.queryByText('another goal')).toBeVisible();
});

it('cant be updated, if new name is blank', async () => {
const availableGoals = [];
const selectedGoals = [
{
id: 1, name: 'goal to edit', new: true, objectives: [],
},
{ id: 2, name: 'another goal', objectives: [] },
];

render(
<RenderGoal
availableGoals={availableGoals}
selectedGoals={selectedGoals}
/>,
);

const menuButton = await screen.findByRole('button', { name: /actions for goal 1/i });
fireEvent.click(menuButton);

const editButton = await screen.findByRole('button', { name: 'Edit' });
fireEvent.click(editButton);

const goalNameInput = await screen.findByLabelText('Edit goal');
await waitFor(() => expect(goalNameInput).toBeVisible());

fireEvent.change(goalNameInput, { target: { value: '' } });

const updateButton = await screen.findByRole('button', { name: 'Update Goal' });
fireEvent.click(updateButton);

// Old goal name should exist, new blank goal name should not
expect(screen.queryByText('goal to edit')).toBeVisible();
expect(screen.queryByText('another goal')).toBeVisible();
});

it('objective can be updated', async () => {
const availableGoals = [];
const selectedGoals = [
{
id: 1,
name: 'goal to edit',
new: true,
objectives: [{
id: 1,
title: 'orig objective 1',
ttaProvided: 'objective 1 desc',
status: 'In Progress',
}],
},
];

render(
<RenderGoal
availableGoals={availableGoals}
selectedGoals={selectedGoals}
/>,
);

const optionsObjBtn = screen.getByRole('button', { name: /edit or delete objective 1 on goal 1/i });
fireEvent.click(optionsObjBtn);

const editObjBtn = await screen.findByRole('button', { name: 'Edit' });
fireEvent.click(editObjBtn);

const objectiveTitleTxtBx = screen.getByDisplayValue(/objective 1/i);
fireEvent.change(objectiveTitleTxtBx, { target: { value: 'updated objective 1' } });

const saveObjectiveBtn = screen.getByRole('button', { name: /save objective 1 on goal 1/i });
userEvent.click(saveObjectiveBtn);

expect(screen.queryByText('orig objective 1')).not.toBeInTheDocument();
expect(screen.queryByText('updated objective 1')).toBeVisible();
});
});

describe('input box', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* eslint-disable react/jsx-props-no-spreading */
import '@testing-library/jest-dom';
import {
render,
screen,
} from '@testing-library/react';
import React from 'react';
import { TextInput } from '@trussworks/react-uswds';
import ObjectiveFormItem from '../ObjectiveFormItem';

const RenderObjectiveFormItem = ({
// eslint-disable-next-line react/prop-types
isValid, formItemValue, onChange = () => { },
}) => (
<ObjectiveFormItem
showErrors={!isValid}
className="margin-top-0"
message="objective form item required"
label="Objective"
value={formItemValue}
>
<TextInput
name="text-input-name"
aria-label="text-input-label"
onChange={onChange}
value={formItemValue}
/>
</ObjectiveFormItem>
);

describe('ObjectiveFormItem', () => {
it('renders correctly', async () => {
const onChange = jest.fn();
render(<RenderObjectiveFormItem isValid formItemValue="some value" onChange={onChange} />);
const save = await screen.findByText('Objective');
expect(save).toBeVisible();
const errorMessage = screen.queryByText('objective form item required');
expect(errorMessage).toBeNull();
});

it('renders with required message', async () => {
const onChange = jest.fn();
render(<RenderObjectiveFormItem isValid={false} formItemValue="" onChange={onChange} />);
const save = await screen.findByText('Objective');
expect(save).toBeVisible();
const errorMessage = await screen.findByText('objective form item required');
expect(errorMessage).toBeVisible();
});
});

0 comments on commit dac56f9

Please sign in to comment.