@faststore/core 4.6.0-dev.1 → 4.6.0-dev.3
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/.turbo/turbo-generate.log +4 -4
- package/.turbo/turbo-test.log +104 -98
- package/@generated/cached-operations.json +1 -1
- package/@generated/gql.ts +9 -9
- package/@generated/graphql.ts +18 -12
- package/@generated/persisted-documents.json +2 -2
- package/@generated/schema.graphql +6 -0
- package/CHANGELOG.md +13 -0
- package/package.json +6 -6
- package/src/components/sections/RecommendationShelf/README.md +4 -1
- package/src/components/sections/RecommendationShelf/RecommendationShelf.tsx +11 -92
- package/src/components/sections/RecommendationShelf/RecommendationShelf.types.ts +3 -18
- package/src/sdk/cart/index.ts +15 -1
- package/src/sdk/cart/waitForSessionValidated.ts +43 -0
- package/src/sdk/recommendations/index.ts +17 -0
- package/src/sdk/recommendations/types.ts +17 -0
- package/src/sdk/recommendations/useRecommendationShelf.ts +141 -0
- package/src/{components/sections/RecommendationShelf → sdk/recommendations}/vrn.ts +1 -1
- package/src/sdk/session/index.ts +14 -0
- package/src/sdk/session/syncSalesChannelFromOrderForm.ts +68 -0
- package/test/components/RecommendationShelf/RecommendationShelf.browser.test.tsx +6 -12
- package/test/sdk/cart/waitForSessionValidated.test.ts +96 -0
- package/test/sdk/recommendations/useRecommendationShelf.browser.test.tsx +199 -0
- package/test/{components/RecommendationShelf → sdk/recommendations}/useRecommendationUserId.test.ts +1 -1
- package/test/{components/RecommendationShelf → sdk/recommendations}/useRecommendations.test.ts +1 -1
- package/test/{components/RecommendationShelf → sdk/recommendations}/vrn.test.ts +1 -4
- package/test/sdk/session/syncSalesChannelFromOrderForm.test.ts +125 -0
- /package/src/{components/sections/RecommendationShelf → sdk/recommendations}/useRecommendationUserId.ts +0 -0
- /package/src/{components/sections/RecommendationShelf → sdk/recommendations}/useRecommendations.ts +0 -0
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest'
|
|
2
|
+
import type { Session } from '@faststore/sdk'
|
|
3
|
+
import { syncSalesChannelFromOrderForm } from '../../../src/sdk/session/syncSalesChannelFromOrderForm'
|
|
4
|
+
|
|
5
|
+
const baseSession = (
|
|
6
|
+
salesChannel: string,
|
|
7
|
+
explicit = true,
|
|
8
|
+
channelOverride?: string | null
|
|
9
|
+
): Session => ({
|
|
10
|
+
currency: { code: 'BRL', symbol: 'R$' },
|
|
11
|
+
locale: 'pt-BR',
|
|
12
|
+
country: 'BRA',
|
|
13
|
+
channel:
|
|
14
|
+
channelOverride !== undefined
|
|
15
|
+
? channelOverride
|
|
16
|
+
: JSON.stringify({
|
|
17
|
+
salesChannel,
|
|
18
|
+
regionId: '',
|
|
19
|
+
hasOnlyDefaultSalesChannel: !explicit,
|
|
20
|
+
}),
|
|
21
|
+
deliveryMode: null,
|
|
22
|
+
addressType: null,
|
|
23
|
+
city: null,
|
|
24
|
+
postalCode: null,
|
|
25
|
+
geoCoordinates: null,
|
|
26
|
+
person: null,
|
|
27
|
+
b2b: null,
|
|
28
|
+
marketingData: null,
|
|
29
|
+
refreshAfter: null,
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
describe('syncSalesChannelFromOrderForm', () => {
|
|
33
|
+
it('updates session channel silently when SC diverges', () => {
|
|
34
|
+
const setSilent = vi.fn()
|
|
35
|
+
const synced = syncSalesChannelFromOrderForm(
|
|
36
|
+
'2',
|
|
37
|
+
() => baseSession('1', false),
|
|
38
|
+
setSilent
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
expect(synced).toBe(true)
|
|
42
|
+
expect(setSilent).toHaveBeenCalledTimes(1)
|
|
43
|
+
const next = setSilent.mock.calls[0][0] as Session
|
|
44
|
+
expect(JSON.parse(next.channel ?? '{}')).toMatchObject({
|
|
45
|
+
salesChannel: '2',
|
|
46
|
+
hasOnlyDefaultSalesChannel: false,
|
|
47
|
+
})
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
it('is a no-op when SC already matches', () => {
|
|
51
|
+
const setSilent = vi.fn()
|
|
52
|
+
const synced = syncSalesChannelFromOrderForm(
|
|
53
|
+
'2',
|
|
54
|
+
() => baseSession('2'),
|
|
55
|
+
setSilent
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
expect(synced).toBe(false)
|
|
59
|
+
expect(setSilent).not.toHaveBeenCalled()
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
it('is a no-op when adopted SC is missing', () => {
|
|
63
|
+
const setSilent = vi.fn()
|
|
64
|
+
expect(
|
|
65
|
+
syncSalesChannelFromOrderForm(null, () => baseSession('1'), setSilent)
|
|
66
|
+
).toBe(false)
|
|
67
|
+
expect(
|
|
68
|
+
syncSalesChannelFromOrderForm('', () => baseSession('1'), setSilent)
|
|
69
|
+
).toBe(false)
|
|
70
|
+
expect(setSilent).not.toHaveBeenCalled()
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
it('resets non-object channel JSON before adopting SC', () => {
|
|
74
|
+
const setSilent = vi.fn()
|
|
75
|
+
|
|
76
|
+
for (const invalid of ['null', '[]', '"text"', '0', 'false']) {
|
|
77
|
+
setSilent.mockClear()
|
|
78
|
+
const synced = syncSalesChannelFromOrderForm(
|
|
79
|
+
'2',
|
|
80
|
+
() => baseSession('1', true, invalid),
|
|
81
|
+
setSilent
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
expect(synced).toBe(true)
|
|
85
|
+
expect(JSON.parse(setSilent.mock.calls[0][0].channel)).toMatchObject({
|
|
86
|
+
salesChannel: '2',
|
|
87
|
+
hasOnlyDefaultSalesChannel: false,
|
|
88
|
+
})
|
|
89
|
+
}
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
it('resets malformed channel JSON before adopting SC', () => {
|
|
93
|
+
const setSilent = vi.fn()
|
|
94
|
+
const synced = syncSalesChannelFromOrderForm(
|
|
95
|
+
'2',
|
|
96
|
+
() => baseSession('1', true, '{'),
|
|
97
|
+
setSilent
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
expect(synced).toBe(true)
|
|
101
|
+
expect(JSON.parse(setSilent.mock.calls[0][0].channel)).toMatchObject({
|
|
102
|
+
salesChannel: '2',
|
|
103
|
+
hasOnlyDefaultSalesChannel: false,
|
|
104
|
+
})
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
it('ignores object-valued salesChannel when comparing', () => {
|
|
108
|
+
const setSilent = vi.fn()
|
|
109
|
+
const synced = syncSalesChannelFromOrderForm(
|
|
110
|
+
'2',
|
|
111
|
+
() =>
|
|
112
|
+
baseSession(
|
|
113
|
+
'1',
|
|
114
|
+
true,
|
|
115
|
+
JSON.stringify({ salesChannel: { nested: true } })
|
|
116
|
+
),
|
|
117
|
+
setSilent
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
expect(synced).toBe(true)
|
|
121
|
+
expect(JSON.parse(setSilent.mock.calls[0][0].channel).salesChannel).toBe(
|
|
122
|
+
'2'
|
|
123
|
+
)
|
|
124
|
+
})
|
|
125
|
+
})
|
|
File without changes
|
/package/src/{components/sections/RecommendationShelf → sdk/recommendations}/useRecommendations.ts
RENAMED
|
File without changes
|