From ccaf3a64b85cb9694cec3cb9fcaec42d6ac0b524 Mon Sep 17 00:00:00 2001 From: Artem Alexeyenko Date: Tue, 13 Dec 2022 17:46:33 -0500 Subject: [PATCH 1/2] sitecore-jss: first batch of tests --- packages/sitecore-jss/.nycrc | 5 +- .../sitecore-jss/src/graphql/graphql.test.ts | 17 ++++++ .../sitecore-jss/src/layout/utils.test.ts | 56 +++++++++++++++++++ 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 packages/sitecore-jss/src/layout/utils.test.ts diff --git a/packages/sitecore-jss/.nycrc b/packages/sitecore-jss/.nycrc index 95260f8fb4..c67b084bc6 100644 --- a/packages/sitecore-jss/.nycrc +++ b/packages/sitecore-jss/.nycrc @@ -3,6 +3,7 @@ ".ts" ], "exclude": [ + "**/index.ts", "**/*.d.ts", "**/*.test.ts", "src/test-data", @@ -11,7 +12,9 @@ "src/index.ts", "src/dataModels.ts", "./*.js", - "./*.d.ts" + "./*.d.ts", + "**/layout/models.ts", + "*.js" ], "all": true, "reporter": [ diff --git a/packages/sitecore-jss/src/graphql/graphql.test.ts b/packages/sitecore-jss/src/graphql/graphql.test.ts index ccfaf90743..ffd293b171 100644 --- a/packages/sitecore-jss/src/graphql/graphql.test.ts +++ b/packages/sitecore-jss/src/graphql/graphql.test.ts @@ -1,6 +1,7 @@ /* eslint-disable no-unused-expressions */ import { expect } from 'chai'; import { getAppRootId, siteNameError, languageError } from './app-root-query'; +import { SearchQueryService } from './search-service'; import { GraphQLRequestClient } from './../graphql-request-client'; import appRootQueryResponse from '../test-data/mockAppRootQueryResponse.json'; import nock from 'nock'; @@ -150,4 +151,20 @@ describe('graphql', () => { }); }); }); + + describe('search-service', () => { + const endpoint = 'http://site'; + const apiKey = 'api-key'; + const client = new GraphQLRequestClient(endpoint, { apiKey }); + const searchService = new SearchQueryService(client); + + it('should throw when rootItemId is missing', async () => { + const result = searchService.fetch('mockQuery', { + language: 'en', + }); + await result.catch((error: RangeError) => { + expect(error.message).to.equal('"rootItemId" must be a non-empty string'); + }); + }); + }); }); diff --git a/packages/sitecore-jss/src/layout/utils.test.ts b/packages/sitecore-jss/src/layout/utils.test.ts new file mode 100644 index 0000000000..18ee43363a --- /dev/null +++ b/packages/sitecore-jss/src/layout/utils.test.ts @@ -0,0 +1,56 @@ +/* eslint-disable no-unused-expressions */ +import { expect } from 'chai'; +import { ComponentRendering } from '../../layout'; +import { getFieldValue, getChildPlaceholder } from './utils'; + +describe('sitecore-jss layout utils', () => { + describe('getFieldValue', () => { + const fields = { + crop: { + value: 'rice', + }, + }; + + it('should read field from ComponentRendering type', () => { + const componentRendering: ComponentRendering = { + componentName: 'uTest', + fields: fields, + }; + + const result = getFieldValue(componentRendering, 'crop'); + expect(result).to.be.equal('rice'); + }); + + it('should read field from Fields type', () => { + expect(getFieldValue(fields, 'crop')).to.be.equal('rice'); + }); + + it('should return default value when field is not found', () => { + const defaultYield = '1000 tn'; + expect(getFieldValue(fields, 'yield', defaultYield)).to.be.equal(defaultYield); + }); + }); + + describe('getChildPlaceholder', () => { + it('should return child placeholder', () => { + const testRendering: ComponentRendering = { + componentName: 'test', + placeholders: { + place: [ + { + componentName: 'placed', + }, + ], + holder: [ + { + componentName: 'held', + }, + ], + }, + }; + const result = getChildPlaceholder(testRendering, 'place'); + expect(result.length).to.be.equal(1); + expect((result[0] as ComponentRendering).componentName).to.be.equal('placed'); + }); + }); +}); From 9053c46d98a642614ffb5db22a3ab6de7012763a Mon Sep 17 00:00:00 2001 From: Artem Alexeyenko Date: Fri, 16 Dec 2022 11:55:29 -0500 Subject: [PATCH 2/2] search service test tweak --- packages/sitecore-jss/src/graphql/graphql.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/sitecore-jss/src/graphql/graphql.test.ts b/packages/sitecore-jss/src/graphql/graphql.test.ts index ffd293b171..10fe010cd6 100644 --- a/packages/sitecore-jss/src/graphql/graphql.test.ts +++ b/packages/sitecore-jss/src/graphql/graphql.test.ts @@ -163,7 +163,7 @@ describe('graphql', () => { language: 'en', }); await result.catch((error: RangeError) => { - expect(error.message).to.equal('"rootItemId" must be a non-empty string'); + expect(error.message).to.equal('"rootItemId" and "language" must be non-empty strings'); }); }); });