Skip to content

Commit

Permalink
test: add tests unit transpiled | and node-mocha
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamensuli committed Dec 23, 2023
1 parent bc4cbd0 commit 18d8235
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 33 deletions.
31 changes: 26 additions & 5 deletions src/tests/Container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ import { expect, assert } from 'chai';
import 'mocha';
import Container from '../Container';

// eslint-disable-next-line @typescript-eslint/no-namespace
declare namespace NodeJS {
interface Global {
container :Container

declare global {
interface NodeJSGlobal {
container: Container;
}
}

declare let global: NodeJS.Global & { container?: Container };

class myClass{
Expand Down Expand Up @@ -47,16 +46,25 @@ describe('Container', () => {
});

it('Container set and get services', () => {
if(! global.container){
throw new Error(`global not ready `)
}
// Récupération du service
expect(global.container.get('service1')).to.be.a('function');
expect(global.container.get('myclass')).to.be.instanceOf(myClass)
});

it('Container set and get parameters', () => {
if(! global.container){
throw new Error(`global not ready `)
}
// Récupération du service
global.container.setParameters("foo.bar", "test")
expect(global.container.getParameters("foo.bar")).eq("test")
assert.throws(() =>{
if(! global.container){
throw new Error(`global not ready `)
}
global.container.setParameters("foo.bar.ele", {})
}, Error, "Cannot create property 'ele' on string 'test'")
const obj = {}
Expand All @@ -67,6 +75,9 @@ describe('Container', () => {
});

it('Container Scope', () => {
if(! global.container){
throw new Error(`global not ready `)
}
global.container.addScope("myscope")
const scopeContainer = global.container.enterScope("myscope")
global.container.addScope("myscope2")
Expand All @@ -78,6 +89,9 @@ describe('Container', () => {
})

it('Container Scope set and get parameters', () => {
if(! global.container){
throw new Error(`global not ready `)
}
const obj = {}
global.container.setParameters("foo.bar", obj)
global.container.addScope("myscope")
Expand All @@ -95,6 +109,10 @@ describe('Container', () => {
})

it('Container Scope add service on scope', () => {

if(! global.container){
throw new Error(`global not ready `)
}
global.container.addScope("myscope")
const scopeContainer = global.container.enterScope("myscope")
global.container.addScope("myscope2")
Expand All @@ -106,6 +124,9 @@ describe('Container', () => {
})

it('Container Scope add service on main container', () => {
if(! global.container){
throw new Error(`global not ready `)
}
global.container.addScope("myscope")
const scopeContainer = global.container.enterScope("myscope")
global.container.addScope("myscope2")
Expand Down
26 changes: 24 additions & 2 deletions src/tests/Event.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ import Event ,{create , notification} from '../Event'
import {isPromise} from '../Tools'


declare let global: NodeJS.Global & { notificationsCenter: Event };

declare global {
interface NodeJSGlobal {
notificationsCenter: Event;
}
}
declare let global: NodeJS.Global & { notificationsCenter?: Event };


describe("NODEFONY Notifications Center", () => {
Expand All @@ -36,6 +40,9 @@ describe("NODEFONY Notifications Center", () => {

describe("sync", () => {
it("sync", (done) => {
if ( ! global.notificationsCenter){
throw new Error(`global not ready`)
}
const obj = {};
global.notificationsCenter.on("myEvent", (count, args) => {
assert.strictEqual(args, obj);
Expand All @@ -47,6 +54,9 @@ describe("NODEFONY Notifications Center", () => {
});
let i = 0;
setTimeout(() => {
if ( ! global.notificationsCenter){
throw new Error(`global not ready`)
}
global.notificationsCenter.fire("myEvent", i, obj);
global.notificationsCenter.emit("myEvent", ++i, obj);
}, 500);
Expand All @@ -65,6 +75,9 @@ describe("NODEFONY Notifications Center", () => {
global.notificationsCenter = create();
});
it("simple", async () => {
if ( ! global.notificationsCenter){
throw new Error(`global not ready`)
}
const obj = {};
global.notificationsCenter.on("myEvent", async (count, args) => new Promise((resolve) => {
assert.strictEqual(args, obj);
Expand All @@ -89,6 +102,9 @@ describe("NODEFONY Notifications Center", () => {
});

it("multi", async () => {
if ( ! global.notificationsCenter){
throw new Error(`global not ready`)
}
const obj = {};
global.notificationsCenter.on("myEvent", async (count, args) => new Promise((resolve) => {
setTimeout(() => {
Expand Down Expand Up @@ -117,6 +133,9 @@ describe("NODEFONY Notifications Center", () => {
});

it("await", async () => {
if ( ! global.notificationsCenter){
throw new Error(`global not ready`)
}
const myFunc = async function (count: number, args: any) {
if (count === 0) {
return count + 1;
Expand Down Expand Up @@ -146,6 +165,9 @@ describe("NODEFONY Notifications Center", () => {
});

it("await error", async () => {
if ( ! global.notificationsCenter){
throw new Error(`global not ready`)
}
const myFunc = async function (count: number, args: any) {
if (count === 0) {
return count + 1;
Expand Down
2 changes: 1 addition & 1 deletion src/tests/Extend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe("TOOLS extend", () => {
bar: "foo"
});
assert(res === myobj);
let obj:{[key: string]: any} = {};
let obj:{foo?: string, bar?:string} = {};
res = extend(obj, myobj, {
bar: "foo"
});
Expand Down
2 changes: 2 additions & 0 deletions src/tests/Service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import 'mocha';
//import assert from 'node:assert'
23 changes: 13 additions & 10 deletions src/tests/Syslog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
//import { expect, assert as assertChai} from 'chai'
import Syslog ,{ conditionsInterface}from "../syslog/Syslog";
import nodefony from "../Nodefony"
//import nodefony from "../Nodefony"
import Pdu from '../syslog/Pdu'
import assert from 'node:assert';

Expand Down Expand Up @@ -137,12 +137,12 @@ describe("NODEFONY SYSLOG", () => {

it("1000 entries ", (done) => {
let i = 0;
const res = global.syslog.on(
global.syslog.on(
"onLog",
(pdu) => i++
);
for (let i = 0; i < 1000; i++) {
const pdu = global.syslog.log(i, i % 2 ? "INFO" : "DEBUG");
global.syslog.log(i, i % 2 ? "INFO" : "DEBUG");
}
assert.strict.equal(global.syslog.ringStack.length, 100);
assert.strict.equal(global.syslog.ringStack[0].payload, 900);
Expand Down Expand Up @@ -189,6 +189,7 @@ describe("NODEFONY SYSLOG", () => {
});

describe("loadStack ", () => {

it("loadStack 1000 entries ", (done) => {
const inst = new Syslog({
maxStack: 100
Expand All @@ -197,8 +198,9 @@ describe("NODEFONY SYSLOG", () => {
assert.strict.equal(inst.ringStack.length, 100);
done();
});

it("loadStack 1000 events ", (done) => {
const inst = new nodefony.Syslog({
const inst = new Syslog({
maxStack: 100
});
let i = 0;
Expand All @@ -215,8 +217,9 @@ describe("NODEFONY SYSLOG", () => {
assert.strict.equal(i, 50);
done();
});

it("loadStack 1000 events ", (done) => {
const inst = new nodefony.Syslog({
const inst = new Syslog({
maxStack: 100
});
let i = 0;
Expand Down Expand Up @@ -334,7 +337,7 @@ describe("NODEFONY SYSLOG", () => {
);
assert.strict.equal(global.syslog._eventsCount, 1);
for (let i = 0; i < 10; i++) {
const pdu = global.syslog.log(i, i % 2 ? "INFO" : "DEBUG");
global.syslog.log(i, i % 2 ? "INFO" : "DEBUG");
}
assert.strict.equal(i, 10);
done();
Expand All @@ -353,7 +356,7 @@ describe("NODEFONY SYSLOG", () => {
);
assert.strict.equal(global.syslog._eventsCount, 1);
for (let i = 0; i < 10; i++) {
const pdu = global.syslog.log(i, i % 2 ? "INFO" : "DEBUG");
global.syslog.log(i, i % 2 ? "INFO" : "DEBUG");
}
assert.strict.equal(i, 5);
done();
Expand All @@ -376,7 +379,7 @@ describe("NODEFONY SYSLOG", () => {
);
assert.strict.equal(global.syslog._eventsCount, 1);
for (let i = 0; i < 10; i++) {
const pdu = global.syslog.log(i, i % 2 ? "INFO" : "DEBUG");
global.syslog.log(i, i % 2 ? "INFO" : "DEBUG");
}
assert.strict.equal(i, 5);
done();
Expand All @@ -398,7 +401,7 @@ describe("NODEFONY SYSLOG", () => {
}
);
for (let i = 0; i < 10; i++) {
const pdu = global.syslog.log(i, i % 2 ? "INFO" : "DEBUG");
global.syslog.log(i, i % 2 ? "INFO" : "DEBUG");
}
assert.strict.equal(i, 5);
done();
Expand All @@ -422,7 +425,7 @@ describe("NODEFONY SYSLOG", () => {
);
assert.strict.equal(global.syslog._eventsCount, 1);
for (let i = 0; i < 10; i++) {
const pdu = global.syslog.log(i, i % 2 ? "INFO" : "DEBUG");
global.syslog.log(i, i % 2 ? "INFO" : "DEBUG");
}
assert.strict.equal(i, 5);
done();
Expand Down
15 changes: 0 additions & 15 deletions src/tests/index.test.ts

This file was deleted.

0 comments on commit 18d8235

Please sign in to comment.