The first thing to do would be to write some test cases for the brand new validation function we just created, which means we're going to make a new test file called validation.test.js.
Inside there, we're going to load in an expect making a variable called expect. We could also make it a constant. We're going to set that equal to require and we're going to require the expect library:
const expect = require('expect');
Next up, we're going to load in RealString, import isRealString, and we're going to add three test cases. The describe blocks should be something like isRealString, and the three test cases will be as follows:
- The first one, should reject non-string values, in this case I want you to pass a number object or anything else into the isRealString function, you should get false back.
- Next up, should reject strings with only spaces. If I have a string that's just a bunch of spaces that should not pass the isRealString function validation. That's also going to fail; trim is going to remove all of those spaces and the length will be 0.
- Last up, should allow strings with non-space characters. In this case you can pass in whatever you like, some sort of valid value. You could have space space LOTR for Lord of the Rings, the beginning spaces are going to get trimmed out so it's not important. You could add the letter a, any valid string is going to pass this one.
Go ahead and set up those three test cases, making sure the right Boolean value comes back from isRealString. When you're done run npm test from the Terminal, make sure all of your three tests passed.
The first thing we're going to do is import isRealString by making a variable. We can make this a constant or a variable, I'm going to go with a constant, and we're going to use ES6 destructuring to grab isRealString, and we're going to grab it off of the require call to our local file, ./validation, which is just alongside the current file validation.test.js:
const expect = require('expect');
// import isRealString
const {isRealString} = require('./validation');
Now we can add the things we have down below starting with our describe block.