Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it("should do something", async () => {
// give the locals map to the invoke method
const instance: MyCtrl = await TestContext.invoke(MyCtrl, [{
provide: DbService,
use: {
getData: () => {
return "test";
}
}
}]);
// and test it
expect(!!instance).to.eq(true);
expect(instance.getData()).to.equals("test");
});
});
it("should get the service from InjectorService", async () => {
// GIVEN
const memoryStorage = {
set: () => {
},
get: () => {
}
};
// WHEN
const calendarsService: CalendarsService = await TestContext.invoke(CalendarsService, [
{provide: MemoryStorage, use: memoryStorage}
]);
// THEN
expect(calendarsService).to.be.an.instanceof(CalendarsService);
// @ts-ignore
expect(calendarsService.memoryStorage).to.equal(memoryStorage);
});
});
it("should invoke a calendarService with a fake memoryStorage", () => {
// GIVEN
const memoryStorage = {
set: () => {
},
get: () => {
}
};
// WHEN
const calendarsService = TestContext.invoke(CalendarsService, [
{provide: MemoryStorage, use: memoryStorage}
]);
// THEN
expect(calendarsService.memoryStorage).to.equal(memoryStorage);
expect(calendarsService).to.be.an.instanceof(CalendarsService);
});
});
it("should return a result from mocked service", async () => {
// GIVEN
const calendarsService = {
find: Sinon.stub().rejects({id: "1"})
};
const calendarsCtrl = await TestContext.invoke(CalendarsCtrl, [{
provide: CalendarsService,
use: calendarsService
}]);
// WHEN
let actualError;
try {
await calendarsCtrl.get("1");
} catch (er) {
actualError = er;
}
// THEN
actualError.should.be.instanceof(NotFound);
calendarsService.find.should.be.calledWithExactly("1");
});
it("should return a result from mocked service", async () => {
// GIVEN
const calendarsService = {
find: Sinon.stub().resolves({id: "1"})
};
const calendarsCtrl = await TestContext.invoke(CalendarsCtrl, [{
provide: CalendarsService,
use: calendarsService
}]);
// WHEN
const result = await calendarsCtrl.get("1");
// THEN
result.should.deep.equal({id: "1"});
calendarsService.find.should.be.calledWithExactly("1");
calendarsCtrl.should.be.an.instanceof(CalendarsCtrl);
calendarsCtrl.calendarsService.should.deep.equal(calendarsService);
});
});
it("should return the expected result", () => {
// GIVEN
const calendarsService = {
find: Sinon.stub().returns(Promise.resolve({id: "1"}))
};
const calendarController = TestContext.invoke(CalendarCtrl, [
{provide: CalendarsService, use: calendarsService}
]);
// WHEN
const result = calendarController.get("1");
// THEN
result.should.eventually.deep.equal({id: "1"});
calendarsService.find.should.be.calledWithExactly("1");
expect(calendarController.calendarsService).to.equal(calendarsService);
});
});
it("should throw an error", async () => {
// GIVEN
const calendarsService = {
find: Sinon.stub().resolves()
};
const middleware: CheckCalendarIdMiddleware = await TestContext.invoke(CheckCalendarIdMiddleware, [{
provide: CalendarsService,
use: calendarsService
}]);
// WHEN
let actualError;
try {
await middleware.use("1");
} catch (er) {
actualError = er;
}
// THEN
// @ts-ignore
calendarsService.find.should.be.calledWithExactly("1");
actualError.should.instanceOf(NotFound);
actualError.message.should.eq("Calendar not found");
it("should return a result from mocked service", async () => {
// GIVEN
const eventsCtrl: EventsCtrl = await TestContext.invoke(EventsCtrl, []);
const calendarId = "2";
const startDate = "startDate";
const endDate = "endDate";
const name = "name";
const id = "7";
// WHEN
const result = await eventsCtrl.update(calendarId, id, startDate, endDate, name);
// THEN
result.should.deep.equal({
"calendarId": "2",
"endDate": "name",
"id": "7",
"name": "name",
"startDate": "name",
it("should get the service from InjectorService", async () => {
// GIVEN
const memoryStorage = {
set: () => {
},
get: () => {
}
};
const locals = new Map();
locals.set(MemoryStorage, memoryStorage);
// WHEN
const calendarsService: CalendarsService = await TestContext.invoke(CalendarsService, [
{provide: MemoryStorage, use: memoryStorage}
]);
// THEN
expect(calendarsService).to.be.an.instanceof(CalendarsService);
// @ts-ignore
expect(calendarsService.memoryStorage).to.equal(memoryStorage);
});
});
before(async () => {
await TestContext.create();
this.socketIOServer = {attach: Sinon.stub(), adapter: Sinon.stub()};
this.httpServer = {type: "http", get: Sinon.stub().returns("httpServer")};
this.httpsServer = {type: "https", get: Sinon.stub().returns("httpsServer")};
socketIOService = TestContext.invoke(SocketIOService, [
{provide: HttpServer, use: this.httpServer},
{provide: HttpsServer, use: this.httpsServer},
{provide: SocketIOServer, use: this.socketIOServer}
]);
this.getWebsocketServicesStub = Sinon.stub(socketIOService, "getWebsocketServices");
this.bindProviderStub = Sinon.stub(socketIOService, "bindProvider");
this.printSocketEventsStub = Sinon.stub(socketIOService, "printSocketEvents");
this.getWebsocketServicesStub.returns([{provider: "provider"}]);
socketIOService.serverSettingsService.set("socketIO", {config: "config", adapter: "adapter"});
socketIOService.$onServerReady();
});
after(() => {