@reactionary/examples-node 0.3.12 → 0.3.14

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,15 @@
1
1
  {
2
2
  "name": "@reactionary/examples-node",
3
- "version": "0.3.12",
3
+ "version": "0.3.14",
4
4
  "main": "index.js",
5
5
  "types": "src/index.d.ts",
6
6
  "dependencies": {
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"
7
+ "@reactionary/core": "0.3.14",
8
+ "@reactionary/provider-commercetools": "0.3.14",
9
+ "@reactionary/provider-algolia": "0.3.14",
10
+ "@reactionary/provider-medusa": "0.3.14",
11
+ "@reactionary/provider-meilisearch": "0.3.14",
12
+ "@reactionary/provider-fake": "0.3.14"
12
13
  },
13
14
  "type": "module"
14
15
  }
@@ -9,277 +9,169 @@ const testData = {
9
9
  },
10
10
  };
11
11
 
12
- describe.each([PrimaryProvider.COMMERCETOOLS])(
13
- 'Product Reviews',
14
- (provider) => {
15
- let client: ReturnType<typeof createClient>;
12
+ describe.each([PrimaryProvider.FAKE, PrimaryProvider.COMMERCETOOLS])('Product Reviews', (provider) => {
13
+ let client: ReturnType<typeof createClient>;
16
14
 
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
- }
15
+ beforeEach(() => {
16
+ client = createClient(provider);
17
+ });
29
18
 
30
- expect(result.value.averageRating).toBeGreaterThan(0);
31
- expect(result.value.totalRatings).toBeGreaterThan(0);
19
+ it('can get a rating summary for a product that has it', async () => {
20
+ const result = await client.productReviews.getRatingSummary({
21
+ product: { key: testData.product.id },
32
22
  });
33
23
 
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
- });
24
+ if (!result.success) {
25
+ assert.fail(JSON.stringify(result.error));
26
+ }
38
27
 
39
- if (result.success) {
40
- assert.fail('Expected it to return NotFoundError');
41
- }
28
+ expect(result.value.averageRating).toBeGreaterThan(0);
29
+ expect(result.value.totalRatings).toBeGreaterThan(0);
30
+ });
42
31
 
43
- expect(result.error.type).toBe('NotFound');
32
+ it('should return an empty result for an unknown product', async () => {
33
+ const result = await client.productReviews.getRatingSummary({
34
+ product: { key: 'unknown-product-id' },
44
35
  });
45
36
 
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
- });
37
+ if (!result.success) {
38
+ assert.fail(JSON.stringify(result.error));
39
+ }
54
40
 
55
- if (!result.success) {
56
- assert.fail(JSON.stringify(result.error));
57
- }
41
+ expect(result.value.averageRating).toBe(0);
42
+ expect(result.value.totalRatings).toBeUndefined();
43
+ });
58
44
 
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
- }
45
+ it('can return a list of reviews for a product', async () => {
46
+ const result = await client.productReviews.findReviews({
47
+ product: { key: testData.product.id },
48
+ paginationOptions: {
49
+ pageNumber: 1,
50
+ pageSize: 2,
51
+ },
66
52
  });
67
53
 
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
- });
54
+ if (!result.success) {
55
+ assert.fail(JSON.stringify(result.error));
56
+ }
57
+
58
+ expect(result.value.items.length).toBeLessThanOrEqual(2);
59
+ if (result.value.items.length > 0) {
60
+ expect(result.value.items[0].identifier.key).toBeDefined();
61
+ expect(result.value.items[0].authorName).toBeDefined();
62
+ expect(result.value.items[0].rating).toBeGreaterThan(0);
63
+ expect(result.value.items[0].content).toBeDefined();
64
+ }
65
+ });
76
66
 
77
- if (result.success) {
78
- assert.fail('Expected it to return NotFoundError');
79
- }
80
- expect(result.error.type).toBe('NotFound');
67
+ it('should return an empty list of reviews for an unknown product', async () => {
68
+ const result = await client.productReviews.findReviews({
69
+ product: { key: 'unknown-product-id' },
70
+ paginationOptions: {
71
+ pageNumber: 1,
72
+ pageSize: 2,
73
+ },
81
74
  });
82
75
 
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
- });
76
+ if (!result.success) {
77
+ assert.fail(JSON.stringify(result.error));
78
+ }
79
+ expect(result.value.items.length).toBe(0);
80
+ });
107
81
 
108
- if (!secondPageResult.success) {
109
- assert.fail(JSON.stringify(secondPageResult.error));
110
- }
82
+ it('can paginate the list of reviews for a product', async () => {
83
+ const firstPageResult = await client.productReviews.findReviews({
84
+ product: { key: testData.product.id },
85
+ paginationOptions: {
86
+ pageNumber: 1,
87
+ pageSize: 1,
88
+ },
111
89
  });
112
90
 
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
- });
91
+ if (!firstPageResult.success) {
92
+ assert.fail(JSON.stringify(firstPageResult.error));
93
+ }
144
94
 
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
- });
95
+ expect(firstPageResult.value.items.length).toBe(1);
96
+ const firstReview = firstPageResult.value.items[0];
97
+ expect(firstReview.identifier.key).toBeDefined();
159
98
 
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
- });
99
+ const secondPageResult = await client.productReviews.findReviews({
100
+ product: { key: testData.product.id },
101
+ paginationOptions: {
102
+ pageNumber: 2,
103
+ pageSize: 1,
104
+ },
175
105
  });
176
- });
177
-
178
-
179
- describe.each([PrimaryProvider.MEILISEARCH])(
180
- 'Product Recommendations - Similar - %s',
181
- (provider) => {
182
- let client: ReturnType<typeof createClient>;
183
106
 
184
- beforeEach(() => {
185
- client = createClient(provider);
186
- });
107
+ if (!secondPageResult.success) {
108
+ assert.fail(JSON.stringify(secondPageResult.error));
109
+ }
110
+ });
187
111
 
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,
112
+ describe('Submitting Reviews', () => {
113
+ it('cannot submit review if not authenticated', async () => {
114
+ const result = await client.productReviews.submitReview({
115
+ product: { key: testData.product.id },
116
+ rating: 4,
117
+ title: 'Great product!',
118
+ content: 'I really enjoyed using this product. Highly recommend it.',
119
+ authorName: 'John Doe',
195
120
  });
196
121
 
197
- if (!result.success) {
198
- assert.fail(JSON.stringify(result.error));
122
+ if (result.success) {
123
+ assert.fail('Expected it to return an error for unauthenticated user');
124
+ } else {
125
+ expect(result.error.type).toBe('InvalidInput');
199
126
  }
127
+ });
200
128
 
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();
129
+ it('can submit a review for a product', async () => {
130
+ if (provider === PrimaryProvider.FAKE) {
131
+ return;
211
132
  }
212
- expect(result.value[0].recommendationIdentifier.key).toBeDefined();
213
- });
133
+ const newIdentity = await client.identity.register({
134
+ username: `testuser_${Date.now()}@example.com`,
135
+ password: 'password123',
136
+ });
214
137
 
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,
138
+ const result = await client.productReviews.submitReview({
139
+ product: { key: testData.product.id },
140
+ rating: 4,
141
+ title: 'Great product!',
142
+ content: 'I really enjoyed using this product. Highly recommend it.',
143
+ authorName: 'John Doe',
222
144
  });
223
145
 
224
146
  if (!result.success) {
225
147
  assert.fail(JSON.stringify(result.error));
226
148
  }
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
149
  });
242
150
 
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,
151
+ it('can submit a rating without review', async () => {
152
+ const result = await client.productReviews.submitReview({
153
+ product: { key: testData.product.id },
154
+ rating: 5,
155
+ title: '',
156
+ content: '',
157
+ authorName: 'Jane Doe',
250
158
  });
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
159
  });
269
160
 
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,
161
+ it('should return an error when submitting a review with invalid rating', async () => {
162
+ const result = await client.productReviews.submitReview({
163
+ product: { key: testData.product.id },
164
+ rating: 6, // Invalid rating, should be between 1 and 5
165
+ title: 'Invalid rating',
166
+ content: 'This review has an invalid rating.',
167
+ authorName: 'Invalid User',
277
168
  });
278
169
 
279
- if (!result.success) {
280
- assert.fail(JSON.stringify(result.error));
170
+ if (result.success) {
171
+ assert.fail('Expected it to return an error for invalid rating');
172
+ } else {
173
+ expect(result.error.type).toBe('InvalidInput');
281
174
  }
282
-
283
- expect(result.value.length).toBe(0);
284
175
  });
285
176
  });
177
+ });
package/src/utils.ts CHANGED
@@ -8,6 +8,8 @@ import { withCommercetoolsCapabilities } from '@reactionary/provider-commercetoo
8
8
  import { withAlgoliaCapabilities } from '@reactionary/provider-algolia';
9
9
  import { withMedusaCapabilities } from '@reactionary/provider-medusa';
10
10
  import { withMeilisearchCapabilities } from '@reactionary/provider-meilisearch';
11
+ import { withFakeCapabilities } from '@reactionary/provider-fake';
12
+ import type { FakeConfiguration } from '@reactionary/provider-fake';
11
13
 
12
14
  export function getAlgoliaTestConfiguration() {
13
15
  return {
@@ -39,6 +41,20 @@ export function getMedusaTestConfiguration() {
39
41
  }
40
42
 
41
43
 
44
+ export function getFakeConfiguration(): FakeConfiguration {
45
+ return {
46
+ jitter: {
47
+ mean: 0,
48
+ deviation: 0,
49
+ },
50
+ seeds: {
51
+ product: 1,
52
+ search: 1,
53
+ category: 1,
54
+ },
55
+ } satisfies FakeConfiguration;
56
+ }
57
+
42
58
  export function getCommercetoolsTestConfiguration() {
43
59
  return {
44
60
  apiUrl: process.env['CTP_API_URL'] || '',
@@ -71,7 +87,8 @@ export enum PrimaryProvider {
71
87
  ALGOLIA = 'Algolia',
72
88
  COMMERCETOOLS = 'Commercetools',
73
89
  MEDUSA = 'Medusa',
74
- MEILISEARCH = 'Meilisearch'
90
+ MEILISEARCH = 'Meilisearch',
91
+ FAKE = 'Fake',
75
92
  }
76
93
 
77
94
  export function createClient(provider: PrimaryProvider) {
@@ -99,7 +116,13 @@ export function createClient(provider: PrimaryProvider) {
99
116
  );
100
117
  }
101
118
 
102
-
119
+ if (provider === PrimaryProvider.FAKE) {
120
+ builder = builder.withCapability(
121
+ withFakeCapabilities( getFakeConfiguration() , {
122
+ productReviews: true
123
+ }
124
+ ))
125
+ }
103
126
 
104
127
  if (provider === PrimaryProvider.COMMERCETOOLS) {
105
128
  builder = builder.withCapability(