diff --git a/src/pages/dataElements/fields/AggregationTypeField.test.tsx b/src/pages/dataElements/fields/AggregationTypeField.test.tsx new file mode 100644 index 00000000..2696adc6 --- /dev/null +++ b/src/pages/dataElements/fields/AggregationTypeField.test.tsx @@ -0,0 +1,80 @@ +import { render } from '@testing-library/react' +import React from 'react' +import { Form } from 'react-final-form' +import { AGGREGATION_TYPE, useSchemas } from '../../../lib' +import { AggregationTypeField } from './AggregationTypeField' + +const aggregationTypes = Object.keys(AGGREGATION_TYPE) + +jest.mock('../../../lib/schemas/schemaStore', () => { + return { + ...jest.requireActual('../../../lib/schemas/schemaStore'), + useSchemas: jest.fn(), + } +}) + +describe('', () => { + const uS = useSchemas as jest.Mock + uS.mockImplementation(() => ({ + dataElement: { + properties: { + aggregationType: { constants: aggregationTypes }, + }, + }, + })) + + const disabledValueTypes = [ + 'TEXT', + 'LONG_TEXT', + 'MULTI_TEXT', + 'LETTER', + 'PHONE_NUMBER', + 'EMAIL', + 'TRACKER_ASSOCIATE', + 'USERNAME', + 'FILE_RESOURCE', + 'COORDINATE', + ] + const enabledValueTypes = aggregationTypes.filter( + (aggregationType) => !disabledValueTypes.includes(aggregationType) + ) + const disabelValueTypes = [ + ...enabledValueTypes.map((aggregationType) => [aggregationType, false]), + ...disabledValueTypes.map((aggregationType) => [aggregationType, true]), + ] + describe.each(disabelValueTypes)( + 'disabled should be $disabled for $aggregationType', + (valueType, disabled) => { + test(`should be ${ + disabled ? 'disabled' : 'enabled' + } for valueType ${valueType}`, async () => { + const initialValues = { + valueType, + aggregationType: 'SUM', + } + + const result = render( +
+ {({ handleSubmit }) => ( + + + + )} + + ) + + const input = await result.findByTestId( + 'dhis2-uicore-select-input' + ) + expect(input.classList.contains('disabled')).toBe(disabled) + + const sumElement = result.queryByText('Sum') + if (disabled) { + expect(sumElement).toBeFalsy() + } else { + expect(sumElement).toBeTruthy() + } + }) + } + ) +}) diff --git a/src/pages/dataElements/fields/AggregationTypeField.tsx b/src/pages/dataElements/fields/AggregationTypeField.tsx index 556c9c01..05ec9e53 100644 --- a/src/pages/dataElements/fields/AggregationTypeField.tsx +++ b/src/pages/dataElements/fields/AggregationTypeField.tsx @@ -5,12 +5,9 @@ import { Field as FieldRFF, useForm, useFormState } from 'react-final-form' import { AGGREGATION_TYPE, required, useSchemas } from '../../../lib' /** - * - * Field rule: When value type has a certain value, disable aggregationType - * field - * Field rule: When value type has a certain value, disable aggregationType - * value to '' - * + * Field rule: When value type has a certain value, + * disable aggregationType field + * Field rule: When value type is disabled, set value to '' */ const aggregationTypeHelpText = i18n.t( 'The default way to aggregate this data element in analytics.' diff --git a/src/pages/dataElements/fields/CategoryComboField.test.tsx b/src/pages/dataElements/fields/CategoryComboField.test.tsx index a48cadfb..a28515db 100644 --- a/src/pages/dataElements/fields/CategoryComboField.test.tsx +++ b/src/pages/dataElements/fields/CategoryComboField.test.tsx @@ -35,6 +35,7 @@ describe('', () => { ), }, ]) + const result = render( diff --git a/src/pages/dataElements/fields/ValueTypeField.test.tsx b/src/pages/dataElements/fields/ValueTypeField.test.tsx index a21d03c3..29cb5376 100644 --- a/src/pages/dataElements/fields/ValueTypeField.test.tsx +++ b/src/pages/dataElements/fields/ValueTypeField.test.tsx @@ -1,7 +1,7 @@ import { render, fireEvent } from '@testing-library/react' import React from 'react' import { Form } from 'react-final-form' -import { useSchemas, useOptionSetQuery } from '../../../lib' +import { VALUE_TYPE, useSchemas, useOptionSetQuery } from '../../../lib' import { ValueTypeField } from './ValueTypeField' jest.mock('../../../lib/optionSet/useOptionSetQuery', () => ({ @@ -15,53 +15,23 @@ jest.mock('../../../lib/schemas/schemaStore', () => { } }) -// `(useOptionSetQuery as jest.Mock).mockImplementation` causes the code to be -// built wrongly and subsequently to bug out -const uOSQ = useOptionSetQuery as jest.Mock -const uS = useSchemas as jest.Mock - -const valueTypes = [ - 'TEXT', - 'LONG_TEXT', - 'MULTI_TEXT', - 'LETTER', - 'PHONE_NUMBER', - 'EMAIL', - 'BOOLEAN', - 'TRUE_ONLY', - 'DATE', - 'DATETIME', - 'TIME', - 'NUMBER', - 'UNIT_INTERVAL', - 'PERCENTAGE', - 'INTEGER', - 'INTEGER_POSITIVE', - 'INTEGER_NEGATIVE', - 'INTEGER_ZERO_OR_POSITIVE', - 'TRACKER_ASSOCIATE', - 'USERNAME', - 'COORDINATE', - 'ORGANISATION_UNIT', - 'REFERENCE', - 'AGE', - 'URL', - 'FILE_RESOURCE', - 'IMAGE', - 'GEOJSON', -] +const valueTypes = Object.keys(VALUE_TYPE) describe('', () => { - it('should not have the MULTI_TEXT option when a different value is selected', async () => { - const uS = useSchemas as jest.Mock - uS.mockImplementation(() => ({ - dataElement: { - properties: { - valueType: { constants: valueTypes }, - }, + // `(useOptionSetQuery as jest.Mock).mockImplementation` causes the code to be + // built wrongly and subsequently to bug out + const uOSQ = useOptionSetQuery as jest.Mock + const uS = useSchemas as jest.Mock + + uS.mockImplementation(() => ({ + dataElement: { + properties: { + valueType: { constants: valueTypes }, }, - })) + }, + })) + it('should not have the MULTI_TEXT option when a different value is selected', async () => { uOSQ.mockImplementation(() => ({ called: false, loading: false, @@ -104,15 +74,6 @@ describe('', () => { }) it('should have the MULTI_TEXT option when the selected value is MULTI_TEXT', async () => { - const uS = useSchemas as jest.Mock - uS.mockImplementation(() => ({ - dataElement: { - properties: { - valueType: { constants: valueTypes }, - }, - }, - })) - const mockResult = { called: false, loading: false, @@ -155,14 +116,6 @@ describe('', () => { }) it("should have the MULTI_TEXT option auto-selected when the option set's valueType is MULTI_TEXT", async () => { - uS.mockImplementation(() => ({ - dataElement: { - properties: { - valueType: { constants: valueTypes }, - }, - }, - })) - const mockResult = { called: false, loading: false,