Skip to content

Instantly share code, notes, and snippets.

@rezaindrag
Last active July 2, 2020 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rezaindrag/8fda12b52e79e51338698cd6f2d6e4e9 to your computer and use it in GitHub Desktop.
Save rezaindrag/8fda12b52e79e51338698cd6f2d6e4e9 to your computer and use it in GitHub Desktop.
const expect = require("chai").expect;
const nock = require("nock");
const companyRepository = require("../src/repositories/company.repository");
const usersMock = require("./mocks/users");
describe("test company repository", function () {
after(function() {
nock.restore();
});
afterEach(function() {
nock.cleanAll();
});
describe("test fetch users", function () {
it("should succeed", async function () {
const usersScope = nock("https://api.github.com")
.get("/users")
.reply(200, usersMock, { 'Content-Type': 'application/json' });
const users = await companyRepository.fetchUsers();
expect(users).to.have.lengthOf(2);
expect(users).to.deep.equal(usersMock);
expect(users[0].login).to.equal("mojombo");
// Will throw an assertion error if meanwhile a "https://api.github.com" was
// not performed. (https://github.com/nock/nock#expectations)
usersScope.done();
});
it("should be error", async function () {
const usersScope = nock("https://api.github.com")
.get("/users")
.reply(
500,
{ message: "internal server error" },
{ "Content-Type": "application/json" },
);
try {
await companyRepository.fetchUsers();
} catch (e) {
expect(e.response.status).to.equal(500);
expect(e.response.data.message).to.equal("internal server error");
}
usersScope.done();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment