Skip to content

Commit

Permalink
Merge pull request #1528 from akvo/feature/1524-hh-consent-question
Browse files Browse the repository at this point in the history
Feature/1524 hh consent question
  • Loading branch information
ifirmawan authored Jun 26, 2024
2 parents 21dec46 + c341d0e commit be620ee
Show file tree
Hide file tree
Showing 9 changed files with 1,026 additions and 747 deletions.
10 changes: 7 additions & 3 deletions app/src/form/components/__tests__/QuestionField.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ describe('QuestionField component', () => {
const keyform = 1;
const field = {
id: 1,
name: 'Your Name',
name: 'full_name',
label: 'Your Name',
order: 1,
type: 'input',
required: true,
Expand Down Expand Up @@ -89,7 +90,8 @@ describe('QuestionField component', () => {
const keyform = 1;
const field = {
id: 1,
name: 'Sanitation',
name: 'sanitation',
label: 'Sanitation',
order: 1,
type: 'input',
required: true,
Expand Down Expand Up @@ -138,7 +140,8 @@ describe('QuestionField component', () => {
const keyform = 1;
const field = {
id: 1,
name: 'Your Name',
name: 'full_name',
label: 'Your Name',
order: 1,
type: 'input',
required: true,
Expand Down Expand Up @@ -312,6 +315,7 @@ describe('QuestionField component', () => {
};
act(() => {
FormState.update((s) => {
s.surveyStart = '2024-06-26-14.50.35.123';
s.form = {
json: JSON.stringify(example).replace(/'/g, "''"),
};
Expand Down
3 changes: 2 additions & 1 deletion app/src/form/fields/TypeAutofield.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ const strToFunction = (fnString, values) => {
const fnBody = fixIncompleteMathOperation(generateFnBody(fnStr, values));
// eslint-disable-next-line no-new-func
return new Function(`return ${fnBody}`);
} catch (error) {
} catch {
return null;
}
};
Expand Down Expand Up @@ -198,6 +198,7 @@ const TypeAutofield = ({
}
}
} catch (error) {
console.error('er', error);
Sentry.captureMessage(`[TypeAutofield] question ID: ${id}`);
Sentry.captureException(error);
}
Expand Down
34 changes: 31 additions & 3 deletions app/src/form/fields/__test__/TypeAutofield.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,35 @@ import TypeAutofield from '../TypeAutofield';
import { FormState } from '../../../store';

describe('TypeAutofield component', () => {
beforeAll(() => {
FormState.update((s) => {
s.surveyStart = '2024-06-26-14.50.35.123';
});
});

test('it gives the correct value', () => {
const mockFormQuestions = [
{
name: 'household_location',
label: 'HOUSEHOLD: Location',
question: [
{
id: 1,
order: 1,
name: 'total_male',
label: 'Total Male',
type: 'number',
},
{
id: 2,
order: 2,
name: 'total_female',
label: 'Total Female',
type: 'number',
},
],
},
];
const values = {
1: 2,
2: 3,
Expand All @@ -17,19 +45,19 @@ describe('TypeAutofield component', () => {
const id = 3;
const name = 'Auto Field';
const fn = {
fnString: '#1 * #2',
fnString: '#total_male# + #total_female#',
};

const { getByText, getByTestId } = render(
<TypeAutofield id={id} label={name} fn={fn} keyform={1} />,
<TypeAutofield id={id} label={name} fn={fn} keyform={1} questions={mockFormQuestions} />,
);

const autoFieldLabel = getByText(`1. ${name}`);
expect(autoFieldLabel).toBeDefined();

const autoField = getByTestId('type-autofield');
expect(autoField).toBeDefined();
expect(autoField.props.value).toBe('6');
expect(autoField.props.value).toBe('5');
});

test('it gives null value', () => {
Expand Down
49 changes: 40 additions & 9 deletions app/src/form/fields/__test__/TypeOption.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('TypeOption component', () => {
test('renders dropdown options correctly', async () => {
const setFieldValueMock = jest.fn();
const onChangeMock = jest.fn();
const values = {};
const values = null;
const option = [
{ name: 'option1', label: 'Option 1' },
{ name: 'option2', label: 'Option 2' },
Expand All @@ -32,6 +32,8 @@ describe('TypeOption component', () => {
label="Dropdown Field"
option={option}
setFieldValue={setFieldValueMock}
keyform={1}
required
/>,
);

Expand All @@ -41,11 +43,9 @@ describe('TypeOption component', () => {

it('should translate option dropdown text', async () => {
const setFieldValueMock = jest.fn();
const values = {};
const values = null;

const onChangeMock = jest.fn((fieldName, value) => {
values[fieldName] = value;
});
const onChangeMock = jest.fn();
const option = [
{
id: 1681108456316,
Expand Down Expand Up @@ -112,6 +112,8 @@ describe('TypeOption component', () => {
setFieldValue={setFieldValueMock}
id="maindrinking"
label="What is the main drinking water point provided by the school?"
keyform={1}
required
/>,
);

Expand Down Expand Up @@ -139,30 +141,59 @@ describe('TypeOption component', () => {
});

it('should not show required sign if required param is false and requiredSign is not defined', () => {
const wrapper = render(<TypeOption id="dropdownField" label="Dropdown" required={false} />);
const wrapper = render(
<TypeOption
id="dropdownField"
label="Dropdown"
required={false}
onChange={jest.fn()}
keyform={1}
/>,
);
const requiredIcon = wrapper.queryByTestId('field-required-icon');
expect(requiredIcon).toBeFalsy();
});

it('should not show required sign if required param is false but requiredSign is defined', () => {
const wrapper = render(
<TypeOption id="dropdownField" label="Dropdown" required={false} requiredSign="*" />,
<TypeOption
id="dropdownField"
label="Dropdown"
required={false}
requiredSign="*"
onChange={jest.fn()}
keyform={1}
/>,
);
const requiredIcon = wrapper.queryByTestId('field-required-icon');
expect(requiredIcon).toBeFalsy();
});

it('should not show required sign if required param is true and requiredSign defined', () => {
const wrapper = render(
<TypeOption id="dropdownField" label="Dropdown" required requiredSign="*" />,
<TypeOption
id="dropdownField"
label="Dropdown"
required
requiredSign="*"
onChange={jest.fn()}
keyform={1}
/>,
);
const requiredIcon = wrapper.queryByTestId('field-required-icon');
expect(requiredIcon).toBeTruthy();
});

it('should show required sign with custom requiredSign', () => {
const wrapper = render(
<TypeOption id="dropdownField" label="Dropdown" required requiredSign="**" />,
<TypeOption
id="dropdownField"
label="Dropdown"
required
requiredSign="**"
onChange={jest.fn()}
keyform={1}
/>,
);
const requiredIcon = wrapper.getByText('**');
expect(requiredIcon).toBeTruthy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ exports[`AuthFormPage renders correctly 1`] = `
}
>
App version -
4.0.2
4.0.9
</Text>
<Modal
hardwareAccelerated={false}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@


def to_snake_case(s):
if not s:
return s
# Remove special characters (except underscore and space)
s = re.sub(r'[^\w\s]', '', s)
# Replace whitespace and hyphens with underscores
Expand Down
Loading

0 comments on commit be620ee

Please sign in to comment.