B
B
beefront172018-02-16 14:32:59
JavaScript
beefront17, 2018-02-16 14:32:59

How to write a validation test, react/redux?

Good afternoon! Can you please explain how to write a test for the validate reduxForm method?
I still don’t understand what should be behind what ...
for tests I use jest / enzyme
Validation itself

export const validate = (values, props) => {
  const errors = {};

  if (!values.value) {
    errors.value = 'Please enter value';
  }

  if (values.value && (values.value === props.initialValues.value)) {
    errors.value = 'Please enter a new value';
  }

  return errors;
};

I will be very grateful!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2018-02-16
@beefront17

Throwing in theories: you want to test a function. Your function is pure (that is, it produces the same result for the same input parameters, otherwise _arguments_ of the function). Therefore, testing such a function is easy.
A) you import the function in your test file
B1) you call it without values.value- you expect errors.value = 'Please enter value'
B2) you call it with values.value === props.initialValues.valueand expect it to be, errors.value = 'Please enter a new value'
that is, you yourself describe the values ​​object in the first it of your test, in the second it you describe values ​​and props . Your test can be described as follows: "you assume that if you give 20 rubles to the seller, he will give you a loaf." It's the same here: you assume that if the validate function receives such and such arguments (you just created them in this test), you will get such and such an answer from it.

const validate = require('./validate');
const values = {} // то есть values.value - не существует
const props = {} // для первого теста на существование значения, нам не важно какие тут значения props
test('покажет ошибку, если нет значения', () => {
  expect(validate(values, props)).toBe('Please enter value');
});

The second test is for your homework.
ps i wrote in it's answer because i'm used to that tests are written inside it, but now i see in jest dock that they use test ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question