Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sitecore-jss extra unit test coverage #1262

Merged
merged 2 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/sitecore-jss/.nycrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
".ts"
],
"exclude": [
"**/index.ts",
"**/*.d.ts",
"**/*.test.ts",
"src/test-data",
Expand All @@ -11,7 +12,9 @@
"src/index.ts",
"src/dataModels.ts",
"./*.js",
"./*.d.ts"
"./*.d.ts",
"**/layout/models.ts",
"*.js"
],
"all": true,
"reporter": [
Expand Down
17 changes: 17 additions & 0 deletions packages/sitecore-jss/src/graphql/graphql.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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" and "language" must be non-empty strings');
});
});
});
});
56 changes: 56 additions & 0 deletions packages/sitecore-jss/src/layout/utils.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
});