-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Related to #1102
- Loading branch information
Showing
4 changed files
with
217 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
packages/form-js-viewer/test/spec/render/components/form-fields/JSFunctionField.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
import { | ||
render | ||
} from '@testing-library/preact/pure'; | ||
|
||
import { JSFunctionField } from '../../../../../src/render/components/form-fields/JSFunctionField'; | ||
|
||
import { MockFormContext } from '../helper'; | ||
|
||
import { act } from 'preact/test-utils'; | ||
|
||
import { | ||
createFormContainer | ||
} from '../../../../TestHelper'; | ||
|
||
let container; | ||
|
||
describe('JSFunctionField', function() { | ||
|
||
beforeEach(function() { | ||
container = createFormContainer(); | ||
}); | ||
|
||
|
||
afterEach(function() { | ||
container.remove(); | ||
}); | ||
|
||
|
||
it('should evaluate with setValue', async function() { | ||
|
||
// given | ||
const onChangeSpy = sinon.spy(); | ||
const field = defaultField; | ||
const passedData = { value : 42 }; | ||
|
||
const services = { | ||
expressionLanguage: { | ||
isExpression: () => true, | ||
evaluate: () => { | ||
return passedData; | ||
} | ||
} | ||
}; | ||
|
||
// when | ||
act(() => { | ||
createJSFunctionField({ field, onChange: onChangeSpy, services }); | ||
}); | ||
|
||
// wait for the iframe to compute the expression and pass it back | ||
await new Promise(r => setTimeout(r, 100)).then(() => { | ||
|
||
// then | ||
expect(onChangeSpy).to.be.calledOnce; | ||
expect(onChangeSpy).to.be.calledWith({ field, value: 42 }); | ||
}); | ||
|
||
}); | ||
|
||
|
||
it('should evaluate with return', async function() { | ||
|
||
// given | ||
const onChangeSpy = sinon.spy(); | ||
const field = { | ||
...defaultField, | ||
jsFunction: 'return data.value' | ||
}; | ||
const passedData = { value : 42 }; | ||
|
||
const services = { | ||
expressionLanguage: { | ||
isExpression: () => true, | ||
evaluate: () => { | ||
return passedData; | ||
} | ||
} | ||
}; | ||
|
||
// when | ||
act(() => { | ||
createJSFunctionField({ field, onChange: onChangeSpy, services }); | ||
}); | ||
|
||
// wait for the iframe to compute the expression and pass it back | ||
await new Promise(r => setTimeout(r, 100)).then(() => { | ||
|
||
// then | ||
expect(onChangeSpy).to.be.calledOnce; | ||
expect(onChangeSpy).to.be.calledWith({ field, value: 42 }); | ||
}); | ||
|
||
}); | ||
|
||
|
||
it('should evaluate multiple times when using interval', async function() { | ||
|
||
// given | ||
const onChangeSpy = sinon.spy(); | ||
const field = { | ||
...defaultField, | ||
computeOn: 'interval', | ||
interval: 100 | ||
}; | ||
const passedData = { value : 42 }; | ||
|
||
const services = { | ||
expressionLanguage: { | ||
isExpression: () => true, | ||
evaluate: () => { | ||
return passedData; | ||
} | ||
} | ||
}; | ||
|
||
// when | ||
act(() => { | ||
createJSFunctionField({ field, onChange: onChangeSpy, services }); | ||
}); | ||
|
||
// wait for the iframe to compute the expression and pass it back | ||
await new Promise(r => setTimeout(r, 500)).then(() => { | ||
|
||
// then | ||
|
||
// deliberately underestimating the number of calls to account for potential timing issues | ||
expect(onChangeSpy.callCount > 3).to.be.true; | ||
expect(onChangeSpy).to.be.calledWith({ field, value: 42 }); | ||
}); | ||
|
||
|
||
}); | ||
|
||
}); | ||
|
||
// helpers ////////// | ||
|
||
const defaultField = { | ||
type: 'script', | ||
key: 'jsfunction', | ||
jsFunction: 'setValue(data.value)', | ||
computeOn: 'load' | ||
}; | ||
|
||
function createJSFunctionField({ services, ...restOptions } = {}) { | ||
const options = { | ||
field: defaultField, | ||
onChange: () => {}, | ||
...restOptions | ||
}; | ||
|
||
return render( | ||
<MockFormContext | ||
services={ services } | ||
options={ options }> | ||
<JSFunctionField | ||
field={ options.field } | ||
onChange={ options.onChange } /> | ||
</MockFormContext>, { | ||
container: options.container || container.querySelector('.fjs-form') | ||
} | ||
); | ||
} |