Adding to the mock API Service

In the mock API, we need to add a call to get the details for a specific speaker. We can assume that the speaker has an ID field that we can use to gather that information. As usual, let's start our tests with a simple exists check. We will need to add a new describe inside the After Initialization describe for getting a speaker by ID.

describe('Get Speaker By Id', () => {
it('exists', () => {
// assert
expect(service.getById).to.exist;
});
});

To make this test pass, we need to add a method to the mock API.

getById() {
}

Now, we can write a test to verify the functionality we expect when a matching speaker cannot be found. The functionality we want in this case is for a SPEAKER_NOT_FOUND message to be shown once we get to the user interface. At the mock API level, we could assume that a 404 will be sent from the server. We can respond from the mock API with an error containing the SPEAKER_NOT_FOUND type. This is similar to the way an action would be used.

Let’s create another describe for our speaker not found scenario.

describe('Speaker Does Not Exist', () => {
it('SPEAKER_NOT_FOUND error is generated', () => {
// act
const promise = service.getById('fake-speaker');

// assert
return promise.catch((error) => {
expect(error.type).to.equal(errorTypes.SPEAKER_NOT_FOUND);
});
});
});

You may have noticed that we snuck in errorTypes. The errorTypes are in their own folder, but build exactly like actionTypes.

To make this test pass, we must add a rejected promise to our mock API.

getById(id) {
return new Promise((resolve, reject) => {
setTimeout(() => {
reject({ type: errorTypes.SPEAKER_NOT_FOUND });
}, 0);
});
}

We don't have any tests that enforce a positive result from this method, so we can reject every time for now.

That brings us to our next test. What happens if the speaker is found? Ideally, the speaker and all the speakers details would be delivered back to the caller. Let's write that test now.

describe('Speaker Exists', () => {
it('returns the speaker', () => {
// arrange
const speaker = { id: 'test-speaker' };
service.create(speaker);

// act
let promise = service.getById('test-speaker');

// assert
return promise.then((speaker) => {
expect(speaker).to.not.be.null;
expect(speaker.id).to.equal('test-speaker');
});
});
});

To pass this test we will have to add some logic to the production code.

getById(id) {
return new Promise((resolve, reject) => {
setTimeout(() => {
let speaker = this._speakers.find(x => x.id === id);
if(speaker) {
resolve(Object.assign({}, speaker));
} else {
reject({ type: errorTypes.SPEAKER_NOT_FOUND });
}
}, 0);
});
}

To make the test pass, we need to first check to see if the speaker exists. If the speaker does exist, we return that speaker. If the speaker does not exist, we reject the promise and provide our error result.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.244.67