@reactionary/examples-node 0.3.11 → 0.3.13

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.11",
3
+ "version": "0.3.13",
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.13",
8
+ "@reactionary/provider-commercetools": "0.3.13",
9
+ "@reactionary/provider-algolia": "0.3.13",
10
+ "@reactionary/provider-medusa": "0.3.13",
11
+ "@reactionary/provider-meilisearch": "0.3.13",
12
+ "@reactionary/provider-fake": "0.3.13"
12
13
  },
13
14
  "type": "module"
14
15
  }
@@ -0,0 +1,177 @@
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.FAKE, PrimaryProvider.COMMERCETOOLS])('Product Reviews', (provider) => {
13
+ let client: ReturnType<typeof createClient>;
14
+
15
+ beforeEach(() => {
16
+ client = createClient(provider);
17
+ });
18
+
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 },
22
+ });
23
+
24
+ if (!result.success) {
25
+ assert.fail(JSON.stringify(result.error));
26
+ }
27
+
28
+ expect(result.value.averageRating).toBeGreaterThan(0);
29
+ expect(result.value.totalRatings).toBeGreaterThan(0);
30
+ });
31
+
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' },
35
+ });
36
+
37
+ if (!result.success) {
38
+ assert.fail(JSON.stringify(result.error));
39
+ }
40
+
41
+ expect(result.value.averageRating).toBe(0);
42
+ expect(result.value.totalRatings).toBeUndefined();
43
+ });
44
+
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
+ },
52
+ });
53
+
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
+ });
66
+
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
+ },
74
+ });
75
+
76
+ if (!result.success) {
77
+ assert.fail(JSON.stringify(result.error));
78
+ }
79
+ expect(result.value.items.length).toBe(0);
80
+ });
81
+
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
+ },
89
+ });
90
+
91
+ if (!firstPageResult.success) {
92
+ assert.fail(JSON.stringify(firstPageResult.error));
93
+ }
94
+
95
+ expect(firstPageResult.value.items.length).toBe(1);
96
+ const firstReview = firstPageResult.value.items[0];
97
+ expect(firstReview.identifier.key).toBeDefined();
98
+
99
+ const secondPageResult = await client.productReviews.findReviews({
100
+ product: { key: testData.product.id },
101
+ paginationOptions: {
102
+ pageNumber: 2,
103
+ pageSize: 1,
104
+ },
105
+ });
106
+
107
+ if (!secondPageResult.success) {
108
+ assert.fail(JSON.stringify(secondPageResult.error));
109
+ }
110
+ });
111
+
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',
120
+ });
121
+
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');
126
+ }
127
+ });
128
+
129
+ it('can submit a review for a product', async () => {
130
+ if (provider === PrimaryProvider.FAKE) {
131
+ return;
132
+ }
133
+ const newIdentity = await client.identity.register({
134
+ username: `testuser_${Date.now()}@example.com`,
135
+ password: 'password123',
136
+ });
137
+
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',
144
+ });
145
+
146
+ if (!result.success) {
147
+ assert.fail(JSON.stringify(result.error));
148
+ }
149
+ });
150
+
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',
158
+ });
159
+ });
160
+
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',
168
+ });
169
+
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');
174
+ }
175
+ });
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(
@@ -113,6 +136,7 @@ export function createClient(provider: PrimaryProvider) {
113
136
  order: true,
114
137
  price: true,
115
138
  productSearch: true,
139
+ productReviews: true,
116
140
  orderSearch: true,
117
141
  store: true,
118
142
  profile: true,