@graphcommerce/next-config 8.1.0-canary.2 → 8.1.0-canary.5
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/CHANGELOG.md +133 -1
- package/Config.graphqls +4 -2
- package/__tests__/config/utils/__snapshots__/mergeEnvIntoConfig.ts.snap +19 -2
- package/__tests__/config/utils/replaceConfigInString.ts +4 -0
- package/__tests__/interceptors/findPlugins.ts +473 -113
- package/__tests__/interceptors/generateInterceptors.ts +610 -322
- package/__tests__/interceptors/parseStructure.ts +158 -0
- package/__tests__/interceptors/writeInterceptors.ts +23 -14
- package/__tests__/utils/resolveDependenciesSync.ts +28 -25
- package/dist/config/commands/generateConfig.js +5 -2
- package/dist/config/demoConfig.js +19 -4
- package/dist/generated/config.js +8 -1
- package/dist/interceptors/InterceptorPlugin.js +70 -25
- package/dist/interceptors/RenameVisitor.js +19 -0
- package/dist/interceptors/Visitor.js +1418 -0
- package/dist/interceptors/extractExports.js +201 -0
- package/dist/interceptors/findOriginalSource.js +87 -0
- package/dist/interceptors/findPlugins.js +21 -53
- package/dist/interceptors/generateInterceptor.js +200 -0
- package/dist/interceptors/generateInterceptors.js +38 -179
- package/dist/interceptors/parseStructure.js +71 -0
- package/dist/interceptors/swc.js +16 -0
- package/dist/interceptors/writeInterceptors.js +19 -10
- package/dist/utils/resolveDependency.js +27 -5
- package/dist/withGraphCommerce.js +2 -1
- package/package.json +4 -1
- package/src/config/commands/generateConfig.ts +5 -2
- package/src/config/demoConfig.ts +19 -4
- package/src/config/index.ts +4 -2
- package/src/generated/config.ts +25 -3
- package/src/index.ts +16 -6
- package/src/interceptors/InterceptorPlugin.ts +90 -32
- package/src/interceptors/RenameVisitor.ts +17 -0
- package/src/interceptors/Visitor.ts +1847 -0
- package/src/interceptors/extractExports.ts +230 -0
- package/src/interceptors/findOriginalSource.ts +105 -0
- package/src/interceptors/findPlugins.ts +36 -87
- package/src/interceptors/generateInterceptor.ts +271 -0
- package/src/interceptors/generateInterceptors.ts +67 -237
- package/src/interceptors/parseStructure.ts +82 -0
- package/src/interceptors/swc.ts +13 -0
- package/src/interceptors/writeInterceptors.ts +26 -10
- package/src/utils/resolveDependency.ts +51 -12
- package/src/withGraphCommerce.ts +2 -1
|
@@ -1,412 +1,700 @@
|
|
|
1
|
+
import { GraphCommerceConfig } from '../../src/generated/config'
|
|
2
|
+
import { findOriginalSource } from '../../src/interceptors/findOriginalSource'
|
|
3
|
+
import { SOURCE_START, SOURCE_END } from '../../src/interceptors/generateInterceptor'
|
|
1
4
|
import { generateInterceptors } from '../../src/interceptors/generateInterceptors'
|
|
5
|
+
import { parseStructure } from '../../src/interceptors/parseStructure'
|
|
6
|
+
import { parseSync } from '../../src/interceptors/swc'
|
|
2
7
|
import { resolveDependency } from '../../src/utils/resolveDependency'
|
|
3
8
|
|
|
4
9
|
const projectRoot = `${process.cwd()}/examples/magento-graphcms`
|
|
5
10
|
|
|
11
|
+
const expectImport = (value: string | undefined): jest.JestMatchers<string> =>
|
|
12
|
+
expect(value?.slice(value.indexOf(`import`) - 1, value.indexOf(SOURCE_START) - 1).trim())
|
|
13
|
+
|
|
14
|
+
const expectInterceptor = (value: string | undefined): jest.JestMatchers<string> => {
|
|
15
|
+
const val = value?.slice(value.indexOf(SOURCE_END) + SOURCE_END.length).trim()
|
|
16
|
+
return expect(val?.trim())
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const expectOriginal = (value: string | undefined): jest.JestMatchers<string> =>
|
|
20
|
+
expect(
|
|
21
|
+
value
|
|
22
|
+
?.slice(value.indexOf(SOURCE_START) + SOURCE_START.length, value.indexOf(SOURCE_END))
|
|
23
|
+
.trim(),
|
|
24
|
+
)
|
|
25
|
+
|
|
6
26
|
it('it replaces paths and creates a relative path', () => {
|
|
7
27
|
const resolver = resolveDependency(projectRoot)
|
|
8
28
|
const resolved = resolver('@graphcommerce/magento-cart-payment-method')
|
|
9
|
-
expect(resolved
|
|
10
|
-
expect(resolved
|
|
11
|
-
expect(resolved
|
|
29
|
+
expect(resolved?.fromRoot).toMatchInlineSnapshot(`"packages/magento-cart-payment-method/index"`)
|
|
30
|
+
expect(resolved?.fromModule).toBe('.')
|
|
31
|
+
expect(resolved?.root).toBe('packages/magento-cart-payment-method')
|
|
12
32
|
|
|
13
33
|
const resolved2 = resolver(
|
|
14
34
|
'@graphcommerce/magento-cart-payment-method/PaymentMethodContext/PaymentMethodContext',
|
|
15
35
|
)
|
|
16
|
-
expect(resolved2
|
|
36
|
+
expect(resolved2?.fromRoot).toMatchInlineSnapshot(
|
|
17
37
|
`"packages/magento-cart-payment-method/PaymentMethodContext/PaymentMethodContext"`,
|
|
18
38
|
)
|
|
19
|
-
expect(resolved2
|
|
20
|
-
expect(resolved2
|
|
39
|
+
expect(resolved2?.fromModule).toBe('./PaymentMethodContext')
|
|
40
|
+
expect(resolved2?.root).toBe('packages/magento-cart-payment-method')
|
|
21
41
|
})
|
|
22
42
|
|
|
23
|
-
it('it generates an interceptor', () => {
|
|
43
|
+
it('it generates an interceptor', async () => {
|
|
24
44
|
const resolve = resolveDependency(projectRoot)
|
|
25
|
-
const interceptors = generateInterceptors(
|
|
45
|
+
const interceptors = await generateInterceptors(
|
|
26
46
|
[
|
|
27
47
|
{
|
|
28
|
-
|
|
48
|
+
type: 'component',
|
|
49
|
+
targetExport: 'PaymentMethodContextProvider',
|
|
50
|
+
sourceExport: 'Plugin',
|
|
29
51
|
enabled: true,
|
|
30
|
-
|
|
31
|
-
|
|
52
|
+
targetModule: '@graphcommerce/magento-cart-payment-method',
|
|
53
|
+
sourceModule: '@graphcommerce/mollie-magento-payment/plugins/AddMollieMethods',
|
|
32
54
|
},
|
|
33
55
|
{
|
|
34
|
-
|
|
56
|
+
type: 'component',
|
|
57
|
+
targetExport: 'PaymentMethodContextProvider',
|
|
58
|
+
sourceExport: 'Plugin',
|
|
35
59
|
enabled: true,
|
|
36
|
-
|
|
37
|
-
|
|
60
|
+
targetModule: '@graphcommerce/magento-cart-payment-method/index',
|
|
61
|
+
sourceModule: '@graphcommerce/magento-payment-braintree/plugins/AddBraintreeMethods',
|
|
38
62
|
},
|
|
39
63
|
],
|
|
40
64
|
resolve,
|
|
41
65
|
)
|
|
42
66
|
|
|
43
|
-
expect(Object.keys(interceptors)[0]).toBe(
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
.toMatchInlineSnapshot(`
|
|
47
|
-
"/* This file is automatically generated for @graphcommerce/magento-cart-payment-method/index */
|
|
48
|
-
|
|
49
|
-
export * from './index'
|
|
50
|
-
import { Plugin as AddBraintreeMethods } from '@graphcommerce/magento-payment-braintree/plugins/AddBraintreeMethods'
|
|
51
|
-
import { Plugin as AddMollieMethods } from '@graphcommerce/mollie-magento-payment/plugins/AddMollieMethods'
|
|
52
|
-
import { ComponentProps } from 'react'
|
|
53
|
-
import { PaymentMethodContextProvider as PaymentMethodContextProviderBase } from './index'
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Interceptor for \`<PaymentMethodContextProvider/>\` with these plugins:
|
|
57
|
-
*
|
|
58
|
-
* - \`@graphcommerce/magento-payment-braintree/plugins/AddBraintreeMethods\`
|
|
59
|
-
* - \`@graphcommerce/mollie-magento-payment/plugins/AddMollieMethods\`
|
|
60
|
-
*/
|
|
61
|
-
type PaymentMethodContextProviderProps = ComponentProps<typeof PaymentMethodContextProviderBase>
|
|
67
|
+
expect(Object.keys(interceptors)[0]).toBe(
|
|
68
|
+
'packages/magento-cart-payment-method/PaymentMethodContext/PaymentMethodContext',
|
|
69
|
+
)
|
|
62
70
|
|
|
63
|
-
|
|
64
|
-
|
|
71
|
+
const result =
|
|
72
|
+
interceptors['packages/magento-cart-payment-method/PaymentMethodContext/PaymentMethodContext']
|
|
73
|
+
?.template
|
|
74
|
+
expectInterceptor(result).toMatchInlineSnapshot(`
|
|
75
|
+
"type PluginAddBraintreeMethodsInterceptorProps = DistributedOmit<
|
|
76
|
+
React.ComponentProps<typeof PluginAddBraintreeMethodsSource>,
|
|
77
|
+
'Prev'
|
|
78
|
+
>
|
|
79
|
+
const PluginAddBraintreeMethodsInterceptor = (props: PluginAddBraintreeMethodsInterceptorProps) => {
|
|
80
|
+
return (
|
|
81
|
+
<PluginAddBraintreeMethodsSource
|
|
82
|
+
{...props}
|
|
83
|
+
Prev={PaymentMethodContextProviderOriginal as React.FC}
|
|
84
|
+
/>
|
|
85
|
+
)
|
|
65
86
|
}
|
|
66
|
-
|
|
67
|
-
|
|
87
|
+
|
|
88
|
+
type PluginAddMollieMethodsInterceptorProps = DistributedOmit<
|
|
89
|
+
React.ComponentProps<typeof PluginAddMollieMethodsSource>,
|
|
90
|
+
'Prev'
|
|
91
|
+
>
|
|
92
|
+
const PluginAddMollieMethodsInterceptor = (
|
|
93
|
+
props: PluginAddBraintreeMethodsInterceptorProps & PluginAddMollieMethodsInterceptorProps,
|
|
94
|
+
) => {
|
|
95
|
+
return (
|
|
96
|
+
<PluginAddMollieMethodsSource
|
|
97
|
+
{...props}
|
|
98
|
+
Prev={PluginAddBraintreeMethodsInterceptor as React.FC}
|
|
99
|
+
/>
|
|
100
|
+
)
|
|
68
101
|
}
|
|
69
|
-
export const PaymentMethodContextProvider =
|
|
70
|
-
"
|
|
102
|
+
export const PaymentMethodContextProvider = PluginAddMollieMethodsInterceptor"
|
|
71
103
|
`)
|
|
72
104
|
})
|
|
73
105
|
|
|
74
|
-
it(
|
|
75
|
-
const interceptors = generateInterceptors(
|
|
106
|
+
it("resolves a 'root plugin' to be relative to the interceptor", async () => {
|
|
107
|
+
const interceptors = await generateInterceptors(
|
|
76
108
|
[
|
|
77
109
|
{
|
|
78
|
-
|
|
110
|
+
type: 'component',
|
|
111
|
+
targetExport: 'PaymentMethodContextProvider',
|
|
112
|
+
sourceExport: 'Plugin',
|
|
79
113
|
enabled: true,
|
|
80
|
-
|
|
81
|
-
|
|
114
|
+
targetModule: '@graphcommerce/magento-cart-payment-method',
|
|
115
|
+
sourceModule: './plugins/AddPaymentMethodEnhancer',
|
|
82
116
|
},
|
|
83
117
|
],
|
|
84
118
|
resolveDependency(projectRoot),
|
|
85
119
|
)
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
"
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
*
|
|
98
|
-
* - \`@graphcommerce/magento-graphcms/plugins/AddPaymentMethodEnhancer\`
|
|
99
|
-
*/
|
|
100
|
-
type PaymentMethodContextProviderProps = ComponentProps<typeof PaymentMethodContextProviderBase>
|
|
101
|
-
|
|
102
|
-
function AddPaymentMethodEnhancerInterceptor(props: PaymentMethodContextProviderProps) {
|
|
103
|
-
return <AddPaymentMethodEnhancer {...props} Prev={PaymentMethodContextProviderBase} />
|
|
104
|
-
}
|
|
105
|
-
export const PaymentMethodContextProvider = AddPaymentMethodEnhancerInterceptor
|
|
106
|
-
"
|
|
120
|
+
|
|
121
|
+
expect(Object.keys(interceptors)[0]).toMatchInlineSnapshot(
|
|
122
|
+
`"packages/magento-cart-payment-method/PaymentMethodContext/PaymentMethodContext"`,
|
|
123
|
+
)
|
|
124
|
+
expectImport(
|
|
125
|
+
interceptors['packages/magento-cart-payment-method/PaymentMethodContext/PaymentMethodContext']
|
|
126
|
+
?.template,
|
|
127
|
+
).toMatchInlineSnapshot(`
|
|
128
|
+
"import type { DistributedOmit } from 'type-fest'
|
|
129
|
+
|
|
130
|
+
import { Plugin as PluginAddPaymentMethodEnhancerSource } from '../../../plugins/AddPaymentMethodEnhancer'"
|
|
107
131
|
`)
|
|
108
132
|
})
|
|
109
133
|
|
|
110
|
-
it('it can apply multiple plugins to a single export', () => {
|
|
111
|
-
const plugins = [
|
|
112
|
-
{
|
|
113
|
-
component: 'PaymentMethodContextProvider',
|
|
114
|
-
enabled: true,
|
|
115
|
-
exported:
|
|
116
|
-
'@graphcommerce/magento-cart-payment-method/PaymentMethodContext/PaymentMethodContext',
|
|
117
|
-
plugin: '@graphcommerce/mollie-magento-payment/plugins/AddMollieMethods',
|
|
118
|
-
},
|
|
119
|
-
{
|
|
120
|
-
component: 'OneMoreComponent',
|
|
121
|
-
enabled: true,
|
|
122
|
-
exported:
|
|
123
|
-
'@graphcommerce/magento-cart-payment-method/PaymentMethodContext/PaymentMethodContext',
|
|
124
|
-
plugin: '@graphcommerce/magento-payment-braintree/plugins/AddOneMore',
|
|
125
|
-
},
|
|
126
|
-
]
|
|
127
|
-
|
|
134
|
+
it('it can apply multiple plugins to a single export', async () => {
|
|
128
135
|
const resolve = resolveDependency(projectRoot)
|
|
129
|
-
const interceptors = generateInterceptors(
|
|
130
|
-
|
|
136
|
+
const interceptors = await generateInterceptors(
|
|
137
|
+
[
|
|
138
|
+
{
|
|
139
|
+
type: 'component',
|
|
140
|
+
targetExport: 'PaymentMethodContextProvider',
|
|
141
|
+
sourceExport: 'Plugin',
|
|
142
|
+
enabled: true,
|
|
143
|
+
targetModule: '@graphcommerce/magento-cart-payment-method',
|
|
144
|
+
sourceModule: '@graphcommerce/mollie-magento-payment/plugins/AddMollieMethods',
|
|
145
|
+
},
|
|
146
|
+
{
|
|
147
|
+
type: 'component',
|
|
148
|
+
targetExport: 'PaymentMethodContextProvider',
|
|
149
|
+
sourceExport: 'Plugin',
|
|
150
|
+
enabled: true,
|
|
151
|
+
targetModule: '@graphcommerce/magento-cart-payment-method',
|
|
152
|
+
sourceModule: '@graphcommerce/magento-payment-adyen/plugins/AddAdyenMethods',
|
|
153
|
+
},
|
|
154
|
+
],
|
|
155
|
+
resolve,
|
|
156
|
+
)
|
|
157
|
+
const result =
|
|
131
158
|
interceptors['packages/magento-cart-payment-method/PaymentMethodContext/PaymentMethodContext']
|
|
132
|
-
?.template
|
|
133
|
-
).toMatchInlineSnapshot(`
|
|
134
|
-
"
|
|
135
|
-
|
|
136
|
-
export * from './PaymentMethodContext'
|
|
137
|
-
import { Plugin as AddOneMore } from '@graphcommerce/magento-payment-braintree/plugins/AddOneMore'
|
|
138
|
-
import { Plugin as AddMollieMethods } from '@graphcommerce/mollie-magento-payment/plugins/AddMollieMethods'
|
|
139
|
-
import { ComponentProps } from 'react'
|
|
140
|
-
import {
|
|
141
|
-
PaymentMethodContextProvider as PaymentMethodContextProviderBase,
|
|
142
|
-
OneMoreComponent as OneMoreComponentBase,
|
|
143
|
-
} from './PaymentMethodContext'
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Interceptor for \`<PaymentMethodContextProvider/>\` with these plugins:
|
|
147
|
-
*
|
|
148
|
-
* - \`@graphcommerce/mollie-magento-payment/plugins/AddMollieMethods\`
|
|
149
|
-
*/
|
|
150
|
-
type PaymentMethodContextProviderProps = ComponentProps<typeof PaymentMethodContextProviderBase>
|
|
151
|
-
|
|
152
|
-
function AddMollieMethodsInterceptor(props: PaymentMethodContextProviderProps) {
|
|
153
|
-
return <AddMollieMethods {...props} Prev={PaymentMethodContextProviderBase} />
|
|
154
|
-
}
|
|
155
|
-
export const PaymentMethodContextProvider = AddMollieMethodsInterceptor
|
|
159
|
+
?.template
|
|
160
|
+
expectImport(result).toMatchInlineSnapshot(`
|
|
161
|
+
"import type { DistributedOmit } from 'type-fest'
|
|
156
162
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
* - \`@graphcommerce/magento-payment-braintree/plugins/AddOneMore\`
|
|
161
|
-
*/
|
|
162
|
-
type OneMoreComponentProps = ComponentProps<typeof OneMoreComponentBase>
|
|
163
|
+
import { Plugin as PluginAddAdyenMethodsSource } from '@graphcommerce/magento-payment-adyen/plugins/AddAdyenMethods'
|
|
164
|
+
import { Plugin as PluginAddMollieMethodsSource } from '@graphcommerce/mollie-magento-payment/plugins/AddMollieMethods'"
|
|
165
|
+
`)
|
|
163
166
|
|
|
164
|
-
|
|
165
|
-
|
|
167
|
+
expectOriginal(result).toContain('PaymentMethodContextProviderOriginal')
|
|
168
|
+
|
|
169
|
+
expectInterceptor(result).toMatchInlineSnapshot(`
|
|
170
|
+
"type PluginAddAdyenMethodsInterceptorProps = DistributedOmit<
|
|
171
|
+
React.ComponentProps<typeof PluginAddAdyenMethodsSource>,
|
|
172
|
+
'Prev'
|
|
173
|
+
>
|
|
174
|
+
const PluginAddAdyenMethodsInterceptor = (props: PluginAddAdyenMethodsInterceptorProps) => {
|
|
175
|
+
return (
|
|
176
|
+
<PluginAddAdyenMethodsSource
|
|
177
|
+
{...props}
|
|
178
|
+
Prev={PaymentMethodContextProviderOriginal as React.FC}
|
|
179
|
+
/>
|
|
180
|
+
)
|
|
166
181
|
}
|
|
167
|
-
|
|
168
|
-
|
|
182
|
+
|
|
183
|
+
type PluginAddMollieMethodsInterceptorProps = DistributedOmit<
|
|
184
|
+
React.ComponentProps<typeof PluginAddMollieMethodsSource>,
|
|
185
|
+
'Prev'
|
|
186
|
+
>
|
|
187
|
+
const PluginAddMollieMethodsInterceptor = (
|
|
188
|
+
props: PluginAddAdyenMethodsInterceptorProps & PluginAddMollieMethodsInterceptorProps,
|
|
189
|
+
) => {
|
|
190
|
+
return (
|
|
191
|
+
<PluginAddMollieMethodsSource {...props} Prev={PluginAddAdyenMethodsInterceptor as React.FC} />
|
|
192
|
+
)
|
|
193
|
+
}
|
|
194
|
+
export const PaymentMethodContextProvider = PluginAddMollieMethodsInterceptor"
|
|
169
195
|
`)
|
|
170
196
|
})
|
|
171
197
|
|
|
172
|
-
it('it handles on duplicates gracefully', () => {
|
|
173
|
-
const plugins = [
|
|
174
|
-
{
|
|
175
|
-
component: 'PaymentMethodContextProvider',
|
|
176
|
-
enabled: true,
|
|
177
|
-
exported:
|
|
178
|
-
'@graphcommerce/magento-cart-payment-method/PaymentMethodContext/PaymentMethodContext',
|
|
179
|
-
plugin: '@graphcommerce/magento-payment-braintree/plugins/AddBraintreeMethods',
|
|
180
|
-
},
|
|
181
|
-
{
|
|
182
|
-
component: 'PaymentMethodContextProvider',
|
|
183
|
-
enabled: true,
|
|
184
|
-
exported:
|
|
185
|
-
'@graphcommerce/magento-cart-payment-method/PaymentMethodContext/PaymentMethodContext',
|
|
186
|
-
plugin: '@graphcommerce/magento-payment-braintree/plugins/AddBraintreeMethods',
|
|
187
|
-
},
|
|
188
|
-
]
|
|
189
|
-
|
|
198
|
+
it('it handles on duplicates gracefully', async () => {
|
|
190
199
|
const resolve = resolveDependency(projectRoot)
|
|
191
|
-
const interceptors = generateInterceptors(
|
|
192
|
-
|
|
200
|
+
const interceptors = await generateInterceptors(
|
|
201
|
+
[
|
|
202
|
+
{
|
|
203
|
+
type: 'component',
|
|
204
|
+
targetExport: 'PaymentMethodContextProvider',
|
|
205
|
+
sourceExport: 'Plugin',
|
|
206
|
+
enabled: true,
|
|
207
|
+
targetModule: '@graphcommerce/magento-cart-payment-method',
|
|
208
|
+
sourceModule: '@graphcommerce/magento-payment-braintree/plugins/AddBraintreeMethods',
|
|
209
|
+
},
|
|
210
|
+
{
|
|
211
|
+
type: 'component',
|
|
212
|
+
targetExport: 'PaymentMethodContextProvider',
|
|
213
|
+
sourceExport: 'Plugin',
|
|
214
|
+
enabled: true,
|
|
215
|
+
targetModule: '@graphcommerce/magento-cart-payment-method',
|
|
216
|
+
sourceModule: '@graphcommerce/magento-payment-braintree/plugins/AddBraintreeMethods',
|
|
217
|
+
},
|
|
218
|
+
],
|
|
219
|
+
resolve,
|
|
220
|
+
)
|
|
221
|
+
|
|
222
|
+
const result =
|
|
193
223
|
interceptors['packages/magento-cart-payment-method/PaymentMethodContext/PaymentMethodContext']
|
|
194
|
-
?.template
|
|
195
|
-
).toMatchInlineSnapshot(`
|
|
196
|
-
"
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
* - \`@graphcommerce/magento-payment-braintree/plugins/AddBraintreeMethods\`
|
|
208
|
-
*/
|
|
209
|
-
type PaymentMethodContextProviderProps = ComponentProps<typeof PaymentMethodContextProviderBase>
|
|
210
|
-
|
|
211
|
-
function AddBraintreeMethodsInterceptor(props: PaymentMethodContextProviderProps) {
|
|
212
|
-
return <AddBraintreeMethods {...props} Prev={PaymentMethodContextProviderBase} />
|
|
224
|
+
?.template
|
|
225
|
+
expectInterceptor(result).toMatchInlineSnapshot(`
|
|
226
|
+
"type PluginAddBraintreeMethodsInterceptorProps = DistributedOmit<
|
|
227
|
+
React.ComponentProps<typeof PluginAddBraintreeMethodsSource>,
|
|
228
|
+
'Prev'
|
|
229
|
+
>
|
|
230
|
+
const PluginAddBraintreeMethodsInterceptor = (props: PluginAddBraintreeMethodsInterceptorProps) => {
|
|
231
|
+
return (
|
|
232
|
+
<PluginAddBraintreeMethodsSource
|
|
233
|
+
{...props}
|
|
234
|
+
Prev={PaymentMethodContextProviderOriginal as React.FC}
|
|
235
|
+
/>
|
|
236
|
+
)
|
|
213
237
|
}
|
|
214
|
-
export const PaymentMethodContextProvider =
|
|
215
|
-
"
|
|
238
|
+
export const PaymentMethodContextProvider = PluginAddBraintreeMethodsInterceptor"
|
|
216
239
|
`)
|
|
217
240
|
})
|
|
218
241
|
|
|
219
|
-
it('
|
|
220
|
-
const plugins = [
|
|
221
|
-
{
|
|
222
|
-
component: 'AddProductsToCartForm',
|
|
223
|
-
enabled: true,
|
|
224
|
-
exported: '@graphcommerce/magento-product',
|
|
225
|
-
plugin: './plugins/EnableCrosssellsPlugin',
|
|
226
|
-
},
|
|
227
|
-
]
|
|
242
|
+
it('correctly renames all variable usages', async () => {
|
|
228
243
|
const resolve = resolveDependency(projectRoot)
|
|
229
|
-
const interceptors = generateInterceptors(
|
|
244
|
+
const interceptors = await generateInterceptors(
|
|
245
|
+
[
|
|
246
|
+
{
|
|
247
|
+
enabled: true,
|
|
248
|
+
targetModule: '@graphcommerce/magento-product',
|
|
249
|
+
sourceExport: 'Plugin',
|
|
250
|
+
type: 'component',
|
|
251
|
+
targetExport: 'ProductListItem',
|
|
252
|
+
sourceModule: '@graphcommerce/magento-compare/plugins/CompareAbleProductListItem',
|
|
253
|
+
},
|
|
254
|
+
],
|
|
255
|
+
resolve,
|
|
256
|
+
)
|
|
230
257
|
|
|
231
|
-
expect(interceptors[
|
|
232
|
-
"
|
|
258
|
+
expect(Object.keys(interceptors)[0]).toMatchInlineSnapshot(
|
|
259
|
+
`"packages/magento-product/components/ProductListItem/ProductListItem"`,
|
|
260
|
+
)
|
|
233
261
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
262
|
+
const template =
|
|
263
|
+
interceptors['packages/magento-product/components/ProductListItem/ProductListItem']?.template
|
|
264
|
+
expectOriginal(template).not.toContain('ProductListItem.selectors')
|
|
265
|
+
expectOriginal(template).toContain('ProductListItemOriginal.selectors')
|
|
266
|
+
})
|
|
238
267
|
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
268
|
+
it('it handles root plugins', async () => {
|
|
269
|
+
const resolve = resolveDependency(projectRoot)
|
|
270
|
+
const interceptors = await generateInterceptors(
|
|
271
|
+
[
|
|
272
|
+
{
|
|
273
|
+
type: 'component',
|
|
274
|
+
targetExport: 'AddProductsToCartForm',
|
|
275
|
+
sourceExport: 'Plugin',
|
|
276
|
+
enabled: true,
|
|
277
|
+
targetModule: '@graphcommerce/magento-product',
|
|
278
|
+
sourceModule: './plugins/EnableCrosssellsPlugin',
|
|
279
|
+
},
|
|
280
|
+
],
|
|
281
|
+
resolve,
|
|
282
|
+
)
|
|
245
283
|
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
export const AddProductsToCartForm = EnableCrosssellsPluginInterceptor
|
|
250
|
-
"
|
|
251
|
-
`)
|
|
284
|
+
expect(interceptors['packages/magento-product/index']?.template).toMatchInlineSnapshot(
|
|
285
|
+
`undefined`,
|
|
286
|
+
)
|
|
252
287
|
})
|
|
253
288
|
|
|
254
|
-
it('it handles root plugins
|
|
255
|
-
const plugins = [
|
|
256
|
-
{
|
|
257
|
-
component: 'OverlaySsr',
|
|
258
|
-
enabled: true,
|
|
259
|
-
exported: '@graphcommerce/next-ui/Overlay/components/OverlaySsr',
|
|
260
|
-
plugin: './plugins/EnableCrosssellsPlugin',
|
|
261
|
-
},
|
|
262
|
-
]
|
|
289
|
+
it('it handles root plugins and creates a relative path', async () => {
|
|
263
290
|
const resolve = resolveDependency(projectRoot)
|
|
264
|
-
|
|
291
|
+
|
|
292
|
+
const interceptors = await generateInterceptors(
|
|
293
|
+
[
|
|
294
|
+
{
|
|
295
|
+
type: 'component',
|
|
296
|
+
targetExport: 'OverlayBase',
|
|
297
|
+
sourceExport: 'Plugin',
|
|
298
|
+
enabled: true,
|
|
299
|
+
targetModule: '@graphcommerce/next-ui',
|
|
300
|
+
sourceModule: './plugins/EnableCrosssellsPlugin',
|
|
301
|
+
},
|
|
302
|
+
],
|
|
303
|
+
resolve,
|
|
304
|
+
)
|
|
305
|
+
|
|
306
|
+
expect(Object.keys(interceptors)[0]).toMatchInlineSnapshot(
|
|
307
|
+
`"packages/next-ui/Overlay/components/OverlayBase"`,
|
|
308
|
+
)
|
|
265
309
|
|
|
266
310
|
expect(
|
|
267
|
-
interceptors['packages/next-ui/Overlay/components/
|
|
311
|
+
interceptors['packages/next-ui/Overlay/components/OverlayBase'].targetExports.OverlayBase[0]
|
|
312
|
+
.sourceModule,
|
|
268
313
|
).toMatchInlineSnapshot(`"../../../../examples/magento-graphcms/plugins/EnableCrosssellsPlugin"`)
|
|
269
314
|
})
|
|
270
315
|
|
|
271
|
-
it('generates method interceptors alognside component interceptors', () => {
|
|
272
|
-
const
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
316
|
+
it('generates method interceptors alognside component interceptors', async () => {
|
|
317
|
+
const resolve = resolveDependency(projectRoot)
|
|
318
|
+
const interceptors = await generateInterceptors(
|
|
319
|
+
[
|
|
320
|
+
{
|
|
321
|
+
enabled: true,
|
|
322
|
+
targetModule: '@graphcommerce/graphql',
|
|
323
|
+
type: 'component',
|
|
324
|
+
sourceExport: 'Plugin',
|
|
325
|
+
targetExport: 'GraphQLProvider',
|
|
326
|
+
sourceModule: '@graphcommerce/magento-graphql/plugins/MagentoGraphqlGraphqlProvider',
|
|
327
|
+
},
|
|
328
|
+
{
|
|
329
|
+
type: 'function',
|
|
330
|
+
targetExport: 'graphqlConfig',
|
|
331
|
+
enabled: true,
|
|
332
|
+
sourceExport: 'Plugin',
|
|
333
|
+
targetModule: '@graphcommerce/graphql',
|
|
334
|
+
sourceModule: '@graphcommerce/magento-graphql/plugins/magentoInitMemoryCache',
|
|
335
|
+
},
|
|
336
|
+
{
|
|
337
|
+
type: 'function',
|
|
338
|
+
targetExport: 'graphqlConfig',
|
|
339
|
+
enabled: true,
|
|
340
|
+
sourceExport: 'Plugin',
|
|
341
|
+
targetModule: '@graphcommerce/graphql',
|
|
342
|
+
sourceModule: '@graphcommerce/magento-hygraph/plugins/hygraphInitMemoryCache',
|
|
343
|
+
},
|
|
344
|
+
],
|
|
345
|
+
resolve,
|
|
346
|
+
)
|
|
292
347
|
|
|
348
|
+
expect(Object.keys(interceptors)).toMatchInlineSnapshot(`
|
|
349
|
+
[
|
|
350
|
+
"packages/graphql/components/GraphQLProvider/GraphQLProvider",
|
|
351
|
+
"packages/graphql/config",
|
|
352
|
+
]
|
|
353
|
+
`)
|
|
354
|
+
expect(
|
|
355
|
+
interceptors['packages/graphql/components/GraphQLProvider/GraphQLProvider']?.template,
|
|
356
|
+
).toContain('MagentoGraphqlGraphqlProvider')
|
|
357
|
+
expect(interceptors['packages/graphql/config']?.template).toContain('magentoInitMemoryCache')
|
|
358
|
+
expect(interceptors['packages/graphql/config']?.template).toContain('hygraphInitMemoryCache')
|
|
359
|
+
})
|
|
360
|
+
|
|
361
|
+
it('adds debug logging to interceptors for components', async () => {
|
|
293
362
|
const resolve = resolveDependency(projectRoot)
|
|
294
|
-
const interceptors = generateInterceptors(
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
363
|
+
const interceptors = await generateInterceptors(
|
|
364
|
+
[
|
|
365
|
+
{
|
|
366
|
+
enabled: true,
|
|
367
|
+
targetModule: '@graphcommerce/graphql',
|
|
368
|
+
type: 'component',
|
|
369
|
+
sourceExport: 'Plugin',
|
|
370
|
+
targetExport: 'GraphQLProvider',
|
|
371
|
+
sourceModule: '@graphcommerce/magento-graphql/plugins/MagentoGraphqlGraphqlProvider',
|
|
372
|
+
},
|
|
373
|
+
{
|
|
374
|
+
type: 'function',
|
|
375
|
+
targetExport: 'graphqlConfig',
|
|
376
|
+
enabled: true,
|
|
377
|
+
sourceExport: 'plugin',
|
|
378
|
+
targetModule: '@graphcommerce/graphql',
|
|
379
|
+
sourceModule: '@graphcommerce/magento-graphql/plugins/magentoInitMemoryCache',
|
|
380
|
+
},
|
|
381
|
+
{
|
|
382
|
+
type: 'function',
|
|
383
|
+
targetExport: 'graphqlConfig',
|
|
384
|
+
enabled: true,
|
|
385
|
+
sourceExport: 'plugin',
|
|
386
|
+
targetModule: '@graphcommerce/graphql',
|
|
387
|
+
sourceModule: '@graphcommerce/magento-hygraph/plugins/hygraphInitMemoryCache',
|
|
388
|
+
},
|
|
389
|
+
],
|
|
390
|
+
resolve,
|
|
391
|
+
{ pluginStatus: true },
|
|
392
|
+
)
|
|
393
|
+
|
|
394
|
+
expectImport(interceptors['packages/graphql/config']?.template).toMatchInlineSnapshot(`
|
|
395
|
+
"import { plugin as pluginmagentoInitMemoryCacheSource } from '@graphcommerce/magento-graphql/plugins/magentoInitMemoryCache'
|
|
396
|
+
import { plugin as pluginhygraphInitMemoryCacheSource } from '@graphcommerce/magento-hygraph/plugins/hygraphInitMemoryCache'"
|
|
397
|
+
`)
|
|
398
|
+
|
|
399
|
+
expectOriginal(interceptors['packages/graphql/config']?.template).toMatchInlineSnapshot(`
|
|
400
|
+
"import { ApolloLink, TypePolicies } from '@apollo/client'
|
|
401
|
+
import type { GraphCommerceStorefrontConfig } from '@graphcommerce/next-config'
|
|
402
|
+
import { MigrateCache } from './components/GraphQLProvider/migrateCache'
|
|
403
|
+
export type ApolloClientConfigInput = {
|
|
404
|
+
storefront: GraphCommerceStorefrontConfig
|
|
405
|
+
links?: ApolloLink[]
|
|
406
|
+
policies?: TypePolicies[]
|
|
407
|
+
migrations?: MigrateCache[]
|
|
318
408
|
}
|
|
319
|
-
export
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
409
|
+
export type ApolloClientConfig = Required<ApolloClientConfigInput>
|
|
410
|
+
export function graphqlConfigOriginal(config: ApolloClientConfigInput): ApolloClientConfig {
|
|
411
|
+
const { storefront, links = [], policies = [], migrations = [] } = config
|
|
412
|
+
return {
|
|
413
|
+
storefront,
|
|
414
|
+
links,
|
|
415
|
+
policies,
|
|
416
|
+
migrations,
|
|
417
|
+
}
|
|
418
|
+
}"
|
|
419
|
+
`)
|
|
420
|
+
|
|
421
|
+
expectInterceptor(interceptors['packages/graphql/config']?.template).toMatchInlineSnapshot(`
|
|
422
|
+
"const logged: Set<string> = new Set()
|
|
423
|
+
const logOnce = (log: string, ...additional: unknown[]) => {
|
|
424
|
+
if (logged.has(log)) return
|
|
425
|
+
logged.add(log)
|
|
426
|
+
console.warn(log, ...additional)
|
|
329
427
|
}
|
|
330
|
-
|
|
331
|
-
|
|
428
|
+
|
|
429
|
+
const pluginhygraphInitMemoryCacheInterceptor: typeof graphqlConfigOriginal = (...args) => {
|
|
430
|
+
logOnce(
|
|
431
|
+
\`🔌 Calling graphqlConfig with plugin(s): pluginmagentoInitMemoryCache wrapping pluginhygraphInitMemoryCache wrapping graphqlConfig()\`,
|
|
432
|
+
)
|
|
433
|
+
return pluginhygraphInitMemoryCacheSource(graphqlConfigOriginal, ...args)
|
|
434
|
+
}
|
|
435
|
+
const pluginmagentoInitMemoryCacheInterceptor: typeof pluginhygraphInitMemoryCacheInterceptor = (
|
|
436
|
+
...args
|
|
437
|
+
) => {
|
|
438
|
+
logOnce(
|
|
439
|
+
\`🔌 Calling graphqlConfig with plugin(s): pluginhygraphInitMemoryCache wrapping pluginmagentoInitMemoryCache wrapping graphqlConfig()\`,
|
|
440
|
+
)
|
|
441
|
+
return pluginmagentoInitMemoryCacheSource(pluginhygraphInitMemoryCacheInterceptor, ...args)
|
|
332
442
|
}
|
|
333
|
-
export const
|
|
334
|
-
"
|
|
443
|
+
export const graphqlConfig = pluginmagentoInitMemoryCacheInterceptor"
|
|
335
444
|
`)
|
|
336
445
|
})
|
|
337
446
|
|
|
338
|
-
it('
|
|
339
|
-
const
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
447
|
+
it('correctly resolves when a source can not be parsed', async () => {
|
|
448
|
+
const resolve = resolveDependency(projectRoot)
|
|
449
|
+
const interceptors = await generateInterceptors(
|
|
450
|
+
[
|
|
451
|
+
{
|
|
452
|
+
type: 'component',
|
|
453
|
+
enabled: true,
|
|
454
|
+
sourceExport: 'Plugin',
|
|
455
|
+
sourceModule: '@graphcommerce/demo-magento-graphcommerce/plugins/DemoRowLinks',
|
|
456
|
+
targetModule: '@graphcommerce/next-ui',
|
|
457
|
+
targetExport: 'RowLinks',
|
|
458
|
+
},
|
|
459
|
+
],
|
|
460
|
+
resolve,
|
|
461
|
+
{ pluginStatus: true },
|
|
462
|
+
)
|
|
463
|
+
|
|
464
|
+
expect(Object.keys(interceptors)[0]).toMatchInlineSnapshot(
|
|
465
|
+
`"packages/next-ui/Row/RowLinks/RowLinks"`,
|
|
466
|
+
)
|
|
467
|
+
})
|
|
468
|
+
|
|
469
|
+
it('can correctly find the source for deeper chained exports', () => {
|
|
470
|
+
const resolve = resolveDependency(projectRoot)
|
|
471
|
+
const originalSource = findOriginalSource(
|
|
352
472
|
{
|
|
353
473
|
enabled: true,
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
474
|
+
sourceModule: '@graphcommerce/demo-magento-graphcommerce/plugins/DemoRowLinks',
|
|
475
|
+
sourceExport: 'Plugin',
|
|
476
|
+
targetModule: '@graphcommerce/next-ui',
|
|
477
|
+
targetExport: 'BlogTag',
|
|
478
|
+
type: 'component',
|
|
357
479
|
},
|
|
358
|
-
|
|
480
|
+
resolve('@graphcommerce/next-ui', { includeSources: true }),
|
|
481
|
+
resolve,
|
|
482
|
+
)
|
|
483
|
+
expect(originalSource.error).toBeUndefined()
|
|
484
|
+
expect(originalSource.resolved?.dependency).toMatchInlineSnapshot(
|
|
485
|
+
`"@graphcommerce/next-ui/Blog/BlogTags/BlogTag"`,
|
|
486
|
+
)
|
|
487
|
+
})
|
|
359
488
|
|
|
489
|
+
it('Should apply overrides to the correct file', async () => {
|
|
360
490
|
const resolve = resolveDependency(projectRoot)
|
|
361
|
-
const interceptors = generateInterceptors(
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
} from '.'
|
|
375
|
-
|
|
376
|
-
const logged: Set<string> = new Set();
|
|
377
|
-
const logInterceptor = (log: string, ...additional: unknown[]) => {
|
|
378
|
-
if (logged.has(log)) return
|
|
379
|
-
logged.add(log)
|
|
380
|
-
console.log(log, ...additional)
|
|
381
|
-
}
|
|
491
|
+
const interceptors = await generateInterceptors(
|
|
492
|
+
[
|
|
493
|
+
{
|
|
494
|
+
enabled: true,
|
|
495
|
+
sourceModule: './plugins/replaceGetProductStaticPaths',
|
|
496
|
+
sourceExport: 'getProductStaticPaths',
|
|
497
|
+
targetModule: '@graphcommerce/magento-product',
|
|
498
|
+
targetExport: 'getProductStaticPaths',
|
|
499
|
+
type: 'replace',
|
|
500
|
+
},
|
|
501
|
+
],
|
|
502
|
+
resolve,
|
|
503
|
+
)
|
|
382
504
|
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
* - \`@graphcommerce/magento-graphql/plugins/MagentoGraphqlGraphqlProvider\`
|
|
387
|
-
*/
|
|
388
|
-
type GraphQLProviderProps = ComponentProps<typeof GraphQLProviderBase>
|
|
505
|
+
expect(Object.keys(interceptors)[0]).toMatchInlineSnapshot(
|
|
506
|
+
`"packages/magento-product/components/ProductStaticPaths/getProductStaticPaths"`,
|
|
507
|
+
)
|
|
389
508
|
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
509
|
+
const result =
|
|
510
|
+
interceptors['packages/magento-product/components/ProductStaticPaths/getProductStaticPaths']
|
|
511
|
+
?.template
|
|
512
|
+
expectImport(result).toMatchInlineSnapshot(
|
|
513
|
+
`"import { getProductStaticPaths as getProductStaticPathsreplaceGetProductStaticPathsSource } from '../../../../plugins/replaceGetProductStaticPaths'"`,
|
|
514
|
+
)
|
|
515
|
+
|
|
516
|
+
expectOriginal(result).toContain(`getProductStaticPathsDisabled`)
|
|
517
|
+
})
|
|
518
|
+
|
|
519
|
+
it('Should report an error when multiple files are overriding the same export', () => {})
|
|
520
|
+
|
|
521
|
+
it('correctly reports an error for an incorrect export', async () => {
|
|
522
|
+
const fakeconfig = {
|
|
523
|
+
googleRecaptchaKey: '123',
|
|
524
|
+
googleAnalyticsId: '123',
|
|
525
|
+
demoMode: true,
|
|
526
|
+
} as GraphCommerceConfig
|
|
527
|
+
|
|
528
|
+
const src = `
|
|
529
|
+
import { getSitemapPaths as getSitemapPathsType } from '@graphcommerce/magento-product'
|
|
530
|
+
import { IfConfig, MethodPlugin } from '@graphcommerce/next-config'
|
|
531
|
+
|
|
532
|
+
export const method = 'getSitemapPaths'
|
|
533
|
+
export const exported = '@graphcommerce/magento-product'
|
|
534
|
+
export const ifConfig: IfConfig = 'demoMode'
|
|
535
|
+
|
|
536
|
+
export const plugin: MethodPlugin<typeof getSitemapPathsType> = (prev, ...args) => {
|
|
537
|
+
console.log('getSitemapPaths plugin ran!')
|
|
538
|
+
return prev(...args)
|
|
539
|
+
}
|
|
540
|
+
`
|
|
541
|
+
|
|
542
|
+
console.error = jest.fn()
|
|
543
|
+
const plugins = parseStructure(parseSync(src), fakeconfig, './plugins/MyPlugin.tsx')
|
|
544
|
+
|
|
545
|
+
// @ts-expect-error mock not typed
|
|
546
|
+
expect(console.error.mock.calls[0][0]).toMatchInlineSnapshot(
|
|
547
|
+
`"Plugin configuration invalid! See ./plugins/MyPlugin.tsx"`,
|
|
548
|
+
)
|
|
549
|
+
|
|
550
|
+
expect(plugins).toMatchInlineSnapshot(`[]`)
|
|
551
|
+
const result = await generateInterceptors(plugins, resolveDependency(projectRoot))
|
|
552
|
+
|
|
553
|
+
expect(Object.keys(result)).toMatchInlineSnapshot(`[]`)
|
|
554
|
+
})
|
|
555
|
+
|
|
556
|
+
it('generated a correct file if a replacement and a plugin is applied to the same export', async () => {
|
|
557
|
+
const src1 = `import { ProductPageNameProps } from '@graphcommerce/magento-product'
|
|
558
|
+
import { PluginConfig } from '@graphcommerce/next-config'
|
|
559
|
+
|
|
560
|
+
export const config: PluginConfig = {
|
|
561
|
+
type: 'replace',
|
|
562
|
+
module: '@graphcommerce/magento-product',
|
|
563
|
+
ifConfig: 'demoMode',
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
export function ProductPageName(props: ProductPageNameProps) {
|
|
567
|
+
const { product } = props
|
|
568
|
+
return <div>Complete overrides {product.url_key}</div>
|
|
569
|
+
}
|
|
570
|
+
`
|
|
571
|
+
|
|
572
|
+
const src2 = `import type { AddToCartItemSelector, ProductPageNameProps } from '@graphcommerce/magento-product'
|
|
573
|
+
import type { IfConfig, PluginProps } from '@graphcommerce/next-config'
|
|
574
|
+
import { useConfigurableSelectedVariant } from '../../hooks'
|
|
575
|
+
|
|
576
|
+
export const component = 'ProductPageName'
|
|
577
|
+
export const exported = '@graphcommerce/magento-product'
|
|
578
|
+
export const ifConfig: IfConfig = 'configurableVariantValues.content'
|
|
579
|
+
|
|
580
|
+
const ConfigurableProductPageName = (
|
|
581
|
+
props: PluginProps<ProductPageNameProps> & AddToCartItemSelector,
|
|
582
|
+
) => {
|
|
583
|
+
const { Prev, product, index, ...rest } = props
|
|
584
|
+
const variant = useConfigurableSelectedVariant({ url_key: product.url_key, index })
|
|
585
|
+
return <Prev product={variant ?? product} {...rest} />
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
export const Plugin = ConfigurableProductPageName
|
|
589
|
+
`
|
|
590
|
+
|
|
591
|
+
const config = {
|
|
592
|
+
demoMode: true,
|
|
593
|
+
configurableVariantForSimple: true,
|
|
594
|
+
configurableVariantValues: { content: true, gallery: true, url: true },
|
|
595
|
+
} as GraphCommerceConfig
|
|
596
|
+
|
|
597
|
+
const firstFile = parseStructure(parseSync(src1), config, './plugins/MyPlugin')
|
|
598
|
+
|
|
599
|
+
const secondFile = parseStructure(
|
|
600
|
+
parseSync(src2),
|
|
601
|
+
config,
|
|
602
|
+
'@graphcommerce/magento-product-configurable/plugins/ConfigurableProductPage/ConfigurableProductPageName',
|
|
603
|
+
)
|
|
604
|
+
|
|
605
|
+
const plugins = [...firstFile, ...secondFile]
|
|
606
|
+
|
|
607
|
+
expect(plugins).toMatchInlineSnapshot(`
|
|
608
|
+
[
|
|
609
|
+
{
|
|
610
|
+
"enabled": true,
|
|
611
|
+
"ifConfig": "demoMode",
|
|
612
|
+
"sourceExport": "ProductPageName",
|
|
613
|
+
"sourceModule": "./plugins/MyPlugin",
|
|
614
|
+
"targetExport": "ProductPageName",
|
|
615
|
+
"targetModule": "@graphcommerce/magento-product",
|
|
616
|
+
"type": "replace",
|
|
617
|
+
},
|
|
618
|
+
{
|
|
619
|
+
"enabled": true,
|
|
620
|
+
"ifConfig": "configurableVariantValues.content",
|
|
621
|
+
"sourceExport": "Plugin",
|
|
622
|
+
"sourceModule": "@graphcommerce/magento-product-configurable/plugins/ConfigurableProductPage/ConfigurableProductPageName",
|
|
623
|
+
"targetExport": "ProductPageName",
|
|
624
|
+
"targetModule": "@graphcommerce/magento-product",
|
|
625
|
+
"type": "component",
|
|
626
|
+
},
|
|
627
|
+
]
|
|
628
|
+
`)
|
|
629
|
+
|
|
630
|
+
const interceptors = await generateInterceptors(plugins, resolveDependency(projectRoot))
|
|
631
|
+
|
|
632
|
+
expect(Object.keys(interceptors)[0]).toMatchInlineSnapshot(
|
|
633
|
+
`"packages/magento-product/components/ProductPageName/ProductPageName"`,
|
|
634
|
+
)
|
|
635
|
+
|
|
636
|
+
const result =
|
|
637
|
+
interceptors['packages/magento-product/components/ProductPageName/ProductPageName']?.template
|
|
638
|
+
|
|
639
|
+
expectImport(result).toMatchInlineSnapshot(`
|
|
640
|
+
"import type { DistributedOmit } from 'type-fest'
|
|
641
|
+
|
|
642
|
+
import { Plugin as PluginConfigurableProductPageNameSource } from '@graphcommerce/magento-product-configurable/plugins/ConfigurableProductPage/ConfigurableProductPageName'
|
|
643
|
+
import { ProductPageName as ProductPageNameMyPluginSource } from '../../../../plugins/MyPlugin'"
|
|
644
|
+
`)
|
|
645
|
+
|
|
646
|
+
expectOriginal(result).toMatchInlineSnapshot(`
|
|
647
|
+
"import { ProductPageNameFragment } from './ProductPageName.gql'
|
|
648
|
+
export type ProductPageNameProps = {
|
|
649
|
+
product: ProductPageNameFragment
|
|
405
650
|
}
|
|
406
|
-
const
|
|
407
|
-
|
|
651
|
+
export const ProductPageNameDisabled = (props: ProductPageNameProps) => {
|
|
652
|
+
const { product } = props
|
|
653
|
+
return <>{product.name}</>
|
|
654
|
+
}"
|
|
655
|
+
`)
|
|
656
|
+
|
|
657
|
+
expectInterceptor(result).toMatchInlineSnapshot(`
|
|
658
|
+
"type ProductPageNameMyPluginInterceptorProps = React.ComponentProps<
|
|
659
|
+
typeof ProductPageNameMyPluginSource
|
|
660
|
+
>
|
|
661
|
+
|
|
662
|
+
type PluginConfigurableProductPageNameInterceptorProps = DistributedOmit<
|
|
663
|
+
React.ComponentProps<typeof PluginConfigurableProductPageNameSource>,
|
|
664
|
+
'Prev'
|
|
665
|
+
>
|
|
666
|
+
const PluginConfigurableProductPageNameInterceptor = (
|
|
667
|
+
props: ProductPageNameMyPluginInterceptorProps &
|
|
668
|
+
PluginConfigurableProductPageNameInterceptorProps,
|
|
669
|
+
) => {
|
|
670
|
+
return (
|
|
671
|
+
<PluginConfigurableProductPageNameSource
|
|
672
|
+
{...props}
|
|
673
|
+
Prev={ProductPageNameMyPluginSource as React.FC}
|
|
674
|
+
/>
|
|
675
|
+
)
|
|
408
676
|
}
|
|
409
|
-
export const
|
|
410
|
-
"
|
|
677
|
+
export const ProductPageName = PluginConfigurableProductPageNameInterceptor"
|
|
411
678
|
`)
|
|
412
679
|
})
|
|
680
|
+
|
|
681
|
+
it('generates to a .ts file when the target file is a .ts as well', async () => {
|
|
682
|
+
const resolve = resolveDependency(projectRoot)
|
|
683
|
+
const interceptors = await generateInterceptors(
|
|
684
|
+
[
|
|
685
|
+
{
|
|
686
|
+
type: 'function',
|
|
687
|
+
targetExport: 'graphqlConfig',
|
|
688
|
+
enabled: true,
|
|
689
|
+
sourceExport: 'Plugin',
|
|
690
|
+
targetModule: '@graphcommerce/graphql',
|
|
691
|
+
sourceModule: '@graphcommerce/magento-graphql/plugins/magentoInitMemoryCache',
|
|
692
|
+
},
|
|
693
|
+
],
|
|
694
|
+
resolve,
|
|
695
|
+
)
|
|
696
|
+
|
|
697
|
+
expect(Object.keys(interceptors)[0]).toMatchInlineSnapshot(`"packages/graphql/config"`)
|
|
698
|
+
const interceptor = interceptors['packages/graphql/config']
|
|
699
|
+
expect(interceptor.sourcePath).toBe(`packages/graphql/config.ts`)
|
|
700
|
+
})
|