Skip to content

Commit

Permalink
test: refactor tests structure
Browse files Browse the repository at this point in the history
  • Loading branch information
b-ma committed Dec 12, 2023
1 parent 46d9113 commit f62bbda
Show file tree
Hide file tree
Showing 9 changed files with 392 additions and 389 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
"scripts": {
"doc": "rm -Rf docs && jsdoc -c .jsdoc.json --verbose && cp -R assets docs/",
"lint": "npx eslint src",
"test": "mocha",
"test": "npx mocha",
"test:all": "npx mocha tests/*/*.spec.js",
"toc": "markdown-toc -i README.md",
"types": "rm -Rf types && tsc",
"preversion": "npm run toc && git commit -am 'docs: build' --allow-empty",
Expand Down
6 changes: 3 additions & 3 deletions tests/Context.spec.js → tests/contexts/Context.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { assert } from 'chai';

import { Server, Context as ServerContext } from '../src/server/index.js';
import { Client, Context as ClientContext } from '../src/client/index.js';
import { Server, Context as ServerContext } from '../../src/server/index.js';
import { Client, Context as ClientContext } from '../../src/client/index.js';

import config from './utils/config.js';
import config from '../utils/config.js';

describe('# Context', () => {
describe(`## contructor()`, () => {
Expand Down
8 changes: 4 additions & 4 deletions tests/Client.spec.js → tests/essentials/Client.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { assert } from 'chai';

import { Server, Context as ServerContext } from '../src/server/index.js';
import { Client, Context as ClientContext } from '../src/client/index.js';
import { Server, Context as ServerContext } from '../../src/server/index.js';
import { Client, Context as ClientContext } from '../../src/client/index.js';

import pluginDelayClient from './utils/PluginDelayClient.js';
import config from './utils/config.js';
import pluginDelayClient from '../utils/PluginDelayClient.js';
import config from '../utils/config.js';

describe('# client::Client', () => {
describe(`## new Client(config)`, () => {
Expand Down
6 changes: 3 additions & 3 deletions tests/Server.spec.js → tests/essentials/Server.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import dotenv from 'dotenv';
import merge from 'lodash.merge';
import tcpp from 'tcp-ping';

import { Server, Context as ServerContext } from '../src/server/index.js';
import { Client } from '../src/client/index.js';
import { Server, Context as ServerContext } from '../../src/server/index.js';
import { Client } from '../../src/client/index.js';

import config from './utils/config.js';
import config from '../utils/config.js';

const __filename = url.fileURLToPath(import.meta.url);
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
Expand Down
10 changes: 5 additions & 5 deletions tests/plugins/Plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const config = {
},
};

describe('Plugin', () => {
describe(`# new Plugin(server|client, options)`, () => {
describe('# Plugin', () => {
describe(`## constructor(server|client, options)`, () => {
it(`id and type should be readonly`, async () => {
const server = new Server(config);
server.pluginManager.register('delay', pluginDelayServer, {
Expand Down Expand Up @@ -59,7 +59,7 @@ describe('Plugin', () => {
});
});

describe(`# [client] Plugin.state propagation`, () => {
describe(`## [client] Plugin.state propagation`, () => {
it(`should propagate its inner state`, async () => {
const server = new Server(config);
server.pluginManager.register('stateful', (ServerPlugin) => class StatefulPlugin extends ServerPlugin {});
Expand Down Expand Up @@ -135,7 +135,7 @@ describe('Plugin', () => {
});
});

describe(`# [client|server] Require plugin within plugin`, () => {
describe(`## [client|server] Require plugin within plugin`, () => {
it(`should work`, async function() {
this.timeout(2000);

Expand Down Expand Up @@ -214,7 +214,7 @@ describe('Plugin', () => {
});
});

describe(`# [server] Plugin.state propagation`, () => {
describe(`## [server] Plugin.state propagation`, () => {
it('PluginManager should properly propagate plugin state', async () => {
const server = new Server(config);
server.pluginManager.register('stateful', (ClientPlugin) => {
Expand Down
36 changes: 18 additions & 18 deletions tests/plugins/client.PluginManager.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import pluginDelayServer from '../utils/PluginDelayServer.js';
import pluginDelayClient from '../utils/PluginDelayClient.js';
import config from '../utils/config.js';

describe(`client::PluginManager`, () => {
describe(`# PluginManagerClient`, () => {
let server = null;

before(async function() {
Expand All @@ -29,7 +29,7 @@ describe(`client::PluginManager`, () => {
await server.stop();
});

describe(`# (protected) new PluginManager(client)`, () => {
describe(`## [private] constructor(client)`, () => {
it(`should throw if argument is not instance of Client`, () => {
let errored = false;
try {
Expand All @@ -42,7 +42,7 @@ describe(`client::PluginManager`, () => {
});
});

describe(`# register(id, pluginFactory)`, () => {
describe(`## register(id, pluginFactory)`, () => {
let client;

beforeEach(() => {
Expand Down Expand Up @@ -140,7 +140,7 @@ describe(`client::PluginManager`, () => {
});
});

describe(`# (protected) await pluginManager.init()`, () => {
describe(`## [private] async init()`, () => {
it(`should throw if started twice`, async () => {
const client = new Client({ role: 'test', ...config });
await client.init(); // run pluginManager.init()
Expand All @@ -156,7 +156,7 @@ describe(`client::PluginManager`, () => {
});
});

describe(`# await pluginManager.get(id)`, () => {
describe(`## async get(id)`, () => {
it(`should throw if called before server.init()`, async () => {
const client = new Client({ role: 'test', ...config });
client.pluginManager.register('delay', pluginDelayClient, { delayTime: 100 });
Expand Down Expand Up @@ -246,7 +246,7 @@ describe(`client::PluginManager`, () => {
// });
});

describe(`# pluginManager.onStateChange((states, updatedPlugin) => {})`, () => {
describe(`## onStateChange((states, updatedPlugin) => {})`, () => {
it(`should properly propagate statuses`, async function() {
this.timeout(3 * 1000);

Expand Down Expand Up @@ -302,7 +302,18 @@ describe(`client::PluginManager`, () => {
});
});

describe(`# plugin initialization lifecycle`, () => {
describe(`## getRegisteredPlugins()`, () => {
it(`should return the list of registered plugins`, () => {
const client = new Client({ role: 'test', ...config });
client.pluginManager.register('delay-1', pluginDelayClient, { delayTime: 0 });
client.pluginManager.register('delay-2', pluginDelayClient, { delayTime: 0 });

const registeredPlugins = client.pluginManager.getRegisteredPlugins();
assert.deepEqual(['delay-1', 'delay-2'], registeredPlugins)
});
});

describe(`## [lifecyle] plugin initialization`, () => {
it(`client should start if no plugins registered`, async function() {
const client = new Client({ role: 'test', ...config });
await client.init();
Expand Down Expand Up @@ -502,15 +513,4 @@ describe(`client::PluginManager`, () => {
}
});
});

describe(`# pluginManager.getRegisteredPlugins()`, () => {
it(`should return the list of registered plugins`, () => {
const client = new Client({ role: 'test', ...config });
client.pluginManager.register('delay-1', pluginDelayClient, { delayTime: 0 });
client.pluginManager.register('delay-2', pluginDelayClient, { delayTime: 0 });

const registeredPlugins = client.pluginManager.getRegisteredPlugins();
assert.deepEqual(['delay-1', 'delay-2'], registeredPlugins)
});
});
});
18 changes: 9 additions & 9 deletions tests/plugins/server.PluginManager.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import PluginManager from '../../src/server/PluginManager.js';
import pluginDelayServer from '../utils/PluginDelayServer.js';
import config from '../utils/config.js';

describe(`server::PluginManager`, () => {
describe(`# (protected) new PluginManager(server)`, () => {
describe(`# PluginManagerServer`, () => {
describe(`## [private] constructor(server)`, () => {
it(`should throw if argument is not instance of Server`, () => {
let errored = false;
try {
Expand All @@ -22,7 +22,7 @@ describe(`server::PluginManager`, () => {
});
});

describe(`# register(id, pluginFactory)`, () => {
describe(`## register(id, pluginFactory)`, () => {
let server;

beforeEach(() => {
Expand Down Expand Up @@ -121,7 +121,7 @@ describe(`server::PluginManager`, () => {
});
});

describe(`# [protected] await pluginManager.init()`, () => {
describe(`## [private] async init()`, () => {
it(`should throw if started twice`, async () => {
const server = new Server(config);
await server.init(); // run pluginManager.start()
Expand All @@ -137,7 +137,7 @@ describe(`server::PluginManager`, () => {
});
});

describe(`# await pluginManager.get(id)`, () => {
describe(`## async get(id)`, () => {
it(`should throw if called before server.init()`, async () => {
const server = new Server(config);
server.pluginManager.register('delay', pluginDelayServer, { delayTime: 100 });
Expand Down Expand Up @@ -229,7 +229,7 @@ describe(`server::PluginManager`, () => {
// });
});

describe(`# pluginManager.onStateChange((plugins, updatedPlugin) => {})`, () => {
describe(`## onStateChange((plugins, updatedPlugin) => {})`, () => {
it(`should properly propagate statuses`, async function() {
this.timeout(3 * 1000);

Expand Down Expand Up @@ -287,7 +287,7 @@ describe(`server::PluginManager`, () => {
});
});

describe(`# [protected] pluginManager.addClient(client)`, () => {
describe(`## [private] addClient(client)`, () => {
it(`should properly add clients to Plugin`, async () => {
let addClientCalled = false;

Expand Down Expand Up @@ -360,7 +360,7 @@ describe(`server::PluginManager`, () => {
});
});

describe(`# [protected] pluginManager.removeClient(client)`, () => {
describe(`## [private] removeClient(client)`, () => {
it(`should be called on client.stop()`, async () => {
let removeClientCalled = false;

Expand Down Expand Up @@ -467,7 +467,7 @@ describe(`server::PluginManager`, () => {
});
});

describe(`# plugin initialization lifecycle`, () => {
describe(`## [lifecycle] plugin initialization`, () => {
it(`server should start if no plugins registered`, async function() {
const server = new Server(config);
await server.init();
Expand Down
2 changes: 1 addition & 1 deletion tests/states/SharedStateCollection.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Client } from '../../src/client/index.js';
import config from '../utils/config.js';
import { a, b } from '../utils/schemas.js';

describe(`await getCollection()`, () => {
describe(`# SharedStateCollection`, () => {
let server;
let clients = [];

Expand Down
Loading

0 comments on commit f62bbda

Please sign in to comment.