Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Skaiir committed Nov 23, 2023
1 parent 3017a1a commit df8363e
Show file tree
Hide file tree
Showing 2 changed files with 246 additions and 0 deletions.
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 <></>;
}
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 <></>;
}

0 comments on commit df8363e

Please sign in to comment.