Skip to content

Commit

Permalink
Added tests for utilities functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jermnelson committed Sep 19, 2019
1 parent 28813fa commit 219bf03
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
2 changes: 1 addition & 1 deletion __tests__/utilities/propertyTemplates.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
defaultValuesFromPropertyTemplate,
getLookupConfigItems,
getTagNameForPropertyTemplate,
} from '../../src/utilities/propertyTemplates'
} from 'utilities/propertyTemplates'

import shortid from 'shortid'

Expand Down
35 changes: 33 additions & 2 deletions __tests__/utilities/qa.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright 2019 Stanford University see LICENSE for license

import { isContext } from 'utilities/qa'

import getSearchResults, { isContext } from 'utilities/qa'

describe('isContext()', () => {
// check context property is picked up and when property is not present, method returns false
Expand All @@ -13,3 +12,35 @@ describe('isContext()', () => {
expect(isContext({})).toBeFalsy()
})
})

describe('getSearchResults()', () => {
const template = {
valueConstraint: {
useValuesFrom: [
'urn:ld4p:qa:agrovoc',
],
},
}

it('returns an array of values from a search', () => {
expect.assertions(1)

return getSearchResults('Corn', template).then((result) => {
expect(result[0].body.length).toEqual(8)
})
})

it('returns a single value using a bad authority source', () => {
const badAuthorityURItemplate = {
valueConstraint: {
useValuesFrom: [
'urn:ld4p:qa:names:organization',
],
},
}

return getSearchResults('Austin, Jane', badAuthorityURItemplate).catch((result) => {
expect(result[0].statusCode).toEqual(400)
})
})
})
5 changes: 3 additions & 2 deletions src/utilities/qa.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Config from 'Config'
const getSearchResults = (query, propertyTemplate) => Swagger({ spec: swaggerSpec })
.then((client) => {
const lookupConfigs = getLookupConfigItems(propertyTemplate)

// Create array of promises based on the lookup config array that is sent in
const lookupPromises = createLookupPromises(client, query, lookupConfigs, isContext(propertyTemplate))

Expand All @@ -34,11 +35,11 @@ const getSearchResults = (query, propertyTemplate) => Swagger({ spec: swaggerSpe

export const isContext = propertyTemplate => propertyTemplate?.subtype === 'context'

const createLookupPromises = (client, query, lookupConfigs, context) => lookupConfigs.map((lookupConfig) => {
export const createLookupPromises = (client, query, lookupConfigs, context) => lookupConfigs.map((lookupConfig) => {
const authority = lookupConfig.authority
const subauthority = lookupConfig.subauthority
const language = lookupConfig.language

/*
* There are two types of lookup: linked data and non-linked data. The API calls
* for each type are different, so check the nonldLookup field in the lookup config.
Expand Down

0 comments on commit 219bf03

Please sign in to comment.