-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow the transform function to be a promise
- Loading branch information
Ralph Huwiler
committed
Feb 8, 2018
1 parent
d781a16
commit b10c0ba
Showing
6 changed files
with
90 additions
and
39 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
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
This file was deleted.
Oops, something went wrong.
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,82 @@ | ||
'use strict' | ||
|
||
/** | ||
* adonis-bumblebee | ||
* | ||
* (c) Ralph Huwiler <ralph@huwiler.rocks> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
const test = require('japa') | ||
const { ioc } = require('@adonisjs/fold') | ||
|
||
const setup = require('./setup') | ||
const TransformerAbstract = require('../src/Bumblebee/TransformerAbstract') | ||
|
||
class BookTransformer extends TransformerAbstract { | ||
defaultInclude () { | ||
return [ | ||
'author' | ||
] | ||
} | ||
|
||
async transform (book) { | ||
return { | ||
id: book.id, | ||
title: book.title, | ||
year: book.yr | ||
} | ||
} | ||
|
||
async includeAuthor (book) { | ||
return this.item(book.author, author => ({name: author.name})) | ||
} | ||
} | ||
|
||
test.group('Promise', (group) => { | ||
group.before(async () => { | ||
await setup() | ||
}) | ||
|
||
test('data can be a promise and will resolve before transforming', async (assert) => { | ||
const Context = ioc.use('Adonis/Src/HttpContext') | ||
const {transform} = new Context() | ||
|
||
let data = new Promise((resolve, reject) => { | ||
setTimeout(resolve, 1, {item_id: 3}) | ||
}) | ||
|
||
let transformed = await transform | ||
.item(data, model => ({id: model.item_id})) | ||
|
||
assert.equal(transformed.id, 3) | ||
}) | ||
|
||
test('the transform function can be a promise', async (assert) => { | ||
const Context = ioc.use('Adonis/Src/HttpContext') | ||
const {transform} = new Context() | ||
|
||
let data = { | ||
id: 1, | ||
title: 'A Game of Thrones', | ||
yr: 1996, | ||
author: { | ||
name: 'George R. R. Martin' | ||
} | ||
} | ||
|
||
let transformed = await transform | ||
.item(data, BookTransformer) | ||
|
||
assert.deepEqual(transformed, { | ||
id: 1, | ||
title: 'A Game of Thrones', | ||
year: 1996, | ||
author: { | ||
name: 'George R. R. Martin' | ||
} | ||
}) | ||
}) | ||
}) |