-
Notifications
You must be signed in to change notification settings - Fork 228
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
(test) Remove unnecessary partial mocks; improve typing in tests #1933
Conversation
Size Change: -56 B (0%) Total Size: 11.1 MB ℹ️ View Unchanged
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks @denniskigen .
I wonder if we can make a nice pattern that ensures that typescript or the test breaks if mockUseConfig.mockReturnValue
is set to something incompatible with the config type. Maybe like
mockUseConfig.mockReturnValue({
concepts: {
pulseUuid: '5087AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
},
} as Partial<ConfigObject>);
?
@brandones if we want strict conformance with the source function's type definitions, we can do something like: import type { ConfigObject } from './config-schema';
const mockUseConfig = jest.mocked<() => ConfigObject>(useConfig); And then by default, TypeScript will expect you to provide an object that fully satisfies the contract as the mock's return value: beforeEach(() => {
mockUseConfig.mockReturnValue({
biometrics: { bmiUnit: 'kg / m²', heightUnit: 'm', weightUnit: 'kg' },
concepts: {
diastolicBloodPressureUuid: '5086AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
heightUuid: '5090AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
midUpperArmCircumferenceUuid: '1343AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
oxygenSaturationUuid: '5092AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
pulseUuid: '5087AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
respiratoryRateUuid: '5242AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
systolicBloodPressureUuid: '5085AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
temperatureUuid: '5088AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
weightUuid: '5089AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
},
vitals: {
encounterTypeUuid: '67a71486-1a54-468f-ac3e-7091a9a79584',
formUuid: 'a000cb34-9ec1-4344-a1c8-f692232f6edd',
useFormEngine: false,
useMuacColors: false,
formName: 'Vitals',
showPrintButton: false,
},
});
}); If we want to satisfy the type contract with a partial config, we could then do something like: beforeEach(() => {
mockUseConfig.mockReturnValue({
concepts: {
pulseUuid: false,
},
} as ConfigObject);
}); This will fail type checking because the |
Yeah I think the latter is reasonable. I wonder if it also works well with beforeEach(() => {
mockUseConfig.mockReturnValue({
...getDefaultsFromConfigSchema(schema),
concepts: {
pulseUuid: "asdfasdfasdf",
},
} as ConfigObject);
}); which IMO may be the best way to do config overrides |
It does. I'd forgotten about The added benefit is that you don't need to typecast your partial config mock using |
4e6276e
to
cb434e8
Compare
@@ -38,9 +44,13 @@ jest.mock('../common', () => ({ | |||
})), | |||
})); | |||
|
|||
mockUseConfig.mockReturnValue({ | |||
...getDefaultsFromConfigSchema(configSchema), | |||
mockVitalsConfig, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like it would create a key called mockVitalsConfig
on the object, which I would really hope would produce a type error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in the latest push, I think.
8b49498
to
02f59d6
Compare
02f59d6
to
edac26d
Compare
I'm sorry for blowing the blast radius of this PR a fair bit after review. The main motivators are:
|
@@ -1,42 +1,91 @@ | |||
import { Type } from '@openmrs/esm-framework'; | |||
|
|||
export const esmPatientChartSchema = { | |||
defaultFacilityUrl: { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yesss thank you
Requirements
Summary
This PR refactors various tests in the Patient Chart to fix the issue of unnecessary partial mocks. The mock.tsx manual mock in Core aggregates stubs for most functions in Core. We should only extend the manual mock if we want to include stubs for things that exist in Core, but are not included in the manual mock. Otherwise, we should extend the stubs already provided by the manual mock. This PR enables just that.
Screenshots
Related Issue
Other
Documentation updates will follow shortly.