Let's get started by loading in expect, const expect = require('expect'), and we can also go ahead and load in our users file, const. Using ES6 destructuring we're going to grab Users, and we're going to get that via the local file ./users:
const expect = require('expect');
const {Users} = require('./users');
Now for the moment, we're just going to add a test case for adding a user. We'll make a quick describe block, most of the heavy lifting is going to happen later in the section. We'll describe our Users class, we can then add our arrow function and we can go ahead and add a test case, it, inside quotes, should add new user. I'm going to go ahead and set up the function for this one. It's going to be a synchronous function so there's no need for the done argument, and we can create a new instance of users, var users, equals a new Users:
describe('Users', () => {
it('should add new user', ()=> {
var users = new Users();
});
});
Now since we don't take any arguments in the constructor function, we're not going to pass any in when we actually create our instance.
The next thing we're going to do is make a user, then we'll be passing its properties to addUser making sure the appropriate thing shows up in the end. Let's go ahead and make a variable user and we'll set that equal to an object:
it('should add new user', ()=> {
var users = new Users();
var user = {
}
});
I'm going to go ahead and set on this object three properties, an id equal to something like 123, a name property equal to some name like Andrew, and you can go ahead and use your first name for example, Andrew, and a room name. I'm going to use The Office Fans:
describe('Users', () => {
it('should add new user', ()=> {
var users = new Users();
var user = {
id: '123',
name: 'Andrew',
room: 'The office fans'
};
});
});
Now we have the user in place and we can go ahead and call that method that we just created, the addUser method with the three necessary arguments, id, name, and room. I'm going to store the response in a variable called resUser, and we'll set it equal to users.addUser passing in those three pieces of information, user.id, user.name, and user.room as the third argument:
describe('Users', () => {
it('should add new user', ()=> {
var users = new Users();
var user = {
id: '123',
name: 'Andrew',
room: 'The office fans'
};
var resUser = users.addUser(user.id, user.name, user.room);
});
});
With the call in place we can now start making our assertions.