Skip to content

Commit

Permalink
feat: add decorator for collectionAction
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlafroscia committed Apr 5, 2020
1 parent a1ed674 commit 8af9612
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
9 changes: 9 additions & 0 deletions addon/decorators.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import collectionOp, { CollectionOperationOptions } from './utils/collection-action';
import instanceOp, { InstanceOperationOptions } from './utils/member-action';

export function collectionAction<IN = any, OUT = any>(options: CollectionOperationOptions<IN, OUT>) {
return function createCollectionActionDescriptor() {
return {
value: collectionOp(options)
};
}
}

export function memberAction<IN = any, OUT = any>(options: InstanceOperationOptions<IN, OUT>) {
return function createMemberActionDescriptor() {
return {
Expand Down
52 changes: 52 additions & 0 deletions tests/unit/utils/decorators/collection-action-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { collectionAction } from 'ember-api-actions/decorators';
import DS from 'ember-data';
import { setupTest } from 'ember-qunit';
import Pretender from 'pretender';
import { module, test } from 'qunit';

class Fruit extends DS.Model {
@collectionAction({ path: 'ripenEverything' })
public ripenAll;
}

module('Unit | Utility | decorators/collection-action', (hooks) => {
setupTest(hooks);

let server: any;
hooks.beforeEach(() => {
server = new Pretender();
});
hooks.afterEach(() => {
server.shutdown();
});


hooks.beforeEach(function() {
this.owner.unregister('model:fruit');
this.owner.register('model:fruit', Fruit);

this.store = this.owner.lookup('service:store');

this.fruit = this.store.createRecord('fruit', {
id: 1,
name: 'apple'
});
});

test('it adds a method through a decorator', async function(assert) {
assert.expect(4);

server.put('/fruits/ripenEverything', (request: { url: string; requestBody: string }) => {
const data = JSON.parse(request.requestBody);
assert.deepEqual(data, { test: 'ok' }, 'collection action - request payload is correct');
assert.ok(true, 'request was made to "ripenEverything"');
return [200, {}, '{"status": "ok"}'];
});

assert.equal(typeof this.fruit.ripenAll, 'function', 'Assigns a method on the type');

const result = await this.fruit.ripenAll({ test: 'ok' });

assert.deepEqual(result, { status: 'ok' }, 'Passes through the API response');
});
});

0 comments on commit 8af9612

Please sign in to comment.