Skip to content
This repository has been archived by the owner on Mar 28, 2022. It is now read-only.

Commit

Permalink
feat(#109): Add loadRoutes method and new loadMocks method to plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
javierbrea committed Jan 16, 2021
1 parent 8718d87 commit 1068085
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 161 deletions.
26 changes: 14 additions & 12 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,26 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## [unreleased]
### Added
- feat: Add new plugin for loading routes and mocks in v2 format
- feat: Pass new method `loadRoutes` to plugins.
### Changed
- refactor: Move "path" and "watch" options inside files-loader plugin
- refactor: Move `path` and `watch` options inside files-loader plugin
- refactor: Reorganize files and folders
- refactor: Refactor Loaders to receive specific onLoad callback instead of full core instance
- refactor: Refactor Config to receive a single argument with all options in an object
- refactor: Refactor Config to receive a single argument with all options
- refactor: Refactor Plugins to receive a single argument with all options
### Fixed
### Removed
### BREAKING CHANGES
- feat: Remove deprecated options "features" and "behaviors", "path" option should be used instead.
- feat: Remove deprecated option "feature", "behavior" option should be used instead.
- feat: Legacy mocks defined using v1 format have to be loaded from folder defined using option "legacy-path" instead of "path", which now is used to define the folder from which load routes and mocks in v2 format. Folder defined with "legacy-path" option will not be created automatically if it is not found, and the option is not required.
- feat: Watching files for mocks in legacy v1 format has to be disabled using "legacy-watch" option instead of "watch", which now affects only to routes and mocks in v2 format.
- feat: "watch" option now is a standard commander boolean, so, to disable watch, argument "--no-watch" has to be provided. ("--no-legacy-watch" for legacy v1 mocks folder)
- feat: Remove "booleanString" option type. Now only "number", "boolean" or "string" can be used.
- feat: Remove deprecated "onLoadMocks" method, "onChangeMocks" must be used instead.
- feat: Remove "onLoadFiles" method. There is no alternative, as it is an internal event of the files-loader plugin and it should't be used by other external pieces.
- feat: Rename "loadMocks" plugins method to "loadLegacyMocks"
- feat: Rename "onChangeMocks" core method to "onChangeLegacyMocks"
- feat: Remove deprecated options `features` and `behaviors`, `path` option should be used instead.
- feat: Remove deprecated option `feature`, `behavior` option should be used instead.
- feat: Legacy mocks defined using v1 format have to be loaded from folder defined using option `legacy-path` instead of `path`, which now is used to define the folder from which load routes and mocks in v2 format. Folder defined with `legacy-path` option will not be created automatically if it is not found, and the option is not required.
- feat: Watching files for mocks in legacy v1 format has to be disabled using `legacy-watch` option instead of `watch`, which now affects only to routes and mocks in v2 format.
- feat: `watch` option now is a standard commander boolean, so, to disable watch, argument `--no-watch` has to be provided. (`--no-legacy-watch` for legacy v1 mocks folder)
- feat: Remove `booleanString` option type. Now only `number`, `boolean` or `string` can be used.
- feat: Remove deprecated `onLoadMocks` method, `onChangeMocks` must be used instead.
- feat: Remove `onLoadFiles` method. There is no alternative, as it is an internal event of the files-loader plugin and it should't be used by other external pieces.
- feat: Legacy mocks have to be loaded using plugins custom method `loadLegacyMocks`. `loadMocks` will be able to handle only v2 mocks.
- feat: Rename `onChangeMocks` core method to `onChangeLegacyMocks`

## [1.6.0] - 2020-12-25

Expand Down
47 changes: 38 additions & 9 deletions src/Core.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const {
CHANGE_SETTINGS,
CHANGE_ALERTS,
LOAD_LEGACY_MOCKS,
LOAD_MOCKS,
LOAD_ROUTES,
} = require("./eventNames");
const tracer = require("./tracer");

Expand All @@ -37,10 +39,8 @@ class Core {
constructor(programmaticConfig) {
this._eventEmitter = new EventEmitter();

// TODO, refactor all pieces as the next one. They should receive callbacks
// They should never access directly to the full core
// (expect in cases where it is needed to be passed to plugins or another external pieces)
this._alerts = new Alerts({
// TODO, rename into onChange
onChangeValues: (alerts) => {
this._eventEmitter.emit(CHANGE_ALERTS, alerts);
},
Expand All @@ -52,20 +52,48 @@ class Core {
},
});

this._mocksLoaders = new Loaders({
onLoad: () => {
this._eventEmitter.emit(LOAD_MOCKS);
},
});

this._routesLoaders = new Loaders({
onLoad: () => {
this._eventEmitter.emit(LOAD_ROUTES);
},
});

this._config = new Config({
programmaticConfig,
...scopedAlertsMethods("config", this._alerts.add, this._alerts.remove),
});

// TODO, refactor. Pass specific callbacks instead of objects
this._settings = new Settings(this._eventEmitter, this._config);

this._plugins = new Plugins(
this._config,
this._legacyMocksLoaders,
this,
scopedAlertsMethods("plugins", this._alerts.add, this._alerts.remove, this._alerts.rename)
{
createLegacyMocksLoader: () => {
return this._legacyMocksLoaders.new();
},
createMocksLoader: () => {
return this._mocksLoaders.new();
},
createRoutesLoader: () => {
return this._routesLoaders.new();
},
...scopedAlertsMethods(
"plugins",
this._alerts.add,
this._alerts.remove,
this._alerts.rename
),
},
this //To be used only by plugins
);

// TODO, remove
this._legacyMocks = new LegacyMocks(
this._eventEmitter,
this._settings,
Expand All @@ -74,6 +102,7 @@ class Core {
scopedAlertsMethods("legacy-mocks", this._alerts.add, this._alerts.remove)
);

// TODO, refactor. Pass specific callbacks instead of objects
this._server = new Server(
this._eventEmitter,
this._settings,
Expand All @@ -82,7 +111,7 @@ class Core {
scopedAlertsMethods("server", this._alerts.add, this._alerts.remove)
);

// TODO, rename into eventsOrchestrator, convert into a function
// TODO, remove, add orchestration event listeners here
this._orchestrator = new Orchestrator(this._eventEmitter, this._legacyMocks, this._server);

this._inited = false;
Expand Down Expand Up @@ -117,7 +146,7 @@ class Core {
await this._config.init(options);
this._inited = true;
// Register plugins, let them add their custom settings
await this._plugins.register();
await this._plugins.register(this._config.coreOptions.plugins);
// Init settings, read command line arguments, etc.
await this._settings.init(this._config.options);
// Settings are ready, init all
Expand Down
2 changes: 2 additions & 0 deletions src/eventNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ module.exports = {
START: "start",
STOP: "stop",
LOAD_LEGACY_MOCKS: "load:mocks:legacy",
LOAD_MOCKS: "load:mocks",
LOAD_ROUTES: "load:routes",
CHANGE_LEGACY_MOCKS: "change:mocks:legacy",
CHANGE_SETTINGS: "change:settings",
CHANGE_ALERTS: "change:alerts",
Expand Down
27 changes: 21 additions & 6 deletions src/plugins/Plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,24 @@ const FilesLoader = require("./files-loader/FilesLoader");
const { scopedAlertsMethods } = require("../support/helpers");

class Plugins {
constructor(config, loaders, core, { addAlert, removeAlerts, renameAlerts }) {
constructor(
{
addAlert,
removeAlerts,
renameAlerts,
createLegacyMocksLoader,
createMocksLoader,
createRoutesLoader,
},
core
) {
this._addAlert = addAlert;
this._removeAlerts = removeAlerts;
this._renameAlerts = renameAlerts;
this._config = config;
this._createMocksLoader = createMocksLoader;
this._createRoutesLoader = createRoutesLoader;
this._createLegacyMocksLoader = createLegacyMocksLoader;
this._core = core;
this._loaders = loaders;
this._pluginsInstances = [];
this._pluginsMethods = [];
this._pluginsRegistered = 0;
Expand All @@ -35,8 +46,8 @@ class Plugins {
this._pluginsStopped = 0;
}

register() {
this._plugins = this._config.coreOptions.plugins || [];
register(plugins = []) {
this._plugins = plugins;
this._plugins.unshift(FilesLoader);
this._plugins.unshift(FilesLoaderLegacy);
return this._registerPlugins().then(() => {
Expand Down Expand Up @@ -121,9 +132,13 @@ class Plugins {
if (pluginIndex === this._plugins.length) {
return Promise.resolve();
}
const loadLegacyMocks = this._loaders.new();
const loadLegacyMocks = this._createLegacyMocksLoader();
const loadMocks = this._createMocksLoader();
const loadRoutes = this._createRoutesLoader();
const pluginMethods = {
loadLegacyMocks,
loadMocks,
loadRoutes,
...scopedAlertsMethods(
() => this.pluginDisplayName(pluginIndex),
this._addAlert,
Expand Down
56 changes: 55 additions & 1 deletion test/unit/Core.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,38 @@ describe("Core", () => {
});
});

describe("Plugins callbacks", () => {
describe("createLegacyMocksLoader", () => {
it("should return a new loader", () => {
const FOO_LOADER = "foo";
loadersMocks.stubs.instance.new.returns(FOO_LOADER);
expect(pluginsMocks.stubs.Constructor.mock.calls[0][0].createLegacyMocksLoader()).toEqual(
FOO_LOADER
);
});
});

describe("createMocksLoader", () => {
it("should return a new loader", () => {
const FOO_LOADER = "foo";
loadersMocks.stubs.instance.new.returns(FOO_LOADER);
expect(pluginsMocks.stubs.Constructor.mock.calls[0][0].createMocksLoader()).toEqual(
FOO_LOADER
);
});
});

describe("createRoutesLoader", () => {
it("should return a new loader", () => {
const FOO_LOADER = "foo";
loadersMocks.stubs.instance.new.returns(FOO_LOADER);
expect(pluginsMocks.stubs.Constructor.mock.calls[0][0].createRoutesLoader()).toEqual(
FOO_LOADER
);
});
});
});

describe("init method", () => {
it("should init only once", async () => {
await core.init();
Expand Down Expand Up @@ -253,7 +285,7 @@ describe("Core", () => {
});
});

describe("when legacyMocksLoaders load something", () => {
describe("when legacyMocksLoaders load", () => {
it("should emit an event", (done) => {
expect.assertions(1);
core._eventEmitter.on("load:mocks:legacy", () => {
Expand All @@ -264,6 +296,28 @@ describe("Core", () => {
});
});

describe("when mocksLoaders load", () => {
it("should emit an event", (done) => {
expect.assertions(1);
core._eventEmitter.on("load:mocks", () => {
expect(true).toEqual(true);
done();
});
loadersMocks.stubs.Constructor.mock.calls[1][0].onLoad();
});
});

describe("when routesLoaders load", () => {
it("should emit an event", (done) => {
expect.assertions(1);
core._eventEmitter.on("load:routes", () => {
expect(true).toEqual(true);
done();
});
loadersMocks.stubs.Constructor.mock.calls[2][0].onLoad();
});
});

describe("stop method", () => {
it("should stop server", async () => {
await core.stop();
Expand Down
Loading

0 comments on commit 1068085

Please sign in to comment.