Skip to content

Commit

Permalink
unit test refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
hassanad94 committed Nov 29, 2023
1 parent a38e2e6 commit c871991
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
14 changes: 8 additions & 6 deletions packages/sn-controls-react/src/fieldcontrols/html-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ import { ReactClientFieldSetting } from './client-field-setting'
export const HtmlEditor: React.FC<
ReactClientFieldSetting & {
theme?: Theme
setValue?: (value: string) => void
}
> = (props) => {
const initialState =
props.fieldValue || (props.actionName === 'new' && changeTemplatedValue(props.settings.DefaultValue)) || ''
const [value, setValue] = useState(initialState)
props.setValue?.(initialState)

const editorRef = useRef<MonacoEditor>(null)
const containerRef = useRef<HTMLDivElement>(null)
Expand All @@ -35,6 +36,10 @@ export const HtmlEditor: React.FC<
}, [editorRef])
const readonly = props.actionName === 'browse' || props.settings.ReadOnly

const editorChangeHandler = (newValue: string) => {
props.fieldOnChange?.(props.settings.Name, newValue)
}

return (
<>
<InputLabel shrink>{props.settings.DisplayName}</InputLabel>
Expand All @@ -45,11 +50,8 @@ export const HtmlEditor: React.FC<
{...props}
width="100%"
height="100%"
value={value}
onChange={(v) => {
setValue(v)
props.fieldOnChange?.(props.settings.Name, v)
}}
value={props.fieldValue}
onChange={editorChangeHandler}
options={{
automaticLayout: true,
contextmenu: true,
Expand Down
8 changes: 5 additions & 3 deletions packages/sn-controls-react/test/html-editor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { defaultLocalization, HtmlEditor } from '../src/fieldcontrols'
jest.mock('react-monaco-editor', () =>
jest.fn((props) => {
return (
<div data-test="mock-monaco-editor" onChange={props.onChange}>
<div data-test="mock-monaco-editor" onChange={props.fieldOnChange}>
{props.value}
</div>
)
Expand All @@ -32,7 +32,7 @@ describe('Html Editor', () => {
localization: defaultLocalization,
fieldValue: '<p>Test</p>',

onChange,
fieldOnChange: onChange,
}

const wrapper = mount(<HtmlEditor {...props} />)
Expand All @@ -49,6 +49,8 @@ describe('Html Editor', () => {
mockMonacoEditor.prop('onChange')?.('<p>Changed Test</p>' as any)
})

expect(htmlEditorContainer.text()).toBe('<p>Changed Test</p>')
//should be called with onChange

expect(props.fieldOnChange).toBeCalledWith('<p>Changed Test</p>')
})
})

0 comments on commit c871991

Please sign in to comment.