-
Notifications
You must be signed in to change notification settings - Fork 275
/
graphql.test.ts
170 lines (147 loc) · 5.63 KB
/
graphql.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* 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';
import { SitecoreTemplateId } from '../constants';
// Todo: Instead of creating a GraphQLRequestClient instance, we can use the
// GraphQLClient interface. This would be a better mock. Potentially create a
// 'internal/sitecore-test project for such helpers.
describe('graphql', () => {
describe('getAppRootId', () => {
const endpoint = 'http://site';
const apiKey = 'api-key';
const queryNameFilter = new RegExp('AppRootQuery');
const client = new GraphQLRequestClient(endpoint, { apiKey });
afterEach(() => {
nock.cleanAll();
});
describe('should return', () => {
it('valid app root ID', async () => {
nock(endpoint, { reqheaders: { sc_apikey: apiKey } })
.post('/', queryNameFilter)
.reply(200, appRootQueryResponse);
const result = await getAppRootId(client, 'siteName', 'language');
expect(result).to.equal('GUIDGUIDGUID');
});
it('valid root id using fallback language as en', async () => {
nock(endpoint, { reqheaders: { sc_apikey: apiKey } })
.post('/', (body) => {
return body.variables.language === 'language';
})
.reply(200, {
data: {
layout: {
homePage: {
rootItem: [],
},
},
},
});
nock(endpoint, { reqheaders: { sc_apikey: apiKey } })
.post('/', (body) => {
return body.variables.language === 'en';
})
.reply(200, appRootQueryResponse);
const result = await getAppRootId(client, 'siteName', 'language');
expect(result).to.equal('GUIDGUIDGUID');
});
it('null for the passed language and for fallback language en', async () => {
nock(endpoint, { reqheaders: { sc_apikey: apiKey } })
.post('/', (body) => {
return body.variables.language === 'language';
})
.reply(200, {
data: {
layout: {
homePage: {
rootItem: [],
},
},
},
});
nock(endpoint, { reqheaders: { sc_apikey: apiKey } })
.post('/', (body) => {
return body.variables.language === 'en';
})
.reply(200, {
data: {
layout: {
homePage: {
rootItem: [],
},
},
},
});
const result = await getAppRootId(client, 'siteName', 'language');
expect(result).to.be.null;
});
it('null if no app root found', async () => {
nock(endpoint, { reqheaders: { sc_apikey: apiKey } })
.post('/', queryNameFilter)
.reply(200, {
data: {
layout: {
homePage: {
rootItem: [],
},
},
},
});
const result = await getAppRootId(client, 'siteName', 'en');
expect(result).to.be.null;
});
});
describe('parameters', () => {
it('should enforce valid "siteName" value', async () => {
nock(endpoint, { reqheaders: { sc_apikey: apiKey } })
.post('/', queryNameFilter)
.reply(200, appRootQueryResponse);
await getAppRootId(client, '', 'language').catch((error) => {
expect(error).to.be.instanceOf(RangeError);
expect(error.message).to.equal(siteNameError);
});
});
it('should enforce valid "language" value', async () => {
nock(endpoint, { reqheaders: { sc_apikey: apiKey } })
.post('/', queryNameFilter)
.reply(200, appRootQueryResponse);
await getAppRootId(client, 'siteName', '').catch((error) => {
expect(error).to.be.instanceOf(RangeError);
expect(error.message).to.equal(languageError);
});
});
it('should use custom jssAppTemplateId, if provided', async () => {
const customTemplateId = '{custom-id}';
nock(endpoint, { reqheaders: { sc_apikey: apiKey } })
.post('/', (body) => body.variables.jssAppTemplateId === customTemplateId)
.reply(200, appRootQueryResponse);
await getAppRootId(client, 'siteName', 'language', customTemplateId);
expect(nock.isDone()).to.be.true;
});
it('should use default value for jssAppTemplateId, if not specified', async () => {
nock(endpoint, { reqheaders: { sc_apikey: apiKey } })
.post('/', (body) => body.variables.jssAppTemplateId === SitecoreTemplateId.JssApp)
.reply(200, appRootQueryResponse);
await getAppRootId(client, 'siteName', 'language');
expect(nock.isDone()).to.be.true;
});
});
});
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');
});
});
});
});