Add test for api.js

This commit is contained in:
Theresa Gresch 2019-08-13 09:45:10 +02:00
parent 38ce81731b
commit 891ccdf016

View file

@ -1,41 +1,45 @@
import assert from 'assert';
import Api from 'common/api';
import MockAdapter from 'axios-mock-adapter';
import Api from "common/api";
import MockAdapter from "axios-mock-adapter";
const mock = new MockAdapter(Api);
let chai = require("../../../node_modules/chai/chai");
let assert = chai.assert;
const getCollectionResponse = [
{id: 1, name: 'John Smith'},
{id: 1, name: 'John Smith'}
];
describe("common/api", () => {
const getEntityResponse = {
id: 1, name: 'John Smith'
};
const mock = new MockAdapter(Api);
const postEntityResponse = {
users: [
{id: 1, name: 'John Smith'}
]
};
const getCollectionResponse = [
{id: 1, name: "John Smith"},
{id: 1, name: "John Smith"}
];
const putEntityResponse = {
users: [
{id: 2, name: 'John Foo'}
]
};
const getEntityResponse = {
id: 1, name: "John Smith"
};
const deleteEntityResponse = null;
const postEntityResponse = {
users: [
{id: 1, name: "John Smith"}
]
};
mock.onGet('foo').reply(200, getCollectionResponse);
mock.onGet('foo/123').reply(200, getEntityResponse);
mock.onPost('foo').reply(201, postEntityResponse);
mock.onPut('foo/2').reply(200, putEntityResponse);
mock.onDelete('foo/2').reply(204, deleteEntityResponse);
const putEntityResponse = {
users: [
{id: 2, name: "John Foo"}
]
};
describe('common/api', () => {
it('get("foo") should return a list of results and return with HTTP code 200', (done) => {
Api.get('foo').then(
const deleteEntityResponse = null;
mock.onGet("foo").reply(200, getCollectionResponse);
mock.onGet("foo/123").reply(200, getEntityResponse);
mock.onPost("foo").reply(201, postEntityResponse);
mock.onPut("foo/2").reply(200, putEntityResponse);
mock.onDelete("foo/2").reply(204, deleteEntityResponse);
mock.onGet("error").reply(401, "custom error cat");
it("get should return a list of results and return with HTTP code 200", (done) => {
Api.get("foo").then(
(response) => {
assert.equal(200, response.status);
assert.deepEqual(getCollectionResponse, response.data);
@ -48,8 +52,8 @@ describe('common/api', () => {
);
});
it('get("foo/123") should return one item and return with HTTP code 200', (done) => {
Api.get('foo/123').then(
it("get should return one item and return with HTTP code 200", (done) => {
Api.get("foo/123").then(
(response) => {
assert.equal(200, response.status);
assert.deepEqual(getEntityResponse, response.data);
@ -62,8 +66,8 @@ describe('common/api', () => {
);
});
it('post("foo") should create one item and return with HTTP code 201', (done) => {
Api.post('foo', postEntityResponse).then(
it("post should create one item and return with HTTP code 201", (done) => {
Api.post("foo", postEntityResponse).then(
(response) => {
assert.equal(201, response.status);
assert.deepEqual(postEntityResponse, response.data);
@ -76,8 +80,8 @@ describe('common/api', () => {
);
});
it('put("foo/2") should update one item and return with HTTP code 200', (done) => {
Api.put('foo/2', putEntityResponse).then(
it("put should update one item and return with HTTP code 200", (done) => {
Api.put("foo/2", putEntityResponse).then(
(response) => {
assert.equal(200, response.status);
assert.deepEqual(putEntityResponse, response.data);
@ -90,8 +94,8 @@ describe('common/api', () => {
);
});
it('delete("foo/2") should delete one item and return with HTTP code 204', (done) => {
Api.delete('foo/2', deleteEntityResponse).then(
it("delete should delete one item and return with HTTP code 204", (done) => {
Api.delete("foo/2", deleteEntityResponse).then(
(response) => {
assert.equal(204, response.status);
assert.deepEqual(deleteEntityResponse, response.data);
@ -103,4 +107,10 @@ describe('common/api', () => {
}
);
});
it("get error", function() {
return Api.get("error")
.then(function(m) { throw new Error("was not supposed to succeed"); })
.catch(function(m) { assert.equal(m.message, "Request failed with status code 401")});
});
});