Search in sources :

Example 1 with Result

use of com.vaadin.flow.data.binder.Result in project linkki by linkki-framework.

the class BindLabelValueAspectDefinitionTest method testCreateComponentValueSetter_UsesCustomConverter.

@Test
void testCreateComponentValueSetter_UsesCustomConverter() {
    LinkkiText label = new LinkkiText();
    Consumer<Object> valueSetter = new BindLabelValueAspectDefinition().createComponentValueSetter(new NoLabelComponentWrapper(label));
    LinkkiConverterRegistry converterRegistry = LinkkiConverterRegistry.DEFAULT.with(new Converter<String, FooBar>() {

        private static final long serialVersionUID = 1L;

        @Override
        public Result<FooBar> convertToModel(String value, ValueContext context) {
            return Result.ok(FooBar.valueOf(value));
        }

        @Override
        public String convertToPresentation(FooBar value, ValueContext context) {
            return value == FooBar.FOO ? "Foo" : "Bar";
        }
    });
    VaadinSession vaadinSession = mock(VaadinSession.class);
    when(vaadinSession.getAttribute(LinkkiConverterRegistry.class)).thenReturn(converterRegistry);
    VaadinSession.setCurrent(vaadinSession);
    valueSetter.accept(FooBar.FOO);
    assertThat(label.getText(), is("Foo"));
}
Also used : ValueContext(com.vaadin.flow.data.binder.ValueContext) LinkkiText(org.linkki.core.vaadin.component.base.LinkkiText) VaadinSession(com.vaadin.flow.server.VaadinSession) LinkkiConverterRegistry(org.linkki.core.ui.converters.LinkkiConverterRegistry) Result(com.vaadin.flow.data.binder.Result) NoLabelComponentWrapper(org.linkki.core.ui.wrapper.NoLabelComponentWrapper) Test(org.junit.jupiter.api.Test)

Example 2 with Result

use of com.vaadin.flow.data.binder.Result in project linkki by linkki-framework.

the class LabelValueAspectDefinitionTest method testCreateComponentValueSetter_UsesCustomConverter.

@Test
public void testCreateComponentValueSetter_UsesCustomConverter() {
    LinkkiText label = new LinkkiText();
    Consumer<Object> valueSetter = new LabelValueAspectDefinition(false).createComponentValueSetter(new NoLabelComponentWrapper(label));
    LinkkiConverterRegistry converterRegistry = LinkkiConverterRegistry.DEFAULT.with(new Converter<String, FooBar>() {

        private static final long serialVersionUID = 1L;

        @Override
        public Result<FooBar> convertToModel(String value, ValueContext context) {
            return Result.ok(FooBar.valueOf(value));
        }

        @Override
        public String convertToPresentation(FooBar value, ValueContext context) {
            return value == FooBar.FOO ? "Foo" : "Bar";
        }
    });
    VaadinSession vaadinSession = mock(VaadinSession.class);
    when(vaadinSession.getAttribute(LinkkiConverterRegistry.class)).thenReturn(converterRegistry);
    VaadinSession.setCurrent(vaadinSession);
    valueSetter.accept(FooBar.FOO);
    assertThat(label.getText(), is("Foo"));
}
Also used : ValueContext(com.vaadin.flow.data.binder.ValueContext) LinkkiText(org.linkki.core.vaadin.component.base.LinkkiText) VaadinSession(com.vaadin.flow.server.VaadinSession) LinkkiConverterRegistry(org.linkki.core.ui.converters.LinkkiConverterRegistry) Result(com.vaadin.flow.data.binder.Result) NoLabelComponentWrapper(org.linkki.core.ui.wrapper.NoLabelComponentWrapper) Test(org.junit.jupiter.api.Test)

Aggregations

Result (com.vaadin.flow.data.binder.Result)2 ValueContext (com.vaadin.flow.data.binder.ValueContext)2 VaadinSession (com.vaadin.flow.server.VaadinSession)2 Test (org.junit.jupiter.api.Test)2 LinkkiConverterRegistry (org.linkki.core.ui.converters.LinkkiConverterRegistry)2 NoLabelComponentWrapper (org.linkki.core.ui.wrapper.NoLabelComponentWrapper)2 LinkkiText (org.linkki.core.vaadin.component.base.LinkkiText)2