Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rogerhutchings committed Sep 9, 2019
1 parent 63a016f commit 8eac666
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 15 deletions.
@@ -0,0 +1,21 @@
# fetchWorkflowsHelper

An async helper method to fetch the data required to generate the workflow select buttons for the Hero component. Grabs the workflow and translation data for a given array of workflow IDs, and returns a collection of workflow objects.

## Arguments

- `language` - A two-letter language code to fetch the correct translation data from Panoptes. Defaults to `en`
- `activeWorkflows` (required) - an array of string IDs used to fetch the required workflows from Panoptes
- `defaultWorkflow` - the ID of the default workflow, which will be shown at `/projects/:project/:owner:/classify`

## Example

```js
const workflows = await fetchWorkflowsHelper('en', ['1', '2'], '2')

// returns
// [
// { completeness: 0.4, default: false, id: '1', displayName: 'Foo' },
// { completeness: 0.7, default: true, id: '2', displayName: 'Bar' }
// ]
```
@@ -1,7 +1,4 @@
import { panoptes } from '@zooniverse/panoptes-js'
import find from 'lodash/find'
import pick from 'lodash/find'
import toNumber from 'lodash/toNumber'

function fetchWorkflowData (activeWorkflows) {
return panoptes
Expand All @@ -19,15 +16,14 @@ function fetchDisplayNames (language, activeWorkflows) {
.get('/translations', {
fields: 'strings,translated_id',
language,
translated_id: activeWorkflows.join(','),
translated_type: 'workflow'
'translated_id': activeWorkflows.join(','),
'translated_type': 'workflow'
})
.then(response => response.body.translations)
.then(createDisplayNamesMap)
}

async function fetchWorkflowsHelper(language = 'en', activeWorkflows, defaultWorkflow) {
try {
const workflows = await fetchWorkflowData(activeWorkflows)
const workflowIds = workflows.map(workflow => workflow.id)
const displayNames = await fetchDisplayNames(language, workflowIds)
Expand All @@ -42,10 +38,6 @@ async function fetchWorkflowsHelper(language = 'en', activeWorkflows, defaultWor
displayName: displayNames[workflow.id]
}
})
} catch (error) {
console.error(error)
return error
}
}

function pickWorkflowFields (workflow) {
Expand All @@ -59,7 +51,7 @@ function createDisplayNamesMap (translations) {
const map = {}
translations.forEach(translation => {
const workflowId = translation.translated_id.toString()
map[workflowId] = translation.strings.display_name
map[workflowId] = translation.strings['display_name']
})
return map
}
Expand Down
Expand Up @@ -13,7 +13,7 @@ const WORKFLOWS = [
}
]

// `translated_id` is an number because of a bug in the translations API :(
// `translated_id` is a number because of a bug in the translations API :(
const TRANSLATIONS = [
{
translated_id: 1,
Expand All @@ -29,7 +29,7 @@ const TRANSLATIONS = [
}
]

describe('Helpers > fetchWorkflowsHelper', function () {
describe.only('Helpers > fetchWorkflowsHelper', function () {
it('should provide the expected result with a single workflow', async function () {
const scope = nock('https://panoptes-staging.zooniverse.org/api')
.get('/translations')
Expand Down Expand Up @@ -62,11 +62,59 @@ describe('Helpers > fetchWorkflowsHelper', function () {
workflows: WORKFLOWS
})

const result = await fetchWorkflowsHelper('en', ['1', '2'], '2')
const result = await fetchWorkflowsHelper('en', ['1', '2'])
expect(result).to.deep.equal([
{ completeness: 0.4, default: false, id: '1', displayName: 'Foo' },
{ completeness: 0.7, default: true, id: '2', displayName: 'Bar' }
{ completeness: 0.7, default: false, id: '2', displayName: 'Bar' }
])
})

describe('when there is a `defaultWorkflow` provided', function () {
it('should provide the expected result with multiple workflows', async function () {
const scope = nock('https://panoptes-staging.zooniverse.org/api')
.get('/translations')
.query(true)
.reply(200, {
translations: TRANSLATIONS
})
.get('/workflows')
.query(true)
.reply(200, {
workflows: WORKFLOWS
})

const result = await fetchWorkflowsHelper('en', ['1', '2'], '2')
expect(result).to.deep.equal([
{ completeness: 0.4, default: false, id: '1', displayName: 'Foo' },
{ completeness: 0.7, default: true, id: '2', displayName: 'Bar' }
])
})
})

describe.only(`when there's an error`, function () {
it('should allow the error to be thrown for the consumer to handle', async function () {
const error = {
message: 'oh dear. oh dear god'
}
const scope = nock('https://panoptes-staging.zooniverse.org/api')
.get('/translations')
.query(true)
.replyWithError(error)
.get('/workflows')
.query(true)
.reply(200, {
workflows: WORKFLOWS
})

try {
await fetchWorkflowsHelper('en', ['1', '2'], '2')
expect.fail()
} catch (error) {
expect(error).to.deep.equal({
...error,
response: undefined
})
}
})
})
})

0 comments on commit 8eac666

Please sign in to comment.