From 219bf03a6a5586cba888b98efe535a0da08c2ded Mon Sep 17 00:00:00 2001 From: Jeremy Nelson Date: Thu, 19 Sep 2019 13:10:21 +0200 Subject: [PATCH] Added tests for utilities functions --- __tests__/utilities/propertyTemplates.test.js | 2 +- __tests__/utilities/qa.test.js | 35 +++++++++++++++++-- src/utilities/qa.js | 5 +-- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/__tests__/utilities/propertyTemplates.test.js b/__tests__/utilities/propertyTemplates.test.js index b22c9c933..aa20be7aa 100644 --- a/__tests__/utilities/propertyTemplates.test.js +++ b/__tests__/utilities/propertyTemplates.test.js @@ -4,7 +4,7 @@ import { defaultValuesFromPropertyTemplate, getLookupConfigItems, getTagNameForPropertyTemplate, -} from '../../src/utilities/propertyTemplates' +} from 'utilities/propertyTemplates' import shortid from 'shortid' diff --git a/__tests__/utilities/qa.test.js b/__tests__/utilities/qa.test.js index ca3e46508..d1c73cdd8 100644 --- a/__tests__/utilities/qa.test.js +++ b/__tests__/utilities/qa.test.js @@ -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 @@ -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) + }) + }) +}) diff --git a/src/utilities/qa.js b/src/utilities/qa.js index abd1b147e..2808c8bf7 100644 --- a/src/utilities/qa.js +++ b/src/utilities/qa.js @@ -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)) @@ -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.