Skip to content

Instantly share code, notes, and snippets.

@rezaindrag
Last active July 3, 2020 17:11
Show Gist options
  • Save rezaindrag/0e4a893626578ce5fa5f0b5bec38be66 to your computer and use it in GitHub Desktop.
Save rezaindrag/0e4a893626578ce5fa5f0b5bec38be66 to your computer and use it in GitHub Desktop.
const expect = require("chai").expect;
const { stub } = require("sinon");
const companyRepository = require("../src/repositories/company.repository");
const companyService = require("../src/services/company.service");
const usersMock = require("./mocks/users");
const employeesMock = require("./mocks/employees");
describe("test company service", function () {
afterEach(() => {
// This is required to clean up the mock after each test.
// The goal is the next testing will not be affected or disturbed by the previous mock.
companyRepository.fetchUsers.restore();
companyRepository.fetchEmployees.restore();
});
describe("test fetch company profile", function () {
it("should succeed", async function () {
const fetchUsersFn = stub(companyRepository, "fetchUsers").returns(usersMock);
const fetchEmployeesFn = stub(companyRepository, "fetchEmployees").returns(employeesMock.data);
const companyProfile = await companyService.fetchCompanyProfile();
expect(fetchUsersFn.calledOnce).to.be.true;
expect(fetchEmployeesFn.calledOnce).to.be.true;
const expectedData = {
companyName: 'Facebook Inc',
companyUsers: [
{ id: 1, name: 'mojombo' },
{ id: 2, name: 'defunkt' },
],
companyEmployees: [
{ id: 1, name: 'Tiger Nixon' },
{ id: 2, name: 'Garrett Winters' },
],
};
expect(companyProfile).to.deep.equal(expectedData);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment