Skip to content

Commit

Permalink
fix(AppCtxHandlers): change xxxHandlers get props to return Iterators…
Browse files Browse the repository at this point in the history
… instead of the underlying Sets

affects: tao

this improves the closed privacy of AppCtxHandlers preventing external manipulation
  • Loading branch information
eudaimos committed May 10, 2018
1 parent b17cba0 commit 96e16eb
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 9 deletions.
16 changes: 16 additions & 0 deletions config/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,22 @@ const getMatchers = () => ({
};

return { actual: received, pass, message };
},
toBeIterable(received, ignoreStrings = false) {
const exists = received != null;
const isIterable =
exists && typeof received[Symbol.iterator] === 'function';
const isMatch = !ignoreStrings
? isIterable
: typeof received === 'string' ? false : isIterable;
const message = () =>
this.utils.matcherHint(`${isMatch ? '.not' : ''}.toBeIterable`) +
'\n\n' +
`Expected value ${isMatch ? 'not ' : ''}to be Iterable${
ignoreStrings ? ' (ignoring strings)' : ''
}, instead received:\n` +
` ${this.utils.printReceived(received)}`;
return { actual: received, pass: isMatch, message };
}
});

Expand Down
11 changes: 8 additions & 3 deletions packages/tao/src/AppCtxHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import AppCtxRoot from './AppCtxRoot';
import AppCtx from './AppCtx';
import { isIterable } from './utils';

const console = {
error: () => 1,
log: () => 1
};

export default class AppCtxHandlers extends AppCtxRoot {
constructor(term, action, orient, leafAppConHandlers) {
super(term, action, orient);
Expand Down Expand Up @@ -98,15 +103,15 @@ export default class AppCtxHandlers extends AppCtxRoot {
// populateHandlersFromWildcards() {}

get interceptHandlers() {
return this._intercept;
return this._intercept.values();
}

get asyncHandlers() {
return this._async;
return this._async.values();
}

get inlineHandlers() {
return this._inline;
return this._inline.values();
}

async handleAppCon(ac, setAppCtx) {
Expand Down
37 changes: 37 additions & 0 deletions packages/tao/src/AppCtxHandlers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ describe('AppCtxHandlers exports a class extending AppCtxRoot', () => {

describe('AppCtxHandlers is used to attach handlers for Application Contexts', () => {
describe('as Inline handlers', () => {
it('should expose an inlineHandlers iterable property that is not the underlying collection', () => {
// Assemble
const uut = new AppCtxHandlers(TERM, ACTION, ORIENT);
// Act
// Assert
expect(uut.inlineHandlers).toBeDefined();
expect(uut.inlineHandlers).toBeIterable(true);
expect(uut.inlineHandlers).not.toBeInstanceOf(Set);
expect(uut.inlineHandlers).not.toBeInstanceOf(Array);
expect(uut.inlineHandlers).not.toBeInstanceOf(Map);
});

it('should throw if handler is not a function', () => {
// Assemble
const uut = new AppCtxHandlers();
Expand All @@ -58,6 +70,7 @@ describe('AppCtxHandlers is used to attach handlers for Application Contexts', (
// Assemble
const uut = new AppCtxHandlers(TERM, ACTION, ORIENT);
const handler = jest.fn();
// const beforeAttach = Array.from()
// Act
uut.addInlineHandler(handler);
// Assert
Expand Down Expand Up @@ -240,6 +253,18 @@ describe('AppCtxHandlers is used to attach handlers for Application Contexts', (
});

describe('as Async handlers', () => {
it('should expose an asyncHandlers iterable property that is not the underlying collection', () => {
// Assemble
const uut = new AppCtxHandlers(TERM, ACTION, ORIENT);
// Act
// Assert
expect(uut.asyncHandlers).toBeDefined();
expect(uut.asyncHandlers).toBeIterable(true);
expect(uut.asyncHandlers).not.toBeInstanceOf(Set);
expect(uut.asyncHandlers).not.toBeInstanceOf(Array);
expect(uut.asyncHandlers).not.toBeInstanceOf(Map);
});

it('should throw if handler is not a function', () => {
// Assemble
const uut = new AppCtxHandlers();
Expand Down Expand Up @@ -464,6 +489,18 @@ describe('AppCtxHandlers is used to attach handlers for Application Contexts', (
});

describe('as Intercept handlers', () => {
it('should expose an interceptHandlers iterable property that is not the underlying collection', () => {
// Assemble
const uut = new AppCtxHandlers(TERM, ACTION, ORIENT);
// Act
// Assert
expect(uut.interceptHandlers).toBeDefined();
expect(uut.interceptHandlers).toBeIterable(true);
expect(uut.interceptHandlers).not.toBeInstanceOf(Set);
expect(uut.interceptHandlers).not.toBeInstanceOf(Array);
expect(uut.interceptHandlers).not.toBeInstanceOf(Map);
});

it('should throw if handler is not a function', () => {
// Assemble
const uut = new AppCtxHandlers();
Expand Down
12 changes: 6 additions & 6 deletions packages/tao/src/Kernel.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -981,14 +981,14 @@ describe('Kernel is the base entry point of execution for a tao.js app', () => {
const rejectHandlers = postResolveHandlers.get(rejectOnAc.key);
// Assert
expect(triggerHandlers).toBeDefined();
expect(triggerHandlers.inlineHandlers).toBeInstanceOf(Set);
expect(triggerHandlers.inlineHandlers.size).toBe(1);
expect(triggerHandlers.inlineHandlers).toBeIterable(true);
expect(Array.from(triggerHandlers.inlineHandlers).length).toBe(1);
expect(resolveHandlers).toBeDefined();
expect(resolveHandlers.inlineHandlers).toBeInstanceOf(Set);
expect(resolveHandlers.inlineHandlers.size).toBe(0);
expect(resolveHandlers.inlineHandlers).toBeIterable(true);
expect(Array.from(resolveHandlers.inlineHandlers).length).toBe(0);
expect(rejectHandlers).toBeDefined();
expect(rejectHandlers.inlineHandlers).toBeInstanceOf(Set);
expect(rejectHandlers.inlineHandlers.size).toBe(0);
expect(rejectHandlers.inlineHandlers).toBeIterable(true);
expect(Array.from(rejectHandlers.inlineHandlers).length).toBe(0);
});
});
});

0 comments on commit 96e16eb

Please sign in to comment.