Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure fields do not have undefined initial values #3246

Conversation

HendrikThePendric
Copy link
Contributor

@HendrikThePendric HendrikThePendric commented Mar 20, 2025

Implements DHIS2-19266


Key features

  1. When creating the initial state, ensure the values and initialValues do not contain properties that are undefined

Description

The error we were seeing when opening the modal was caused by the fact that a dashboard did not have a code. So the initialValues object provided to createInitialState was not undefined itself, but one of its properties did have a value of undefined. The validation logic was assuming each property would default to the value defined in the defaultInitialValues object, but this was the case.

In this fix I chose to implement a generic fix that scans all initialValues properties and if an undefined value is encountered, it falls back to the default. IMO this preferable over making the validation logic more flexible, or implementing a fix specific to the code field.


@dhis2-bot
Copy link
Contributor

dhis2-bot commented Mar 20, 2025

🚀 Deployed on https://pr-3246.dashboard.netlify.dhis2.org

@dhis2-bot dhis2-bot temporarily deployed to netlify March 20, 2025 16:37 Inactive
@dhis2-bot dhis2-bot temporarily deployed to netlify March 20, 2025 16:40 Inactive
@jenniferarnesen
Copy link
Collaborator

So, I'm a follower of the idea of only testing the intended public interface of a module (hook in this case) rather than testing implementation details (e.g. isValidUuid, createInitialState). This keeps code cleaner and prevents unintended uses of those "internal" functions. It also makes it easier to identify when a constant or function is not in use and to refactor the implementation. As far as testing goes, in the case of the current tests, it is really easy to do these tests just by testing the hook:

      it('creates the expected initial state when some properties are undefined', () => {
            const initialValues = {
                title: undefined,
                code: undefined,
                description: undefined,
                supersetEmbedId: undefined,
                showChartControls: undefined,
                expandFilters: undefined,
            }
            const { result } = renderHook(() =>
                useSupersetDashboardFieldsState(initialValues)
            )

            expect(result.current.values).toEqual(defaultInitialValues)
            expect(result.current.isSupersetEmbedIdValid).toBe(false)
            expect(result.current.isCodeValid).toBe(true)
            expect(result.current.shouldShowSupersetEmbedIdError).toBe(false)
            expect(result.current.hasFieldChanges).toBe(false)
      })

@HendrikThePendric
Copy link
Contributor Author

So, I'm a follower of the idea of only testing the intended public interface of a module (hook in this case) rather than testing implementation details (e.g. isValidUuid, createInitialState). This keeps code cleaner and prevents unintended uses of those "internal" functions. It also makes it easier to identify when a constant or function is not in use and to refactor the implementation.

This is interesting. Up until now I have mostly given preference to testing the smallest unit in isolation when writing unit tests. And while I do think that in general this is a sensible notion, and that this will actually result in the cleanest code, I do see your other points and will start writing unit tests against the intended public interface in the future. My current approach does involve exporting these internals and I have seen the problems you mention happen in practice.

I'll proceed to update the entire spec file.

@dhis2-bot dhis2-bot temporarily deployed to netlify March 24, 2025 15:08 Inactive
@dhis2-bot dhis2-bot temporarily deployed to netlify March 25, 2025 09:04 Inactive
Copy link
Collaborator

@jenniferarnesen jenniferarnesen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add the "Untitled dashboard" placeholder to the title, since that's what it gets set to if the user doesn't type anything. This is also in line with regular dashboard creation.

@HendrikThePendric
Copy link
Contributor Author

Can you add the "Untitled dashboard" placeholder to the title, since that's what it gets set to if the user doesn't type anything. This is also in line with regular dashboard creation.

OK, done 0109115

@dhis2-bot dhis2-bot temporarily deployed to netlify March 25, 2025 14:25 Inactive
@HendrikThePendric HendrikThePendric merged commit d63eaa6 into master Mar 26, 2025
40 checks passed
@HendrikThePendric HendrikThePendric deleted the fix/allow-opening-embedded-dashboard-update-modal-DHIS2-19266 branch March 26, 2025 11:20
dhis2-bot added a commit that referenced this pull request Mar 26, 2025
## [101.1.4](v101.1.3...v101.1.4) (2025-03-26)

### Bug Fixes

* Ensure external dashboard fields do not have undefined initial values ([#3246](#3246)) ([d63eaa6](d63eaa6))
@dhis2-bot
Copy link
Contributor

🎉 This PR is included in version 101.1.4 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants