Skip to content

Commit

Permalink
Merge 193ea92 into a7e3404
Browse files Browse the repository at this point in the history
  • Loading branch information
rthewhite committed Nov 5, 2015
2 parents a7e3404 + 193ea92 commit ebe30fe
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,20 @@ export function AsyncAction(actionName) {
};
}

export function Action(actionName) {
export function Action(actionName, useInstanceDispatcher) {
return (cls, methodName, descriptor) => {
const originalMethod = descriptor.value;
descriptor.value = function(...payload) {
const action = prepareActionName(cls, actionName, methodName);
const originalReturn = Reflect.apply(originalMethod, this, payload);
const dispatchPromise = this.dispatch(action, ...payload);

let dispatchPromise;
if (useInstanceDispatcher) {
dispatchPromise = this.instance.dispatch(action, ...payload);
} else {
dispatchPromise = this.dispatch(action, ...payload);
}

return angular.isDefined(originalReturn) ? originalReturn : dispatchPromise;
};
};
Expand Down
1 change: 1 addition & 0 deletions src/behaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from './behaviors/filterable-store';
export * from './behaviors/searchable-component';
export * from './behaviors/sortable-store';
export * from './behaviors/sortable-component';
export * from './behaviors/paginatable-actions';
export * from './behaviors/paginatable-store';
export * from './behaviors/paginatable-component';
export * from './behaviors/transformable';
23 changes: 23 additions & 0 deletions src/behaviors/paginatable-actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {Behavior} from './behavior';
import {addBehavior, Inject} from '../utils';
import {Action} from '../actions';

export class PaginatableActionsBehavior extends Behavior {
@Inject('ApplicationDispatcher') dispatch
constructor(instance) {
super(...arguments);
this.dispatchInstance = instance;
}

@Action('changeLimit', true) changeLimit() {}
@Action('changePage', true) changePage() {}
}

export function PaginatableActions() {
return cls => {
addBehavior(cls, PaginatableActionsBehavior, {
property: 'paginatableActions',
proxy: ['changeLimit', 'changePage']
});
};
}
74 changes: 74 additions & 0 deletions src/behaviors/paginatable-actions.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*eslint-env node, jasmine*//*global module inject*/
/*eslint-disable max-statements, max-params*/
import angular from 'angular';
import 'angular-mocks';
import 'luxyflux/ng-luxyflux';

import {
Annotations,
Actions,
PaginatableActions,
PaginatableActionsBehavior
} from 'anglue/anglue';

describe('PaginatableActions', () => {
// Clear the AnnotationCache for unit tests to ensure we create new annotations for each class.
beforeEach(() => {
Annotations.clear();
});

describe('PaginatableActionsBehavior', () => {
let mockInstance, behavior;

beforeEach(() => {
behavior = new PaginatableActionsBehavior(mockInstance);
});

it('should have a changeLimit function', () => {
expect(behavior.changeLimit).toBeDefined();
});

it('should have a changePage function', () => {
expect(behavior.changePage).toBeDefined();
});
});

describe('@PaginatableActions() decorator', () => {
@Actions()
@PaginatableActions()
class PaginatableTestActions {}

let testActions;
let appDispatcher;

class MockApplicationDispatcher {
dispatch() {}
}

angular
.module('paginatableActionsApp', [
'luxyflux',
PaginatableTestActions.annotation.module.name
])
.service('ApplicationDispatcher', [
function () {
return new MockApplicationDispatcher();
}
]);

beforeEach(module('paginatableActionsApp'));
beforeEach(inject((_PaginatableTestActions_, _ApplicationDispatcher_) => {
testActions = _PaginatableTestActions_;
appDispatcher = _ApplicationDispatcher_;

spyOn(appDispatcher, 'dispatch');
}));


it('should dispatch an action', () => {
expect(testActions.changeLimit(1));
expect(appDispatcher.dispatch).toHaveBeenCalledWith('CHANGELIMIT', 1);
});

});
});

0 comments on commit ebe30fe

Please sign in to comment.