Last active
July 2, 2020 10:30
-
-
Save rezaindrag/8fda12b52e79e51338698cd6f2d6e4e9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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