Search code examples
unit-testingtextfieldvalidationgxt

Call validate() in unit test on Field after setValidator() in GXT


I have set a Validator on a TextField using setValidator(). The input is validated fine when I run the application.

When writing unit tests against my GUI I set the validator on the TextField and then set the value to a invalid value. However when I call isValid() or validate() on the TextField it always returns true.

When calling the validator.validate on the TextField with its value, it returns false.

Am I misunderstanding the isValid() and validate() method on the TextField? Or do I need to fire some Event before the validation kicks in, in my test?


Solution

  • Inspected the source code and from what I see the validate() and isValid() methods all check the value using getRawValue() which is the value rendered in the HTML element, and an attempt to directly set the raw value will still fail since it checks for a rendered flag which requires a call to render().

    public String getRawValue() {
        String v = rendered ? getInputEl().getValue() : "";
        if (v == null || v.equals(emptyText)) {
          return "";
        }
        return v;
    }
    

    A solution would be to subclass the TextField and overwrite these methods, but I rather stick to calling the validator.validate() then.