Skip to content

Commit

Permalink
Id and version should be optional
Browse files Browse the repository at this point in the history
*Fix tests
  • Loading branch information
krissvaa committed Jan 16, 2024
1 parent e63d427 commit 691762e
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class AbstractEntity {

@Version
@Nullable
private long version;
private Long version;

public Long getId() {
return id;
Expand All @@ -27,11 +27,11 @@ public void setId(Long id) {
this.id = id;
}

public long getVersion() {
public Long getVersion() {
return version;
}

public void setVersion(long version) {
public void setVersion(Long version) {
this.version = version;
}
}
5 changes: 3 additions & 2 deletions packages/ts/lit-form/src/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,9 @@ export const field = directive(
const { value } = binderNode;
const valueFromField = convertFieldValue(model, fieldState.value);
if (value !== valueFromField && !(Number.isNaN(value) && Number.isNaN(valueFromField))) {
fieldState.value = value;
fieldState.strategy.value = value;
const nonNanValue = Number.isNaN(value) ? '' : value;
fieldState.value = nonNanValue;
fieldState.strategy.value = nonNanValue;
}

const { required } = binderNode;
Expand Down
6 changes: 3 additions & 3 deletions packages/ts/lit-form/test/Binder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('@vaadin/hilla-lit-form', () => {
nickName: '',
},
notes: '',
priority: 0,
priority: NaN,
products: [],
total: undefined,
};
Expand Down Expand Up @@ -126,12 +126,12 @@ describe('@vaadin/hilla-lit-form', () => {

it('should have valueOf', () => {
assert.equal(binder.model.notes.valueOf(), '');
assert.equal(binder.model.priority.valueOf(), 0);
assert.isNaN(binder.model.priority.valueOf());
});

it('should have toString', () => {
assert.equal(binder.model.notes.valueOf(), '');
assert.equal(binder.model.priority.toString(), '0');
assert.equal(binder.model.priority.toString(), NaN.toString());
});

it('should have initial value', () => {
Expand Down
8 changes: 4 additions & 4 deletions packages/ts/lit-form/test/Field.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,8 @@ describe('@vaadin/hilla-lit-form', () => {
priorityField = view.priorityField!;
});

it('should set initial zero', () => {
expect(priorityField.value).to.equal('0');
it('should set initial empty', () => {
expect(priorityField.value).to.equal('');
});

it('should set number value from binder', async () => {
Expand Down Expand Up @@ -416,8 +416,8 @@ describe('@vaadin/hilla-lit-form', () => {
priorityField = view.priorityField!;
});

it('should set initial zero', () => {
expect(priorityField.value).to.equal('0');
it('should set initial empty', () => {
expect(priorityField.value).to.equal('');
});

it('should set number value from binder', async () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/ts/lit-form/test/Model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ describe('@vaadin/hilla-lit-form', () => {
expect(validators[0]).to.be.instanceOf(IsNumber);
});

it('should be NaN by the default', () => {
it('should be undefined by the default', () => {
const { value } = binder.for(binder.model.fieldNumber);
expect(value).to.be.NaN;
expect(value).to.be.undefined;
});

describe('_fromString', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/ts/lit-form/test/TestModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export class TestModel<T extends TestEntity = TestEntity> extends ObjectModel<T>
}

get fieldNumber(): NumberModel {
return this[_getPropertyModel]('fieldNumber', (parent, key) => new NumberModel(parent, key, false));
return this[_getPropertyModel]('fieldNumber', (parent, key) => new NumberModel(parent, key, true));
}

get fieldBoolean(): BooleanModel {
Expand Down
21 changes: 19 additions & 2 deletions packages/ts/lit-form/test/Validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ class OrderView extends LitElement {
products,
customer: { fullName, nickName },
total,
priority,
} = this.binder.model;
this.binder.for(priority).value = 0;

return html`
<input id="notes" ...="${field(notes)}" />
Expand Down Expand Up @@ -156,7 +158,7 @@ describe('@vaadin/hilla-lit-form', () => {

it('should run all nested validations per model', async () => {
const errors = await binder.validate();
expect(errors.map((e) => e.property)).to.eql(['customer.fullName', 'customer.fullName', 'notes']);
expect(errors.map((e) => e.property)).to.eql(['customer.fullName', 'customer.fullName', 'notes', 'priority']);
});

it('should run all validations per array items', async () => {
Expand All @@ -171,8 +173,10 @@ describe('@vaadin/hilla-lit-form', () => {
'notes',
'products.0.description',
'products.0.price',
'products.0.price',
'products.1.description',
'products.1.price',
'products.1.price',
]);
});

Expand All @@ -198,6 +202,7 @@ describe('@vaadin/hilla-lit-form', () => {
// do nothing
},
});
testBinder.for(testBinder.model.fieldNumber).value = 0;
const binderSubmitToSpy = sinon.spy(testBinder, 'submitTo');
await testBinder.submit();
sinon.assert.calledOnce(binderSubmitToSpy);
Expand All @@ -206,6 +211,7 @@ describe('@vaadin/hilla-lit-form', () => {
it('should return the result of the endpoint call when calling submit()', async () => {
// eslint-disable-next-line @typescript-eslint/require-await
const testBinder = new Binder(view, TestModel, { onSubmit: async (testEntity) => testEntity });
testBinder.for(testBinder.model.fieldNumber).value = 0;
const result = await testBinder.submit();
assert.deepEqual(result, testBinder.value);
});
Expand All @@ -214,6 +220,7 @@ describe('@vaadin/hilla-lit-form', () => {
const testEndpoint: (entity: TestEntity) => Promise<TestEntity | undefined> = async (_) =>
Promise.resolve(undefined);
const testBinder = new Binder(view, TestModel, { onSubmit: testEndpoint });
testBinder.for(testBinder.model.fieldNumber).value = 0;
const result = await testBinder.submit();
expect(result).to.be.undefined;
});
Expand All @@ -233,6 +240,7 @@ describe('@vaadin/hilla-lit-form', () => {
it('should re-throw on server failure', async () => {
binder.for(binder.model.customer.fullName).value = 'foobar';
binder.for(binder.model.notes).value = 'whatever';
binder.for(binder.model.priority).value = 0;
try {
await binder.submitTo(() => {
throw new Error('whatever');
Expand All @@ -247,6 +255,7 @@ describe('@vaadin/hilla-lit-form', () => {
it('should wrap server validation error', async () => {
binder.for(binder.model.customer.fullName).value = 'foobar';
binder.for(binder.model.notes).value = 'whatever';
binder.for(binder.model.priority).value = 0;
try {
await binder.submitTo(() => {
throw new EndpointValidationError("Validation error in endpoint 'MyEndpoint' method 'saveMyBean'", [
Expand All @@ -270,6 +279,7 @@ describe('@vaadin/hilla-lit-form', () => {
it('should wrap server validation error with any message', async () => {
binder.for(binder.model.customer.fullName).value = 'foobar';
binder.for(binder.model.notes).value = 'whatever';
binder.for(binder.model.priority).value = 0;
try {
await binder.submitTo(() => {
throw new EndpointValidationError("Validation error in endpoint 'MyEndpoint' method 'saveMyBean'", [
Expand Down Expand Up @@ -602,14 +612,16 @@ describe('@vaadin/hilla-lit-form', () => {
'notes',
'products.0.description',
'products.0.price',
'products.0.price',
'products.1.description',
'products.1.price',
'products.1.price',
]);
}

expect(orderView.description).to.have.attribute('invalid');
expect(orderView.price).to.have.attribute('invalid');
expect(String(orderView.priceError.textContent).trim()).to.equal('must be greater than 0');
expect(String(orderView.priceError.textContent).trim()).to.equal('must be a number\nmust be greater than 0');
});

it(`should validate fields of arrays on submit`, async () => {
Expand Down Expand Up @@ -664,6 +676,8 @@ describe('@vaadin/hilla-lit-form', () => {
await fireEvent(orderView.description, 'change');
orderView.price.value = '10';
await fireEvent(orderView.price, 'change');
orderView.total.value = '10';
await fireEvent(orderView.total, 'change');

// eslint-disable-next-line @typescript-eslint/require-await
const item = await orderView.binder.submitTo(async (order) => order);
Expand All @@ -677,6 +691,7 @@ describe('@vaadin/hilla-lit-form', () => {
it('should display server validation error', async () => {
binder.for(binder.model.customer.fullName).value = 'foobar';
binder.for(binder.model.notes).value = 'whatever';
binder.for(binder.model.priority).value = 0;
const requestUpdateSpy = sinon.spy(orderView, 'requestUpdate');
try {
await binder.submitTo(() => {
Expand All @@ -698,6 +713,7 @@ describe('@vaadin/hilla-lit-form', () => {
it('should display submitting state during submission', async () => {
binder.for(binder.model.customer.fullName).value = 'Jane Doe';
binder.for(binder.model.notes).value = 'foo';
binder.for(binder.model.priority).value = 0;
await orderView.updateComplete;
expect(binder.submitting).to.be.false;
const requestUpdateSpy = sinon.spy(orderView, 'requestUpdate');
Expand Down Expand Up @@ -752,6 +768,7 @@ describe('@vaadin/hilla-lit-form', () => {
value.customer.fullName = 'Jane Doe';
value.notes = '42';
value.total = 1;
value.priority = 0;
binder.value = value;
await orderView.updateComplete;

Expand Down
13 changes: 8 additions & 5 deletions packages/ts/react-crud/test/autocrud.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('@vaadin/hilla-react-crud', () => {
expect(firstName.disabled).to.be.true;

const someInteger = await form.getField('Some integer');
expect(someInteger.value).to.equal('0');
expect(someInteger.value).to.equal('');
expect(someInteger.disabled).to.be.true;
});

Expand Down Expand Up @@ -113,8 +113,8 @@ describe('@vaadin/hilla-react-crud', () => {

expect(firstNameField.value).to.equal('');
expect(lastNameField.value).to.equal('');
expect(someIntegerField.value).to.equal('0');
expect(someDecimalField.value).to.equal('0');
expect(someIntegerField.value).to.equal('');
expect(someDecimalField.value).to.equal('');
await form.typeInField('First name', 'Jeff');
await form.typeInField('Last name', 'Lastname');
await form.typeInField('Email', '[email protected]');
Expand Down Expand Up @@ -146,13 +146,16 @@ describe('@vaadin/hilla-react-crud', () => {
await form.typeInField('Last name', 'Lastname');
await form.typeInField('Email', '[email protected]');
await form.typeInField('Some integer', '12');
await form.typeInField('Some decimal', '12.345');
await form.typeInField('Some decimal', '11--12');
// await form.typeInField('Department', '{{"id":0,"version":0,"name":""}}');

// TODO is correct?
await form.submit();
await form.typeInField('First name', 'Jerp');
await form.submit();
expect(grid.getBodyCellContent(1, 0)).to.have.rendered.text('Jerp');
expect(grid.getRowCount()).to.equal(3);
});
}).timeout(5000);

it('updates grid and form when creating a new item after selecting an existing item', async () => {
const { grid, form, newButton } = await CrudController.init(render(<TestAutoCrud />), user);
Expand Down
40 changes: 31 additions & 9 deletions packages/ts/react-crud/test/autoform.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ describe('@vaadin/hilla-react-crud', () => {
lastName: '',
gender: Gender.MALE,
email: '',
someInteger: 0,
someDecimal: 0,
someInteger: NaN,
someDecimal: NaN,
id: -1,
version: -1,
vip: false,
Expand All @@ -73,8 +73,6 @@ describe('@vaadin/hilla-react-crud', () => {
country: '',
},
department: {
id: 0,
version: 0,
name: '',
},
};
Expand All @@ -86,8 +84,8 @@ describe('@vaadin/hilla-react-crud', () => {
person.lastName,
person.gender,
person.email,
person.someInteger.toString(),
person.someDecimal.toString(),
Number.isNaN(person.someInteger) ? '' : person.someInteger.toString(),
Number.isNaN(person.someDecimal) ? '' : person.someDecimal.toString(),
person.vip.toString(),
person.birthDate,
person.shiftStart,
Expand Down Expand Up @@ -185,6 +183,10 @@ describe('@vaadin/hilla-react-crud', () => {
user,
render(<AutoForm service={personService()} model={PersonModel} />).container,
);
// let values;
// await form.getValues(...LABELS).then((results) => {
// values = results;
// });
await expect(form.getValues(...LABELS)).to.eventually.be.deep.equal(getExpectedValues(DEFAULT_PERSON));
});

Expand Down Expand Up @@ -239,6 +241,7 @@ describe('@vaadin/hilla-react-crud', () => {
await form.typeInField('Some integer', '12');
await form.typeInField('Some decimal', '0.12');
await form.typeInField('Street', '123 Fake Street');
// TODO add other default fields
await form.submit();

expect(saveSpy).to.have.been.calledOnce;
Expand Down Expand Up @@ -284,19 +287,26 @@ describe('@vaadin/hilla-react-crud', () => {
// Item is undefined
const result = render(<AutoForm service={service} model={PersonModel} />);
const form = await FormController.init(user, result.container);
await form.typeInField('First name', 'foo');

const typeMinInFields = async () => {
await form.typeInField('First name', 'foo');
await form.typeInField('Some integer', '0');
await form.typeInField('Some decimal', '0');
};
await typeMinInFields();
await form.submit();
await expect(form.getValues(...LABELS)).to.eventually.be.deep.equal(getExpectedValues(DEFAULT_PERSON));

// Item is null
result.rerender(<AutoForm service={service} model={PersonModel} item={null} />);
await form.typeInField('First name', 'foo');
await typeMinInFields();
await form.submit();
await expect(form.getValues(...LABELS)).to.eventually.be.deep.equal(getExpectedValues(DEFAULT_PERSON));

// Item is emptyItem
result.rerender(<AutoForm service={service} model={PersonModel} item={emptyItem} />);
await form.typeInField('First name', 'foo');
await typeMinInFields();

await form.submit();
await expect(form.getValues(...LABELS)).to.eventually.be.deep.equal(getExpectedValues(DEFAULT_PERSON));
});
Expand Down Expand Up @@ -1058,6 +1068,18 @@ describe('@vaadin/hilla-react-crud', () => {
expect(addressField.value).to.equal(JSON.stringify(DEFAULT_PERSON.address));
expect(departmentField.value).to.equal(JSON.stringify(DEFAULT_PERSON.department));
});

it('renders JSON string with default values when creating new item', async () => {
const service = personService();
const result = render(
<AutoForm service={service} model={PersonModel} visibleFields={['address', 'department']} />,
);
const form = await FormController.init(user, result.container);
const [addressField, departmentField] = await form.getFields('Address', 'Department');

expect(addressField.value).to.equal(JSON.stringify(DEFAULT_PERSON.address));
expect(departmentField.value).to.equal(JSON.stringify(DEFAULT_PERSON.department));
});
});

describe('Field Options', () => {
Expand Down
Loading

0 comments on commit 691762e

Please sign in to comment.