-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add survey settings unit tests (#1827)
* implement client side pagination * add fetch report settings * Add checkbox and refine components * call patch update with the updated settings * handle navigation on save * validate survey during update * fix lint and unit test * Add service test * replace double quote with single * Complete dashboard url copy functionality * load engagement slug * remove generate dashboard button setting from report settings * remove only from unit test * update docstring and function names * Add unit tests for survey report settings page * Add edit settings in dropdown
- Loading branch information
1 parent
cc5d76f
commit 0f9a977
Showing
6 changed files
with
169 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
met-web/tests/unit/components/survey/SurveyReportSettings.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
import React from 'react'; | ||
import { render, waitFor, screen, fireEvent } from '@testing-library/react'; | ||
import '@testing-library/jest-dom'; | ||
import { setupEnv } from '../setEnvVars'; | ||
import * as reactRedux from 'react-redux'; | ||
import * as reactRouter from 'react-router'; | ||
import * as engagementService from 'services/engagementService'; | ||
import * as engagementSlugService from 'services/engagementSlugService'; | ||
import * as surveyService from 'services/surveyService'; | ||
import * as reportSettingsService from 'services/surveyService/reportSettingsService'; | ||
import { createDefaultSurvey, Survey } from 'models/survey'; | ||
import { draftEngagement } from '../factory'; | ||
import ReportSettings from 'components/survey/report'; | ||
import assert from 'assert'; | ||
|
||
const survey: Survey = { | ||
...createDefaultSurvey(), | ||
id: 1, | ||
name: 'Survey 1', | ||
engagement_id: 1, | ||
}; | ||
|
||
const surveyReportSettingOne = { | ||
id: 1, | ||
survey_id: survey.id, | ||
question_id: 1, | ||
question_key: 'question key one', | ||
question_type: 'question type one', | ||
question: 'question one', | ||
display: true, | ||
}; | ||
|
||
const surveyReportSettingTwo = { | ||
id: 2, | ||
survey_id: survey.id, | ||
question_id: 2, | ||
question_key: 'question key two', | ||
question_type: 'question type two', | ||
question: 'question two', | ||
display: false, | ||
}; | ||
|
||
const SurveyReportSettings = [surveyReportSettingOne, surveyReportSettingTwo]; | ||
|
||
const engagementSlug = { | ||
slug: 'engagement-name', | ||
}; | ||
|
||
jest.mock('hooks', () => ({ | ||
...jest.requireActual('hooks'), | ||
useAppSelector: jest.fn(() => true), | ||
})); | ||
|
||
jest.mock('@mui/material', () => ({ | ||
...jest.requireActual('@mui/material'), | ||
useMediaQuery: jest.fn(() => true), | ||
})); | ||
|
||
describe('Survey report settings tests', () => { | ||
jest.spyOn(reactRedux, 'useSelector').mockImplementation(() => jest.fn()); | ||
jest.spyOn(reactRedux, 'useDispatch').mockImplementation(() => jest.fn()); | ||
jest.spyOn(reactRouter, 'useNavigate').mockImplementation(() => jest.fn()); | ||
jest.spyOn(reactRouter, 'useParams').mockReturnValue({ surveyId: String(survey.id) }); | ||
jest.spyOn(engagementService, 'getEngagement').mockReturnValue(Promise.resolve(draftEngagement)); | ||
jest.spyOn(engagementSlugService, 'getSlugByEngagementId').mockReturnValue(Promise.resolve(engagementSlug)); | ||
jest.spyOn(surveyService, 'getSurvey').mockReturnValue(Promise.resolve(survey)); | ||
const fetchSurveyReportSettingsMock = jest | ||
.spyOn(reportSettingsService, 'fetchSurveyReportSettings') | ||
.mockReturnValue(Promise.resolve(SurveyReportSettings)); | ||
const updateSurveyReportSettingsMock = jest | ||
.spyOn(reportSettingsService, 'updateSurveyReportSettings') | ||
.mockReturnValue(Promise.resolve(SurveyReportSettings)); | ||
|
||
beforeEach(() => { | ||
setupEnv(); | ||
}); | ||
|
||
test('View survey report settings page', async () => { | ||
render(<ReportSettings />); | ||
|
||
await waitFor(() => { | ||
expect(fetchSurveyReportSettingsMock).toHaveBeenCalledTimes(1); | ||
}); | ||
await waitFor(() => { | ||
expect(screen.getByText(surveyReportSettingOne.question_type)).toBeVisible(); | ||
expect(screen.getByText(surveyReportSettingOne.question)).toBeVisible(); | ||
expect(screen.getByText(surveyReportSettingTwo.question_type)).toBeVisible(); | ||
expect(screen.getByText(surveyReportSettingTwo.question)).toBeVisible(); | ||
}); | ||
|
||
expect(screen.getByTestId(`checkbox-${surveyReportSettingOne.id}`).children[0]).toBeChecked(); | ||
expect(screen.getByTestId(`checkbox-${surveyReportSettingTwo.id}`).children[0]).not.toBeChecked(); | ||
}); | ||
|
||
test('Search question by question text', async () => { | ||
const { container } = render(<ReportSettings />); | ||
|
||
await waitFor(() => { | ||
expect(fetchSurveyReportSettingsMock).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText(surveyReportSettingOne.question_type)).toBeVisible(); | ||
}); | ||
|
||
const searchField = container.querySelector('input[name="searchText"]'); | ||
assert(searchField, 'Unable to find search field that matches the given query'); | ||
|
||
fireEvent.change(searchField, { target: { value: surveyReportSettingOne.question } }); | ||
fireEvent.click(screen.getByTestId('survey/report/search-button')); | ||
|
||
const table = screen.getByRole('table'); | ||
const tableBody = table.querySelector('tbody'); | ||
assert(searchField, 'Unable to find table'); | ||
|
||
// plus one for the row that displays a loader when the table is loading | ||
const originalNumberOfRows = SurveyReportSettings.length + 1; | ||
|
||
await waitFor(() => { | ||
expect(tableBody?.children.length).toBe(originalNumberOfRows - 1); | ||
}); | ||
}); | ||
|
||
test('Survey report settings can be updated', async () => { | ||
render(<ReportSettings />); | ||
|
||
await waitFor(() => { | ||
expect(fetchSurveyReportSettingsMock).toHaveBeenCalledTimes(1); | ||
expect(screen.getByText(surveyReportSettingOne.question)).toBeVisible(); | ||
expect(screen.getByText(surveyReportSettingTwo.question)).toBeVisible(); | ||
}); | ||
|
||
const uncheckedBox = screen.getByTestId(`checkbox-${surveyReportSettingTwo.id}`).children[0]; | ||
expect(uncheckedBox).toBeInTheDocument(); | ||
expect(uncheckedBox).not.toBeChecked(); | ||
|
||
fireEvent.click(uncheckedBox); | ||
await waitFor(() => { | ||
expect(screen.getByTestId(`checkbox-${surveyReportSettingTwo.id}`).children[0]).toBeChecked(); | ||
}); | ||
|
||
fireEvent.click(screen.getByTestId('survey/report/save-button')); | ||
await waitFor(() => { | ||
expect(updateSurveyReportSettingsMock).toHaveBeenNthCalledWith(1, String(survey.id), [ | ||
{ | ||
...surveyReportSettingTwo, | ||
display: true, | ||
}, | ||
]); | ||
}); | ||
}); | ||
}); |
4 changes: 2 additions & 2 deletions
4
...ts/unit/components/surveyListing.test.tsx → .../components/survey/surveyListing.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters