Skip to content

Commit

Permalink
feat(prettier-plugin-jsdoc): expose the container
Browse files Browse the repository at this point in the history
  • Loading branch information
homer0 committed Oct 28, 2020
1 parent 633f6de commit d7dc8ef
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
/* eslint-disable jsdoc/require-jsdoc */
const path = require('path');

const registry = new Map();
const container = new Map();
const providerKey = Symbol('provider-secret');
/**
* Adds a new function to the app registry.
* Adds a new function to the app container.
*
* @param {Function} originalFn The original function to add.
*/
const addFn = (originalFn) => {
registry.set(originalFn, originalFn);
container.set(originalFn, originalFn);
};
/**
* Overrides a function on the app registry.
* Overrides a function on the app container.
*
* @param {OG} originalFn The reference to the original function that will be overriden.
* @param {OG} fn The override function.
* @template OG
*/
const setFn = (originalFn, fn) => {
registry.set(originalFn, fn);
container.set(originalFn, fn);
};
/**
* Gets a function or a function override from the app registry.
* Gets a function or a function override from the app container.
*
* @param {OG} originalFn The reference to the original function.
* @returns {OG}
* @template OG
*/
const getFn = (originalFn) => registry.get(originalFn) || originalFn;
const getFn = (originalFn) => container.get(originalFn) || originalFn;

const registerModule = (id, fns) => {
const useFns = Array.isArray(fns) ? fns : Object.values(fns);
Expand Down Expand Up @@ -61,6 +61,7 @@ const loadProviders = (directoryPath, list) => list
module.exports.addFn = addFn;
module.exports.setFn = setFn;
module.exports.getFn = getFn;
module.exports.container = container;
module.exports.registerModule = registerModule;
module.exports.provider = provider;
module.exports.loadProviders = loadProviders;
138 changes: 138 additions & 0 deletions test/unit/app.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
jest.unmock('../../src/app');
jest.mock('../../src/fns/sortTags', () => ({
provider: jest.fn(),
}));
jest.mock('../../src/fns/splitText', () => ({
provider: jest.fn(),
}));

const path = require('path');
const {
addFn,
setFn,
getFn,
container,
registerModule,
provider,
loadProviders,
} = require('../../src/app');

const { provider: sortTagsProvider } = require('../../src/fns/sortTags');
const { provider: splitTextProvider } = require('../../src/fns/splitText');

describe('app', () => {
beforeEach(() => {
sortTagsProvider.mockReset();
splitTextProvider.mockReset();
});

afterEach(() => {
container.clear();
});

it('should register a function on the container', () => {
// Given
const originalValue = 'original!';
const originalFn = jest.fn(() => originalValue);
let sut = null;
let result = null;
// When
addFn(originalFn);
sut = getFn(originalFn);
result = sut();
// Then
expect(result).toBe(originalValue);
});

it('should return the same function when is not registered on the container', () => {
// Given
const originalValue = 'original!';
const originalFn = jest.fn(() => originalValue);
let sut = null;
let result = null;
// When
sut = getFn(originalFn);
result = sut();
// Then
expect(result).toBe(originalValue);
});

it('should register an override for a function', () => {
// Given
const originalValue = 'original!';
const originalFn = jest.fn(() => originalValue);
const customValue = 'custom!';
const customFn = jest.fn(() => customValue);
let result = null;
let resultAfterOverride = null;
// When
addFn(originalFn);
result = getFn(originalFn)();
setFn(originalFn, customFn);
resultAfterOverride = getFn(originalFn)();
// Then
expect(result).toBe(originalValue);
expect(resultAfterOverride).toBe(customValue);
});

it('should register a list of module functions', () => {
// Given
const fn1 = () => {};
const fn2 = () => {};
const fns = [fn1, fn2];
const id = 'myMod';
let result = null;
// When
registerModule(id, fns);
result = [...container.keys()];
// Then
expect(result).toEqual(fns);
expect(fn1.moduleId).toBe(id);
expect(fn2.moduleId).toBe(id);
});

it('should register a dictionary of module functions', () => {
// Given
const fn1 = () => {};
const fn2 = () => {};
const fns = { fn1, fn2 };
const id = 'myMod';
let result = null;
// When
registerModule(id, fns);
result = [...container.keys()];
// Then
expect(result).toEqual(Object.values(fns));
expect(fn1.moduleId).toBe(id);
expect(fn2.moduleId).toBe(id);
});

it('should generate a module provider function', () => {
// Given
const fn1 = () => {};
const fn2 = () => {};
const fns = [fn1, fn2];
const id = 'myMod';
let sut = null;
let result = null;
// When
sut = provider(id, fns);
sut();
result = [...container.keys()];
// Then
expect(result).toEqual(fns);
expect(fn1.moduleId).toBe(id);
expect(fn2.moduleId).toBe(id);
});

it('should load and execute the providers of a list of modules', () => {
// Given
const directory = path.join(__dirname, '..', '..', 'src', 'fns');
const mods = ['sortTags', 'splitText'];
// When
loadProviders(directory, mods);
// Then
expect(sortTagsProvider).toHaveBeenCalledTimes(1);
expect(splitTextProvider).toHaveBeenCalledTimes(1);
});
});

0 comments on commit d7dc8ef

Please sign in to comment.