-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(test) Add unit tests for rendering types
- Loading branch information
1 parent
2f935f0
commit 3e36049
Showing
17 changed files
with
448 additions
and
16 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
File renamed without changes.
File renamed without changes.
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
48 changes: 48 additions & 0 deletions
48
...teractive-builder/modals/question/question-form/rendering-types/inputs/date/date.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,48 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import Date from './date.component'; | ||
import type { FormField } from '@openmrs/esm-form-engine-lib'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
const mockSetFormField = jest.fn(); | ||
const formField: FormField = { | ||
datePickerFormat: 'both', | ||
type: 'obs', | ||
questionOptions: { rendering: 'date' }, | ||
id: '1', | ||
}; | ||
|
||
describe('Date Component', () => { | ||
it('renders', () => { | ||
renderDateComponent(); | ||
|
||
expect(screen.getByText(/the type of date picker to show/i)).toBeInTheDocument(); | ||
}); | ||
|
||
it('checks the default radio button based on date picker format', () => { | ||
renderDateComponent(); | ||
|
||
const calendarAndTimerRadioButton = screen.getByRole('radio', { | ||
name: /calendar and timer/i, | ||
}); | ||
|
||
expect(calendarAndTimerRadioButton).toBeChecked(); | ||
}); | ||
|
||
it('updates the form field when the date picker type is changed', async () => { | ||
renderDateComponent(); | ||
|
||
const user = userEvent.setup(); | ||
const calendarRadioButton = screen.getByRole('radio', { name: /calendar only/i }); | ||
await user.click(calendarRadioButton); | ||
|
||
expect(mockSetFormField).toHaveBeenCalledWith({ | ||
...formField, | ||
datePickerFormat: 'calendar', | ||
}); | ||
}); | ||
}); | ||
|
||
function renderDateComponent() { | ||
render(<Date formField={formField} setFormField={mockSetFormField} />); | ||
} |
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
76 changes: 76 additions & 0 deletions
76
...ctive-builder/modals/question/question-form/rendering-types/inputs/number/number.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,76 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import Number from './number.component'; | ||
import type { FormField } from '@openmrs/esm-form-engine-lib'; | ||
|
||
const mockSetFormField = jest.fn(); | ||
const formField: FormField = { | ||
type: 'obs', | ||
questionOptions: { rendering: 'number', min: '1', max: '10' }, | ||
id: '1', | ||
}; | ||
|
||
describe('Number Component', () => { | ||
it('renders', () => { | ||
renderNumberComponent(); | ||
|
||
expect(screen.getByText(/min value that can be entered/i)).toBeInTheDocument(); | ||
expect(screen.getByText(/max value that can be entered/i)).toBeInTheDocument(); | ||
}); | ||
|
||
it('shows the default min and max values', () => { | ||
renderNumberComponent(); | ||
|
||
const minInput = screen.getByRole('textbox', { | ||
name: /min/i, | ||
}); | ||
const maxInput = screen.getByRole('textbox', { | ||
name: /max/i, | ||
}); | ||
|
||
expect(minInput).toHaveValue('1'); | ||
expect(maxInput).toHaveValue('10'); | ||
}); | ||
|
||
it('updates the form field when the min or max value is changed', async () => { | ||
formField.questionOptions.min = ''; | ||
formField.questionOptions.max = ''; | ||
renderNumberComponent(); | ||
const user = userEvent.setup(); | ||
|
||
const minInput = screen.getByRole('textbox', { name: /min value that can be entered/i }); | ||
await user.type(minInput, '5'); | ||
expect(mockSetFormField).toHaveBeenCalledWith({ | ||
...formField, | ||
questionOptions: { ...formField.questionOptions, min: '5' }, | ||
}); | ||
|
||
const maxInput = screen.getByRole('textbox', { name: /max value that can be entered/i }); | ||
await user.type(maxInput, '6'); | ||
expect(mockSetFormField).toHaveBeenLastCalledWith({ | ||
...formField, | ||
questionOptions: { ...formField.questionOptions, max: '6' }, | ||
}); | ||
}); | ||
|
||
it('shows the invalid min and max values', () => { | ||
formField.questionOptions.min = '4'; | ||
formField.questionOptions.max = '3'; | ||
renderNumberComponent(); | ||
|
||
const minInput = screen.getByRole('textbox', { | ||
name: /min value that can be entered/i, | ||
}); | ||
const maxInput = screen.getByRole('textbox', { | ||
name: /max value that can be entered/i, | ||
}); | ||
|
||
expect(minInput).toHaveClass('cds--text-input--invalid'); | ||
expect(maxInput).toHaveClass('cds--text-input--invalid'); | ||
}); | ||
}); | ||
|
||
function renderNumberComponent() { | ||
render(<Number formField={formField} setFormField={mockSetFormField} />); | ||
} |
7 changes: 3 additions & 4 deletions
7
...der/modals/question/question-form/rendering-types/inputs/text-area/textarea.component.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
50 changes: 50 additions & 0 deletions
50
...-builder/modals/question/question-form/rendering-types/inputs/text-area/textarea.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,50 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import TextArea from './textarea.component'; | ||
import type { FormField } from '@openmrs/esm-form-engine-lib'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
const mockSetFormField = jest.fn(); | ||
const formField: FormField = { | ||
datePickerFormat: 'both', | ||
type: 'obs', | ||
questionOptions: { rendering: 'textarea', rows: 5 }, | ||
id: '1', | ||
}; | ||
|
||
describe('Text Component', () => { | ||
it('renders', () => { | ||
renderTextComponent(); | ||
|
||
expect(screen.getByText(/rows/i)).toBeInTheDocument(); | ||
}); | ||
|
||
it('shows the default rows value', () => { | ||
renderTextComponent(); | ||
|
||
const rowsInput = screen.getByRole('textbox', { | ||
name: /rows/i, | ||
}); | ||
|
||
expect(rowsInput).toHaveValue('5'); | ||
}); | ||
|
||
it('updates the form field when the rows value is changed', async () => { | ||
formField.questionOptions.rows = 0; | ||
renderTextComponent(); | ||
const user = userEvent.setup(); | ||
const rowsInput = screen.getByRole('textbox', { | ||
name: /rows/i, | ||
}); | ||
await user.type(rowsInput, '8'); | ||
|
||
expect(mockSetFormField).toHaveBeenCalledWith({ | ||
...formField, | ||
questionOptions: { ...formField.questionOptions, rows: 8 }, | ||
}); | ||
}); | ||
}); | ||
|
||
function renderTextComponent() { | ||
render(<TextArea formField={formField} setFormField={mockSetFormField} />); | ||
} |
62 changes: 62 additions & 0 deletions
62
...teractive-builder/modals/question/question-form/rendering-types/inputs/text/text.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,62 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import Text from './text.component'; | ||
import type { FormField } from '@openmrs/esm-form-engine-lib'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
const mockSetFormField = jest.fn(); | ||
const formField: FormField = { | ||
datePickerFormat: 'both', | ||
type: 'obs', | ||
questionOptions: { rendering: 'text', minLength: '1', maxLength: '10' }, | ||
id: '1', | ||
}; | ||
|
||
describe('Text Component', () => { | ||
it('renders', () => { | ||
renderTextComponent(); | ||
|
||
expect(screen.getByText(/min length of characters/i)).toBeInTheDocument(); | ||
expect(screen.getByText(/max length of characters/i)).toBeInTheDocument(); | ||
}); | ||
|
||
it('shows the default min and max values', () => { | ||
renderTextComponent(); | ||
|
||
const minInput = screen.getByRole('textbox', { | ||
name: /min length of characters/i, | ||
}); | ||
const maxInput = screen.getByRole('textbox', { | ||
name: /max length of characters/i, | ||
}); | ||
|
||
expect(minInput).toHaveValue('1'); | ||
expect(maxInput).toHaveValue('10'); | ||
}); | ||
|
||
it('updates the form field when the min or max value is changed', async () => { | ||
formField.questionOptions.minLength = ''; | ||
formField.questionOptions.maxLength = ''; | ||
const user = userEvent.setup(); | ||
|
||
renderTextComponent(); | ||
|
||
const minInput = screen.getByRole('textbox', { name: /min length of characters/i }); | ||
await user.type(minInput, '5'); | ||
expect(mockSetFormField).toHaveBeenCalledWith({ | ||
...formField, | ||
questionOptions: { ...formField.questionOptions, minLength: '5' }, | ||
}); | ||
|
||
const maxInput = screen.getByRole('textbox', { name: /max length of characters/i }); | ||
await user.type(maxInput, '6'); | ||
expect(mockSetFormField).toHaveBeenLastCalledWith({ | ||
...formField, | ||
questionOptions: { ...formField.questionOptions, maxLength: '6' }, | ||
}); | ||
}); | ||
}); | ||
|
||
function renderTextComponent() { | ||
render(<Text formField={formField} setFormField={mockSetFormField} />); | ||
} |
49 changes: 49 additions & 0 deletions
49
...ctive-builder/modals/question/question-form/rendering-types/inputs/toggle/toggle.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,49 @@ | ||
import React from 'react'; | ||
import { fireEvent, render, screen } from '@testing-library/react'; | ||
import Toggle from './toggle.component'; | ||
import type { FormField } from '@openmrs/esm-form-engine-lib'; | ||
|
||
const mockSetFormField = jest.fn(); | ||
const formField: FormField = { | ||
datePickerFormat: 'both', | ||
type: 'obs', | ||
questionOptions: { rendering: 'toggle', toggleOptions: { labelTrue: 'True', labelFalse: 'False' } }, | ||
id: '1', | ||
}; | ||
|
||
describe('Toggle Component', () => { | ||
it('renders', () => { | ||
renderToggleComponent(); | ||
|
||
expect(screen.getByText(/label true/i)).toBeInTheDocument(); | ||
expect(screen.getByText(/label false/i)).toBeInTheDocument(); | ||
}); | ||
|
||
it('shows the default labelTrue and labelFalse values', () => { | ||
renderToggleComponent(); | ||
|
||
const labelTrue = screen.getByRole('textbox', { name: /label true/i }); | ||
const labelFalse = screen.getByRole('textbox', { name: /label false/i }); | ||
|
||
expect(labelTrue).toHaveValue('True'); | ||
expect(labelFalse).toHaveValue('False'); | ||
}); | ||
|
||
it('updates the form field when the labelTrue value is changed', () => { | ||
formField.questionOptions.toggleOptions.labelTrue = ''; | ||
renderToggleComponent(); | ||
|
||
const labelTrueInput = screen.getByRole('textbox', { name: /label true/i }); | ||
|
||
fireEvent.input(labelTrueInput, { target: { value: 'Yes' } }); | ||
|
||
expect(mockSetFormField).toHaveBeenLastCalledWith({ | ||
...formField, | ||
questionOptions: { ...formField.questionOptions, toggleOptions: { labelTrue: 'Yes', labelFalse: 'False' } }, | ||
}); | ||
}); | ||
}); | ||
|
||
function renderToggleComponent() { | ||
render(<Toggle formField={formField} setFormField={mockSetFormField} />); | ||
} |
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
Oops, something went wrong.