-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add decorator for
collectionAction
- Loading branch information
1 parent
60cdcf2
commit fa5736d
Showing
2 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { collectionAction } from 'ember-api-actions/decorators'; | ||
import DS from 'ember-data'; | ||
import { setupTest } from 'ember-qunit'; | ||
import { module, test } from 'qunit'; | ||
import setupPretender from '../../../helpers/setup-pretender'; | ||
|
||
class Fruit extends DS.Model { | ||
@collectionAction({ path: 'ripenEverything' }) | ||
public ripenAll; | ||
} | ||
|
||
module('Unit | Utility | decorators/collection-action', (hooks) => { | ||
setupTest(hooks); | ||
setupPretender(hooks); | ||
|
||
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); | ||
|
||
this.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'); | ||
}); | ||
}); |