-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
246 additions
and
0 deletions.
There are no files selected for viewing
123 changes: 123 additions & 0 deletions
123
packages/form-js-viewer/test/spec/render/hooks/useCleanupMultiSelectValues.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,123 @@ | ||
import { | ||
render, | ||
} from '@testing-library/preact/pure'; | ||
|
||
import useCleanupMultiSelectValues from '../../../../src/render/hooks/useCleanupMultiSelectValues'; | ||
|
||
const spy = sinon.spy; | ||
let root; | ||
|
||
describe('useCleanupMultiSelectValue', function() { | ||
|
||
beforeEach(() => root = document.createElement('div')); | ||
|
||
afterEach(() => root.remove()); | ||
|
||
it('should fire onChange when any value is no longer in the options', function() { | ||
|
||
// given | ||
const onChangeSpy = spy(); | ||
const values = [ 'camunda-platform', 'camunda-cloud' ]; | ||
|
||
const { rerender } = render( | ||
<TestComponent onChangeSpy={ onChangeSpy } options={ baseOptions } values={ values } />, { | ||
container: root | ||
}); | ||
|
||
// when | ||
rerender( | ||
<TestComponent onChangeSpy={ onChangeSpy } options={ noPlatformOptions } values={ values } />, { | ||
container: root | ||
} | ||
); | ||
|
||
// then | ||
expect(onChangeSpy).to.have.been.calledOnce; | ||
expect(onChangeSpy).to.have.been.calledWith({ | ||
field: 'foo', | ||
value: [ 'camunda-cloud' ] | ||
}); | ||
|
||
}); | ||
|
||
|
||
it('should not fire onChange when all values are still in the options', function() { | ||
|
||
// given | ||
const onChangeSpy = spy(); | ||
const values = [ 'camunda-platform', 'camunda-cloud' ]; | ||
|
||
const { rerender } = render( | ||
<TestComponent onChangeSpy={ onChangeSpy } options={ baseOptions } values={ values } />, { | ||
container: root | ||
}); | ||
|
||
// when | ||
rerender( | ||
<TestComponent onChangeSpy={ onChangeSpy } options={ baseOptions } values={ values } />, { | ||
container: root | ||
} | ||
); | ||
|
||
// then | ||
expect(onChangeSpy).to.not.have.been.called; | ||
|
||
}); | ||
|
||
|
||
it('should not reinstate the removed values when the options change back', function() { | ||
|
||
// given | ||
const onChangeSpy = spy(); | ||
const values = [ 'camunda-platform', 'camunda-cloud' ]; | ||
|
||
const { rerender } = render( | ||
<TestComponent onChangeSpy={ onChangeSpy } options={ baseOptions } values={ values } />, { | ||
container: root | ||
}); | ||
|
||
// when | ||
rerender( | ||
<TestComponent onChangeSpy={ onChangeSpy } options={ noPlatformOptions } values={ values } />, { | ||
container: root | ||
} | ||
); | ||
|
||
rerender( | ||
<TestComponent onChangeSpy={ onChangeSpy } options={ baseOptions } values={ values } />, { | ||
container: root | ||
} | ||
); | ||
|
||
// then | ||
expect(onChangeSpy).to.have.been.calledOnce; | ||
expect(onChangeSpy).to.have.been.calledWith({ | ||
field: 'foo', | ||
value: [ 'camunda-cloud' ] | ||
}); | ||
|
||
}); | ||
|
||
}); | ||
|
||
const baseOptions = [ | ||
{ value: 'camunda-platform', label: 'Camunda Platform' }, | ||
{ value: 'camunda-cloud', label: 'Camunda Cloud' } | ||
]; | ||
|
||
const noPlatformOptions = [ | ||
{ value: 'camunda-cloud', label: 'Camunda Cloud' } | ||
]; | ||
|
||
function TestComponent({ onChangeSpy, options, values }) { | ||
|
||
useCleanupMultiSelectValues({ | ||
field: 'foo', | ||
loadState: 'loaded', | ||
onChange: onChangeSpy, | ||
options, | ||
values | ||
}); | ||
|
||
return <></>; | ||
} |
123 changes: 123 additions & 0 deletions
123
packages/form-js-viewer/test/spec/render/hooks/useCleanupSingleSelectValue.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,123 @@ | ||
import { | ||
render, | ||
} from '@testing-library/preact/pure'; | ||
|
||
import useCleanupSingleSelectValue from '../../../../src/render/hooks/useCleanupSingleSelectValue'; | ||
|
||
const spy = sinon.spy; | ||
let root; | ||
|
||
describe('useCleanupSingleSelectValue', function() { | ||
|
||
beforeEach(() => root = document.createElement('div')); | ||
|
||
afterEach(() => root.remove()); | ||
|
||
it('should fire onChange when the value is no longer in the options', function() { | ||
|
||
// given | ||
const onChangeSpy = spy(); | ||
const value = 'camunda-platform'; | ||
|
||
const { rerender } = render( | ||
<TestComponent onChangeSpy={ onChangeSpy } options={ baseOptions } value={ value } />, { | ||
container: root | ||
}); | ||
|
||
// when | ||
rerender( | ||
<TestComponent onChangeSpy={ onChangeSpy } options={ noPlatformOptions } value={ value } />, { | ||
container: root | ||
} | ||
); | ||
|
||
// then | ||
expect(onChangeSpy).to.have.been.calledOnce; | ||
expect(onChangeSpy).to.have.been.calledWith({ | ||
field: 'foo', | ||
value: null | ||
}); | ||
|
||
}); | ||
|
||
|
||
it('should not fire onChange when the value is still in the options', function() { | ||
|
||
// given | ||
const onChangeSpy = spy(); | ||
const value = 'camunda-platform'; | ||
|
||
const { rerender } = render( | ||
<TestComponent onChangeSpy={ onChangeSpy } options={ baseOptions } value={ value } />, { | ||
container: root | ||
}); | ||
|
||
// when | ||
rerender( | ||
<TestComponent onChangeSpy={ onChangeSpy } options={ baseOptions } value={ value } />, { | ||
container: root | ||
} | ||
); | ||
|
||
// then | ||
expect(onChangeSpy).to.not.have.been.called; | ||
|
||
}); | ||
|
||
|
||
it('should not reinstate the value when the options change back', function() { | ||
|
||
// given | ||
const onChangeSpy = spy(); | ||
const value = 'camunda-platform'; | ||
|
||
const { rerender } = render( | ||
<TestComponent onChangeSpy={ onChangeSpy } options={ baseOptions } value={ value } />, { | ||
container: root | ||
}); | ||
|
||
// when | ||
rerender( | ||
<TestComponent onChangeSpy={ onChangeSpy } options={ noPlatformOptions } value={ value } />, { | ||
container: root | ||
} | ||
); | ||
|
||
rerender( | ||
<TestComponent onChangeSpy={ onChangeSpy } options={ baseOptions } value={ value } />, { | ||
container: root | ||
} | ||
); | ||
|
||
// then | ||
expect(onChangeSpy).to.have.been.calledOnce; | ||
expect(onChangeSpy).to.have.been.calledWith({ | ||
field: 'foo', | ||
value: null | ||
}); | ||
|
||
}); | ||
|
||
}); | ||
|
||
const baseOptions = [ | ||
{ value: 'camunda-platform', label: 'Camunda Platform' }, | ||
{ value: 'camunda-cloud', label: 'Camunda Cloud' } | ||
]; | ||
|
||
const noPlatformOptions = [ | ||
{ value: 'camunda-cloud', label: 'Camunda Cloud' } | ||
]; | ||
|
||
function TestComponent({ onChangeSpy, options, value }) { | ||
|
||
useCleanupSingleSelectValue({ | ||
field: 'foo', | ||
loadState: 'loaded', | ||
onChange: onChangeSpy, | ||
options, | ||
value | ||
}); | ||
|
||
return <></>; | ||
} |