index.spec.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. describe('validators', () => {
  2. const id = 'testContainer';
  3. const {
  4. registerValidator,
  5. getValidator,
  6. } = Handsontable.validators;
  7. beforeEach(function() {
  8. this.$container = $(`<div id="${id}"></div>`).appendTo('body');
  9. });
  10. afterEach(function() {
  11. if (this.$container) {
  12. destroy();
  13. this.$container.remove();
  14. }
  15. });
  16. it('should register custom validator', async () => {
  17. registerValidator('myValidator', (value, cb) => {
  18. cb(value === 10);
  19. });
  20. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  21. const hot = handsontable({
  22. data: [
  23. [1, 6, 10],
  24. ],
  25. columns: [{
  26. validator: 'myValidator',
  27. }],
  28. afterValidate: onAfterValidate
  29. });
  30. hot.setDataAtCell(1, 0, 10);
  31. await sleep(100);
  32. expect(onAfterValidate).toHaveBeenCalledWith(true, 10, 1, 0, undefined, undefined);
  33. hot.setDataAtCell(2, 0, 2);
  34. await sleep(100);
  35. expect(onAfterValidate).toHaveBeenCalledWith(false, 2, 2, 0, undefined, undefined);
  36. });
  37. it('should retrieve predefined validators by its names', () => {
  38. expect(getValidator('autocomplete')).toBeFunction();
  39. expect(getValidator('date')).toBeFunction();
  40. expect(getValidator('numeric')).toBeFunction();
  41. expect(getValidator('time')).toBeFunction();
  42. });
  43. it('should retrieve custom validator by its names', () => {
  44. registerValidator('myValidator', (value, cb) => {
  45. cb(value === 10);
  46. });
  47. getValidator('myValidator')(2, (isValid) => {
  48. expect(isValid).toBe(false);
  49. });
  50. getValidator('myValidator')('10', (isValid) => {
  51. expect(isValid).toBe(false);
  52. });
  53. getValidator('myValidator')(10, (isValid) => {
  54. expect(isValid).toBe(true);
  55. });
  56. });
  57. });