Skip to content

Commit

Permalink
test(de aggregation type field): cover with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammer5 committed Dec 11, 2023
1 parent a66b380 commit 48ad7ae
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 67 deletions.
80 changes: 80 additions & 0 deletions src/pages/dataElements/fields/AggregationTypeField.test.tsx
Original file line number Diff line number Diff line change
@@ -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('<AggregationTypeField />', () => {
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(
<Form onSubmit={jest.fn()} initialValues={initialValues}>
{({ handleSubmit }) => (
<form onSubmit={handleSubmit}>
<AggregationTypeField />
</form>
)}
</Form>
)

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()
}
})
}
)
})
9 changes: 3 additions & 6 deletions src/pages/dataElements/fields/AggregationTypeField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
Expand Down
1 change: 1 addition & 0 deletions src/pages/dataElements/fields/CategoryComboField.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ describe('<CategoryComboField />', () => {
),
},
])

const result = render(
<ComponentWithProvider dataForCustomProvider={customData}>
<RouterProvider router={router} />
Expand Down
75 changes: 14 additions & 61 deletions src/pages/dataElements/fields/ValueTypeField.test.tsx
Original file line number Diff line number Diff line change
@@ -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', () => ({
Expand All @@ -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('<ValueTypeField />', () => {
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,
Expand Down Expand Up @@ -104,15 +74,6 @@ describe('<ValueTypeField />', () => {
})

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,
Expand Down Expand Up @@ -155,14 +116,6 @@ describe('<ValueTypeField />', () => {
})

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,
Expand Down

0 comments on commit 48ad7ae

Please sign in to comment.