@reactionary/examples-node 0.3.11 → 0.3.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@reactionary/examples-node",
3
- "version": "0.3.11",
3
+ "version": "0.3.12",
4
4
  "main": "index.js",
5
5
  "types": "src/index.d.ts",
6
6
  "dependencies": {
7
- "@reactionary/core": "0.3.11",
8
- "@reactionary/provider-commercetools": "0.3.11",
9
- "@reactionary/provider-algolia": "0.3.11",
10
- "@reactionary/provider-medusa": "0.3.11",
11
- "@reactionary/provider-meilisearch": "0.3.11"
7
+ "@reactionary/core": "0.3.12",
8
+ "@reactionary/provider-commercetools": "0.3.12",
9
+ "@reactionary/provider-algolia": "0.3.12",
10
+ "@reactionary/provider-medusa": "0.3.12",
11
+ "@reactionary/provider-meilisearch": "0.3.12"
12
12
  },
13
13
  "type": "module"
14
14
  }
@@ -0,0 +1,285 @@
1
+ import 'dotenv/config';
2
+ import { assert, beforeEach, describe, expect, it, vi } from 'vitest';
3
+ import { createClient, PrimaryProvider } from '../utils.js';
4
+ import type { ProductSearchQueryCreateNavigationFilter } from '@reactionary/core';
5
+
6
+ const testData = {
7
+ product: {
8
+ id: 'product_878198',
9
+ },
10
+ };
11
+
12
+ describe.each([PrimaryProvider.COMMERCETOOLS])(
13
+ 'Product Reviews',
14
+ (provider) => {
15
+ let client: ReturnType<typeof createClient>;
16
+
17
+ beforeEach(() => {
18
+ client = createClient(provider);
19
+ });
20
+
21
+ it('can get a rating summary for a product that has it', async () => {
22
+ const result = await client.productReviews.getRatingSummary({
23
+ product: { key: testData.product.id }
24
+ });
25
+
26
+ if (!result.success) {
27
+ assert.fail(JSON.stringify(result.error));
28
+ }
29
+
30
+ expect(result.value.averageRating).toBeGreaterThan(0);
31
+ expect(result.value.totalRatings).toBeGreaterThan(0);
32
+ });
33
+
34
+ it('should return an empty result for an unknown product', async () => {
35
+ const result = await client.productReviews.getRatingSummary({
36
+ product: { key: 'unknown-product-id' }
37
+ });
38
+
39
+ if (result.success) {
40
+ assert.fail('Expected it to return NotFoundError');
41
+ }
42
+
43
+ expect(result.error.type).toBe('NotFound');
44
+ });
45
+
46
+ it('can return a list of reviews for a product', async () => {
47
+ const result = await client.productReviews.listReviews({
48
+ product: { key: testData.product.id },
49
+ paginationOptions: {
50
+ pageNumber: 1,
51
+ pageSize: 2,
52
+ },
53
+ });
54
+
55
+ if (!result.success) {
56
+ assert.fail(JSON.stringify(result.error));
57
+ }
58
+
59
+ expect(result.value.length).toBeLessThanOrEqual(2);
60
+ if (result.value.length > 0) {
61
+ expect(result.value[0].identifier.key).toBeDefined();
62
+ expect(result.value[0].authorName).toBeDefined();
63
+ expect(result.value[0].rating).toBeGreaterThan(0);
64
+ expect(result.value[0].content).toBeDefined();
65
+ }
66
+ });
67
+
68
+ it('should return an empty list of reviews for an unknown product', async () => {
69
+ const result = await client.productReviews.listReviews({
70
+ product: { key: 'unknown-product-id' },
71
+ paginationOptions: {
72
+ pageNumber: 1,
73
+ pageSize: 2,
74
+ },
75
+ });
76
+
77
+ if (result.success) {
78
+ assert.fail('Expected it to return NotFoundError');
79
+ }
80
+ expect(result.error.type).toBe('NotFound');
81
+ });
82
+
83
+ it('can paginate the list of reviews for a product', async () => {
84
+ const firstPageResult = await client.productReviews.listReviews({
85
+ product: { key: testData.product.id },
86
+ paginationOptions: {
87
+ pageNumber: 1,
88
+ pageSize: 1,
89
+ },
90
+ });
91
+
92
+ if (!firstPageResult.success) {
93
+ assert.fail(JSON.stringify(firstPageResult.error));
94
+ }
95
+
96
+ expect(firstPageResult.value.length).toBe(1);
97
+ const firstReview = firstPageResult.value[0];
98
+ expect(firstReview.identifier.key).toBeDefined();
99
+
100
+ const secondPageResult = await client.productReviews.listReviews({
101
+ product: { key: testData.product.id },
102
+ paginationOptions: {
103
+ pageNumber: 2,
104
+ pageSize: 1,
105
+ },
106
+ });
107
+
108
+ if (!secondPageResult.success) {
109
+ assert.fail(JSON.stringify(secondPageResult.error));
110
+ }
111
+ });
112
+
113
+ describe('Submitting Reviews', () => {
114
+ it('cannot submit review if not authenticated', async () => {
115
+ const result = await client.productReviews.submitReview({
116
+ product: { key: testData.product.id },
117
+ rating: 4,
118
+ title: 'Great product!',
119
+ content: 'I really enjoyed using this product. Highly recommend it.',
120
+ authorName: 'John Doe',
121
+ });
122
+
123
+ if (result.success) {
124
+ assert.fail('Expected it to return an error for unauthenticated user');
125
+ } else {
126
+ expect(result.error.type).toBe('InvalidInput');
127
+ }
128
+ });
129
+
130
+ it('can submit a review for a product', async () => {
131
+
132
+ const newIdentity = await client.identity.register({
133
+ username: `testuser_${Date.now()}@example.com`,
134
+ password: 'password123',
135
+ });
136
+
137
+ const result = await client.productReviews.submitReview({
138
+ product: { key: testData.product.id },
139
+ rating: 4,
140
+ title: 'Great product!',
141
+ content: 'I really enjoyed using this product. Highly recommend it.',
142
+ authorName: 'John Doe',
143
+ });
144
+
145
+ if (!result.success) {
146
+ assert.fail(JSON.stringify(result.error));
147
+ }
148
+ });
149
+
150
+ it('can submit a rating without review', async () => {
151
+ const result = await client.productReviews.submitReview({
152
+ product: { key: testData.product.id },
153
+ rating: 5,
154
+ title: '',
155
+ content: '',
156
+ authorName: 'Jane Doe',
157
+ });
158
+ });
159
+
160
+ it('should return an error when submitting a review with invalid rating', async () => {
161
+ const result = await client.productReviews.submitReview({
162
+ product: { key: testData.product.id },
163
+ rating: 6, // Invalid rating, should be between 1 and 5
164
+ title: 'Invalid rating',
165
+ content: 'This review has an invalid rating.',
166
+ authorName: 'Invalid User',
167
+ });
168
+
169
+ if (result.success) {
170
+ assert.fail('Expected it to return an error for invalid rating');
171
+ } else {
172
+ expect(result.error.type).toBe('InvalidInput');
173
+ }
174
+ });
175
+ });
176
+ });
177
+
178
+
179
+ describe.each([PrimaryProvider.MEILISEARCH])(
180
+ 'Product Recommendations - Similar - %s',
181
+ (provider) => {
182
+ let client: ReturnType<typeof createClient>;
183
+
184
+ beforeEach(() => {
185
+ client = createClient(provider);
186
+ });
187
+
188
+ it('should be able to return a list of products for recommendation - Similar ', async () => {
189
+ const result = await client.productRecommendations.getRecommendations({
190
+ algorithm: 'similar',
191
+ sourceProduct: {
192
+ key: testData.product.id,
193
+ },
194
+ numberOfRecommendations: 10,
195
+ });
196
+
197
+ if (!result.success) {
198
+ assert.fail(JSON.stringify(result.error));
199
+ }
200
+
201
+ expect(result.value.length).toBeGreaterThan(0);
202
+ expect(result.value[0].recommendationReturnType).toBe('productSearchResultItem');
203
+ if (result.value[0].recommendationReturnType === 'productSearchResultItem') {
204
+ expect(result.value[0].product.identifier.key).toBeDefined();
205
+ expect(result.value[0].product.name).toBeDefined();
206
+ expect(result.value[0].product.slug).toBeDefined();
207
+ expect(result.value[0].product.variants).toBeDefined();
208
+ expect(result.value[0].product.variants.length).toBeGreaterThan(0);
209
+ expect(result.value[0].product.variants[0].variant.sku).toBeDefined();
210
+ expect(result.value[0].product.variants[0].image.sourceUrl).toBeDefined();
211
+ }
212
+ expect(result.value[0].recommendationIdentifier.key).toBeDefined();
213
+ });
214
+
215
+ it('should return an empty result for an unknown sku', async () => {
216
+ const result = await client.productRecommendations.getRecommendations({
217
+ algorithm: 'similar',
218
+ sourceProduct: {
219
+ key: 'unknown-product-id',
220
+ },
221
+ numberOfRecommendations: 10,
222
+ });
223
+
224
+ if (!result.success) {
225
+ assert.fail(JSON.stringify(result.error));
226
+ }
227
+
228
+ expect(result.value.length).toBe(0);
229
+ });
230
+ });
231
+
232
+
233
+
234
+ describe.each([PrimaryProvider.ALGOLIA])(
235
+ 'Product Recommendations - Related - %s',
236
+ (provider) => {
237
+ let client: ReturnType<typeof createClient>;
238
+
239
+ beforeEach(() => {
240
+ client = createClient(provider);
241
+ });
242
+
243
+ it('should be able to return a list of products for recommendation - Related ', async () => {
244
+ const result = await client.productRecommendations.getRecommendations({
245
+ algorithm: 'related',
246
+ sourceProduct: {
247
+ key: testData.product.id,
248
+ },
249
+ numberOfRecommendations: 10,
250
+ });
251
+
252
+ if (!result.success) {
253
+ assert.fail(JSON.stringify(result.error));
254
+ }
255
+
256
+ expect(result.value.length).toBeGreaterThan(0);
257
+ expect(result.value[0].recommendationReturnType).toBe('productSearchResultItem');
258
+ if (result.value[0].recommendationReturnType === 'productSearchResultItem') {
259
+ expect(result.value[0].product.identifier.key).toBeDefined();
260
+ expect(result.value[0].product.name).toBeDefined();
261
+ expect(result.value[0].product.slug).toBeDefined();
262
+ expect(result.value[0].product.variants).toBeDefined();
263
+ expect(result.value[0].product.variants.length).toBeGreaterThan(0);
264
+ expect(result.value[0].product.variants[0].variant.sku).toBeDefined();
265
+ expect(result.value[0].product.variants[0].image.sourceUrl).toBeDefined();
266
+ }
267
+
268
+ });
269
+
270
+ it('should return an empty result for an unknown sku', async () => {
271
+ const result = await client.productRecommendations.getRecommendations({
272
+ algorithm: 'related',
273
+ sourceProduct: {
274
+ key: 'unknown-product-id',
275
+ },
276
+ numberOfRecommendations: 10,
277
+ });
278
+
279
+ if (!result.success) {
280
+ assert.fail(JSON.stringify(result.error));
281
+ }
282
+
283
+ expect(result.value.length).toBe(0);
284
+ });
285
+ });
package/src/utils.ts CHANGED
@@ -113,6 +113,7 @@ export function createClient(provider: PrimaryProvider) {
113
113
  order: true,
114
114
  price: true,
115
115
  productSearch: true,
116
+ productReviews: true,
116
117
  orderSearch: true,
117
118
  store: true,
118
119
  profile: true,