Skip to content

Commit 70a2f5f

Browse files
authored
fix(shared): base initial disabled state on sdk.field.getIsDisabled() [TOL-2914] (#1852)
1 parent 362b224 commit 70a2f5f

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

packages/_shared/src/FieldConnector.test.tsx

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import noop from 'lodash/noop';
66

77
import { FieldConnector, FieldConnectorChildProps } from './FieldConnector';
88

9-
it('does not rerender with outdated value after calling setValue', async () => {
10-
function getChild(): FieldConnectorChildProps<any> {
11-
return props.children.mock.calls[props.children.mock.calls.length - 1][0];
12-
}
9+
function getChild(children: any): FieldConnectorChildProps<any> {
10+
return children.mock.calls[children.mock.calls.length - 1][0];
11+
}
1312

13+
it('does not rerender with outdated value after calling setValue', async () => {
1414
const onSchemaErrorsChanged = jest.fn();
1515
const [field] = createFakeFieldAPI((field: any) => {
1616
return {
@@ -30,7 +30,7 @@ it('does not rerender with outdated value after calling setValue', async () => {
3030

3131
render(<FieldConnector {...props} />);
3232

33-
let child = getChild();
33+
let child = getChild(props.children);
3434
expect(child.value).toBe('initial value');
3535
const initialRenderCount = props.children.mock.calls.length;
3636

@@ -40,9 +40,40 @@ it('does not rerender with outdated value after calling setValue', async () => {
4040

4141
onSchemaErrorsChanged.mock.calls.forEach(([cb]) => cb([]));
4242

43-
child = getChild();
43+
child = getChild(props.children);
4444
expect(child.value).toBe('new value');
4545

4646
// to ensure that there was actually a rerender after calling `setValue` as we want to test that we don't rerender with outdated data
4747
expect(props.children.mock.calls.length).toBeGreaterThan(initialRenderCount);
4848
});
49+
50+
it('takes initial disable state from sdk.field', () => {
51+
const [field] = createFakeFieldAPI((field: any) => {
52+
return {
53+
...field,
54+
// this promise never resolves
55+
getIsDisabled: jest.fn().mockReturnValue(true),
56+
};
57+
}, 'initial value');
58+
59+
const props = {
60+
isInitiallyDisabled: false,
61+
children: jest.fn().mockImplementation(() => null),
62+
field,
63+
debounce: 0,
64+
};
65+
66+
render(<FieldConnector {...props} />);
67+
68+
const child = getChild(props.children);
69+
expect(child.value).toBe('initial value');
70+
71+
expect(field.getIsDisabled).toHaveBeenCalled();
72+
73+
expect(props.children).toHaveBeenCalledTimes(1);
74+
expect(props.children).toHaveBeenCalledWith(
75+
expect.objectContaining({
76+
disabled: true,
77+
})
78+
);
79+
});

packages/_shared/src/FieldConnector.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export class FieldConnector<ValueType> extends React.Component<
6767
externalReset: 0,
6868
value: initialValue,
6969
lastRemoteValue: initialValue,
70-
disabled: props.isInitiallyDisabled ?? false,
70+
disabled: props.isInitiallyDisabled || props.field.getIsDisabled(),
7171
errors: [],
7272
};
7373
}

packages/reference/src/resources/testHelpers/resourceEditorHelpers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export function mockSdkForField(fieldDefinition: any, fieldValue?: any): FieldAp
1010
onSchemaErrorsChanged: () => {},
1111
// eslint-disable-next-line -- test helper
1212
onIsDisabledChanged: () => {},
13+
getIsDisabled: () => false,
1314
// eslint-disable-next-line -- test helper
1415
onValueChanged: () => {},
1516
...fieldDefinition,

0 commit comments

Comments
 (0)