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

Update numeric question validity checks #2027

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1451,4 +1451,60 @@ describe('VisualEditor', () => {
expect(spy).not.toHaveBeenCalled()
expect(instance.state.saveState).toBe('')
})

test('PageEditor component doesnt save if any input field is invalid', () => {
const props = { model: { title: '' } }
const mockFn = jest.fn()
const spy = jest.spyOn(VisualEditor.prototype, 'exportCurrentToJSON')
const hasInvalidFieldsSpy = jest.spyOn(VisualEditor.prototype, 'hasInvalidFields')

const input = document.createElement('input')
input.reportValidity = jest.fn().mockReturnValue(false)
document.body.appendChild(input)

const component = mount(<VisualEditor {...props} />)
const instance = component.instance()

instance.markUnsaved()
instance.saveModule('mockId')

// eslint-disable-next-line no-undefined
expect(instance.checkIfSaved(mockFn)).toBe(undefined)
expect(spy).not.toHaveBeenCalled()
expect(hasInvalidFieldsSpy).toHaveBeenCalled()
expect(instance.state.saveState).toBe('')

document.body.removeChild(input)
})

test('PageEditor component saves if all input fields are valid', () => {
const props = {
model: {
title: '',
flatJSON: jest.fn().mockReturnValue({ content: {} }),
children: []
},
saveDraft: jest.fn().mockResolvedValue(true)
}
const spy = jest
.spyOn(VisualEditor.prototype, 'exportCurrentToJSON')
.mockImplementation(() => {})
const hasInvalidFieldsSpy = jest.spyOn(VisualEditor.prototype, 'hasInvalidFields')

const input = document.createElement('input')
input.reportValidity = jest.fn().mockReturnValue(true)
document.body.appendChild(input)

const component = mount(<VisualEditor {...props} />)
const instance = component.instance()

instance.markUnsaved()
instance.saveModule('mockId')

expect(hasInvalidFieldsSpy).toHaveBeenCalled()
expect(spy).toHaveBeenCalled()
expect(instance.state.saveState).toBe('saving')

document.body.removeChild(input)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class VisualEditor extends React.Component {
this.renderElement = this.renderElement.bind(this)
this.setEditorFocus = this.setEditorFocus.bind(this)
this.onClick = this.onClick.bind(this)
this.hasInvalidFields = this.hasInvalidFields.bind(this)

this.editor = this.withPlugins(withHistory(withReact(createEditor())))
this.editor.toggleEditable = this.toggleEditable
Expand Down Expand Up @@ -195,10 +196,17 @@ class VisualEditor extends React.Component {
//eslint-disable-next-line
return undefined // Returning undefined will allow browser to close normally
}

if (this.hasInvalidFields()) {
//eslint-disable-next-line
return undefined
}

if (this.state.saveState !== 'saveSuccessful') {
event.returnValue = true
return true // Returning true will cause browser to ask user to confirm leaving page
}

//eslint-disable-next-line
return undefined
}
Expand Down Expand Up @@ -355,11 +363,32 @@ class VisualEditor extends React.Component {
this.saveModule(this.props.draftId)
}

// Checks all input and select fields to see if any have false validity
hasInvalidFields() {
const inputFields = document.querySelectorAll('input,select')

for (const field of Array.from(inputFields)) {
field.blur()
if (field.reportValidity() === false) {
return true
}
}

return false
}

saveModule(draftId) {
if (this.props.readOnly) {
return
}

// Check validity of all input and select elements
if (this.hasInvalidFields()) {
const message = 'Cannot save this module while errors are present'
window.alert(message) //eslint-disable-line no-alert
return
}

this.exportCurrentToJSON()
const json = this.props.model.flatJSON()
json.content.start = EditorStore.state.startingId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ exports[`NumericAnswer Editor Node NumericAnswer (requirement="margin",type="abs
<select
className="select-item"
name="margin-type"
onBlur={[Function]}
onChange={[Function]}
value="Absolute"
>
Expand Down Expand Up @@ -458,6 +459,7 @@ exports[`NumericAnswer Editor Node NumericAnswer (requirement="margin",type="abs
<select
className="select-item"
name="margin-type"
onBlur={[Function]}
onChange={[Function]}
value="Absolute"
>
Expand Down Expand Up @@ -537,6 +539,7 @@ exports[`NumericAnswer Editor Node NumericAnswer (requirement="margin",type="abs
<select
className="select-item"
name="margin-type"
onBlur={[Function]}
onChange={[Function]}
value="Absolute"
>
Expand Down Expand Up @@ -616,6 +619,7 @@ exports[`NumericAnswer Editor Node NumericAnswer (requirement="margin",type="abs
<select
className="select-item"
name="margin-type"
onBlur={[Function]}
onChange={[Function]}
value="Absolute"
>
Expand Down Expand Up @@ -695,6 +699,7 @@ exports[`NumericAnswer Editor Node NumericAnswer (requirement="margin",type="abs
<select
className="select-item"
name="margin-type"
onBlur={[Function]}
onChange={[Function]}
value="Absolute"
>
Expand Down Expand Up @@ -774,6 +779,7 @@ exports[`NumericAnswer Editor Node NumericAnswer (requirement="margin",type="abs
<select
className="select-item"
name="margin-type"
onBlur={[Function]}
onChange={[Function]}
value="Absolute"
>
Expand Down Expand Up @@ -853,6 +859,7 @@ exports[`NumericAnswer Editor Node NumericAnswer (requirement="margin",type="abs
<select
className="select-item"
name="margin-type"
onBlur={[Function]}
onChange={[Function]}
value="Absolute"
>
Expand Down Expand Up @@ -932,6 +939,7 @@ exports[`NumericAnswer Editor Node NumericAnswer (requirement="margin",type="per
<select
className="select-item"
name="margin-type"
onBlur={[Function]}
onChange={[Function]}
value="Percent"
>
Expand Down Expand Up @@ -1011,6 +1019,7 @@ exports[`NumericAnswer Editor Node NumericAnswer (requirement="margin",type="per
<select
className="select-item"
name="margin-type"
onBlur={[Function]}
onChange={[Function]}
value="Percent"
>
Expand Down Expand Up @@ -1090,6 +1099,7 @@ exports[`NumericAnswer Editor Node NumericAnswer (requirement="margin",type="per
<select
className="select-item"
name="margin-type"
onBlur={[Function]}
onChange={[Function]}
value="Percent"
>
Expand Down Expand Up @@ -1169,6 +1179,7 @@ exports[`NumericAnswer Editor Node NumericAnswer (requirement="margin",type="per
<select
className="select-item"
name="margin-type"
onBlur={[Function]}
onChange={[Function]}
value="Percent"
>
Expand Down Expand Up @@ -1248,6 +1259,7 @@ exports[`NumericAnswer Editor Node NumericAnswer (requirement="margin",type="per
<select
className="select-item"
name="margin-type"
onBlur={[Function]}
onChange={[Function]}
value="Percent"
>
Expand Down Expand Up @@ -1327,6 +1339,7 @@ exports[`NumericAnswer Editor Node NumericAnswer (requirement="margin",type="per
<select
className="select-item"
name="margin-type"
onBlur={[Function]}
onChange={[Function]}
value="Percent"
>
Expand Down Expand Up @@ -1406,6 +1419,7 @@ exports[`NumericAnswer Editor Node NumericAnswer (requirement="margin",type="per
<select
className="select-item"
name="margin-type"
onBlur={[Function]}
onChange={[Function]}
value="Percent"
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ exports[`NumericOption NumericOption renders as expected with requirement of \`m
<select
className="select-item"
name="requirement"
onChange={[Function]}
value="Margin of error"
>
<option>
Expand Down Expand Up @@ -126,6 +127,7 @@ exports[`NumericOption NumericOption renders as expected with requirement of \`m
<select
className="select-item"
name="margin-type"
onBlur={[Function]}
>
<option>
Percent
Expand Down Expand Up @@ -163,6 +165,7 @@ exports[`NumericOption NumericOption renders as expected with requirement of \`r
<select
className="select-item"
name="requirement"
onChange={[Function]}
value="Within a range"
>
<option>
Expand Down
Loading