@teleporthq/teleport-plugin-common 0.45.3-alpha.0 → 0.45.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/__tests__/builders/html-builders.ts +9 -2
- package/__tests__/builders/style-builders.ts +135 -1
- package/__tests__/node-handlers/node-to-html/index.ts +2 -0
- package/__tests__/node-handlers/node-to-jsx/attribute-value-mapper.ts +77 -0
- package/__tests__/node-handlers/node-to-jsx/calendar-events-reviver.ts +176 -0
- package/__tests__/node-handlers/node-to-jsx/chart-data-guard.ts +101 -0
- package/__tests__/node-handlers/node-to-jsx/cms-list-repeater-ecommerce-source.ts +79 -0
- package/__tests__/node-handlers/node-to-jsx/cms-list-repeater-loading.ts +122 -0
- package/__tests__/node-handlers/node-to-jsx/index.ts +719 -3
- package/__tests__/node-handlers/node-to-jsx/url-search-params-expression.ts +59 -0
- package/__tests__/node-handlers/node-to-jsx/utils.ts +635 -3
- package/__tests__/utils/ast-utils.ts +188 -0
- package/__tests__/utils/content-property-sanitiser.ts +156 -0
- package/__tests__/utils/page-seeds-state-from-data-source.ts +70 -0
- package/__tests__/utils/parse-string-with-template-expressions.ts +82 -0
- package/__tests__/utils/resource-auth-scheme.ts +55 -0
- package/__tests__/utils/route-utils.ts +118 -0
- package/__tests__/utils/style-utils.ts +59 -0
- package/__tests__/utils/template-expression-balance.ts +83 -0
- package/__tests__/utils/url-query-writer.ts +239 -0
- package/__tests__/utils/url-search-param-sync.ts +138 -0
- package/__tests__/utils/url-search-params.ts +48 -0
- package/dist/cjs/builders/ast-builders.d.ts +2 -0
- package/dist/cjs/builders/ast-builders.d.ts.map +1 -1
- package/dist/cjs/builders/ast-builders.js +41 -3
- package/dist/cjs/builders/ast-builders.js.map +1 -1
- package/dist/cjs/builders/hast-builders.d.ts +1 -0
- package/dist/cjs/builders/hast-builders.d.ts.map +1 -1
- package/dist/cjs/builders/hast-builders.js +11 -1
- package/dist/cjs/builders/hast-builders.js.map +1 -1
- package/dist/cjs/builders/style-builders.d.ts +8 -2
- package/dist/cjs/builders/style-builders.d.ts.map +1 -1
- package/dist/cjs/builders/style-builders.js +80 -33
- package/dist/cjs/builders/style-builders.js.map +1 -1
- package/dist/cjs/index.d.ts +5 -1
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +10 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/node-handlers/node-to-html/constants.d.ts.map +1 -1
- package/dist/cjs/node-handlers/node-to-html/constants.js +2 -0
- package/dist/cjs/node-handlers/node-to-html/constants.js.map +1 -1
- package/dist/cjs/node-handlers/node-to-html/index.d.ts.map +1 -1
- package/dist/cjs/node-handlers/node-to-html/index.js +69 -1
- package/dist/cjs/node-handlers/node-to-html/index.js.map +1 -1
- package/dist/cjs/node-handlers/node-to-html/types.d.ts +5 -1
- package/dist/cjs/node-handlers/node-to-html/types.d.ts.map +1 -1
- package/dist/cjs/node-handlers/node-to-html/utils.d.ts +2 -1
- package/dist/cjs/node-handlers/node-to-html/utils.d.ts.map +1 -1
- package/dist/cjs/node-handlers/node-to-html/utils.js +174 -13
- package/dist/cjs/node-handlers/node-to-html/utils.js.map +1 -1
- package/dist/cjs/node-handlers/node-to-jsx/index.d.ts.map +1 -1
- package/dist/cjs/node-handlers/node-to-jsx/index.js +1443 -45
- package/dist/cjs/node-handlers/node-to-jsx/index.js.map +1 -1
- package/dist/cjs/node-handlers/node-to-jsx/types.d.ts +60 -6
- package/dist/cjs/node-handlers/node-to-jsx/types.d.ts.map +1 -1
- package/dist/cjs/node-handlers/node-to-jsx/utils.d.ts +80 -7
- package/dist/cjs/node-handlers/node-to-jsx/utils.d.ts.map +1 -1
- package/dist/cjs/node-handlers/node-to-jsx/utils.js +883 -72
- package/dist/cjs/node-handlers/node-to-jsx/utils.js.map +1 -1
- package/dist/cjs/tsconfig.tsbuildinfo +1 -1
- package/dist/cjs/utils/ast-utils.d.ts +163 -11
- package/dist/cjs/utils/ast-utils.d.ts.map +1 -1
- package/dist/cjs/utils/ast-utils.js +1013 -75
- package/dist/cjs/utils/ast-utils.js.map +1 -1
- package/dist/cjs/utils/hast-utils.d.ts +8 -6
- package/dist/cjs/utils/hast-utils.d.ts.map +1 -1
- package/dist/cjs/utils/hast-utils.js +28 -4
- package/dist/cjs/utils/hast-utils.js.map +1 -1
- package/dist/cjs/utils/nested-selector-commas.d.ts +2 -0
- package/dist/cjs/utils/nested-selector-commas.d.ts.map +1 -0
- package/dist/cjs/utils/nested-selector-commas.js +81 -0
- package/dist/cjs/utils/nested-selector-commas.js.map +1 -0
- package/dist/cjs/utils/route-utils.d.ts +117 -0
- package/dist/cjs/utils/route-utils.d.ts.map +1 -0
- package/dist/cjs/utils/route-utils.js +212 -0
- package/dist/cjs/utils/route-utils.js.map +1 -0
- package/dist/cjs/utils/style-utils.d.ts +1 -0
- package/dist/cjs/utils/style-utils.d.ts.map +1 -1
- package/dist/cjs/utils/style-utils.js +181 -2
- package/dist/cjs/utils/style-utils.js.map +1 -1
- package/dist/cjs/utils/template-expression-balance.d.ts +58 -0
- package/dist/cjs/utils/template-expression-balance.d.ts.map +1 -0
- package/dist/cjs/utils/template-expression-balance.js +181 -0
- package/dist/cjs/utils/template-expression-balance.js.map +1 -0
- package/dist/cjs/utils/types.d.ts +3 -0
- package/dist/cjs/utils/types.d.ts.map +1 -0
- package/dist/cjs/utils/types.js +3 -0
- package/dist/cjs/utils/types.js.map +1 -0
- package/dist/cjs/utils/url-query-writer.d.ts +123 -0
- package/dist/cjs/utils/url-query-writer.d.ts.map +1 -0
- package/dist/cjs/utils/url-query-writer.js +260 -0
- package/dist/cjs/utils/url-query-writer.js.map +1 -0
- package/dist/cjs/utils/url-search-param-sync.d.ts +4 -0
- package/dist/cjs/utils/url-search-param-sync.d.ts.map +1 -0
- package/dist/cjs/utils/url-search-param-sync.js +241 -0
- package/dist/cjs/utils/url-search-param-sync.js.map +1 -0
- package/dist/cjs/utils/url-search-params.d.ts +48 -0
- package/dist/cjs/utils/url-search-params.d.ts.map +1 -0
- package/dist/cjs/utils/url-search-params.js +109 -0
- package/dist/cjs/utils/url-search-params.js.map +1 -0
- package/dist/esm/builders/ast-builders.d.ts +2 -0
- package/dist/esm/builders/ast-builders.d.ts.map +1 -1
- package/dist/esm/builders/ast-builders.js +40 -4
- package/dist/esm/builders/ast-builders.js.map +1 -1
- package/dist/esm/builders/hast-builders.d.ts +1 -0
- package/dist/esm/builders/hast-builders.d.ts.map +1 -1
- package/dist/esm/builders/hast-builders.js +9 -0
- package/dist/esm/builders/hast-builders.js.map +1 -1
- package/dist/esm/builders/style-builders.d.ts +8 -2
- package/dist/esm/builders/style-builders.d.ts.map +1 -1
- package/dist/esm/builders/style-builders.js +78 -32
- package/dist/esm/builders/style-builders.js.map +1 -1
- package/dist/esm/index.d.ts +5 -1
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +5 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/node-handlers/node-to-html/constants.d.ts.map +1 -1
- package/dist/esm/node-handlers/node-to-html/constants.js +2 -0
- package/dist/esm/node-handlers/node-to-html/constants.js.map +1 -1
- package/dist/esm/node-handlers/node-to-html/index.d.ts.map +1 -1
- package/dist/esm/node-handlers/node-to-html/index.js +71 -3
- package/dist/esm/node-handlers/node-to-html/index.js.map +1 -1
- package/dist/esm/node-handlers/node-to-html/types.d.ts +5 -1
- package/dist/esm/node-handlers/node-to-html/types.d.ts.map +1 -1
- package/dist/esm/node-handlers/node-to-html/utils.d.ts +2 -1
- package/dist/esm/node-handlers/node-to-html/utils.d.ts.map +1 -1
- package/dist/esm/node-handlers/node-to-html/utils.js +170 -13
- package/dist/esm/node-handlers/node-to-html/utils.js.map +1 -1
- package/dist/esm/node-handlers/node-to-jsx/index.d.ts.map +1 -1
- package/dist/esm/node-handlers/node-to-jsx/index.js +1442 -47
- package/dist/esm/node-handlers/node-to-jsx/index.js.map +1 -1
- package/dist/esm/node-handlers/node-to-jsx/types.d.ts +60 -6
- package/dist/esm/node-handlers/node-to-jsx/types.d.ts.map +1 -1
- package/dist/esm/node-handlers/node-to-jsx/utils.d.ts +80 -7
- package/dist/esm/node-handlers/node-to-jsx/utils.d.ts.map +1 -1
- package/dist/esm/node-handlers/node-to-jsx/utils.js +875 -70
- package/dist/esm/node-handlers/node-to-jsx/utils.js.map +1 -1
- package/dist/esm/tsconfig.tsbuildinfo +1 -1
- package/dist/esm/utils/ast-utils.d.ts +163 -11
- package/dist/esm/utils/ast-utils.d.ts.map +1 -1
- package/dist/esm/utils/ast-utils.js +989 -75
- package/dist/esm/utils/ast-utils.js.map +1 -1
- package/dist/esm/utils/hast-utils.d.ts +8 -6
- package/dist/esm/utils/hast-utils.d.ts.map +1 -1
- package/dist/esm/utils/hast-utils.js +26 -3
- package/dist/esm/utils/hast-utils.js.map +1 -1
- package/dist/esm/utils/nested-selector-commas.d.ts +2 -0
- package/dist/esm/utils/nested-selector-commas.d.ts.map +1 -0
- package/dist/esm/utils/nested-selector-commas.js +77 -0
- package/dist/esm/utils/nested-selector-commas.js.map +1 -0
- package/dist/esm/utils/route-utils.d.ts +117 -0
- package/dist/esm/utils/route-utils.d.ts.map +1 -0
- package/dist/esm/utils/route-utils.js +179 -0
- package/dist/esm/utils/route-utils.js.map +1 -0
- package/dist/esm/utils/style-utils.d.ts +1 -0
- package/dist/esm/utils/style-utils.d.ts.map +1 -1
- package/dist/esm/utils/style-utils.js +179 -1
- package/dist/esm/utils/style-utils.js.map +1 -1
- package/dist/esm/utils/template-expression-balance.d.ts +58 -0
- package/dist/esm/utils/template-expression-balance.d.ts.map +1 -0
- package/dist/esm/utils/template-expression-balance.js +174 -0
- package/dist/esm/utils/template-expression-balance.js.map +1 -0
- package/dist/esm/utils/types.d.ts +3 -0
- package/dist/esm/utils/types.d.ts.map +1 -0
- package/dist/esm/utils/types.js +2 -0
- package/dist/esm/utils/types.js.map +1 -0
- package/dist/esm/utils/url-query-writer.d.ts +123 -0
- package/dist/esm/utils/url-query-writer.d.ts.map +1 -0
- package/dist/esm/utils/url-query-writer.js +229 -0
- package/dist/esm/utils/url-query-writer.js.map +1 -0
- package/dist/esm/utils/url-search-param-sync.d.ts +4 -0
- package/dist/esm/utils/url-search-param-sync.d.ts.map +1 -0
- package/dist/esm/utils/url-search-param-sync.js +213 -0
- package/dist/esm/utils/url-search-param-sync.js.map +1 -0
- package/dist/esm/utils/url-search-params.d.ts +48 -0
- package/dist/esm/utils/url-search-params.d.ts.map +1 -0
- package/dist/esm/utils/url-search-params.js +81 -0
- package/dist/esm/utils/url-search-params.js.map +1 -0
- package/package.json +4 -4
- package/src/builders/ast-builders.ts +80 -3
- package/src/builders/hast-builders.ts +11 -0
- package/src/builders/style-builders.ts +121 -43
- package/src/index.ts +22 -1
- package/src/node-handlers/node-to-html/constants.ts +2 -0
- package/src/node-handlers/node-to-html/index.ts +126 -6
- package/src/node-handlers/node-to-html/types.ts +6 -0
- package/src/node-handlers/node-to-html/utils.ts +228 -14
- package/src/node-handlers/node-to-jsx/index.ts +2135 -62
- package/src/node-handlers/node-to-jsx/types.ts +75 -32
- package/src/node-handlers/node-to-jsx/utils.ts +1173 -84
- package/src/utils/ast-utils.ts +1316 -127
- package/src/utils/hast-utils.ts +37 -9
- package/src/utils/nested-selector-commas.ts +88 -0
- package/src/utils/route-utils.ts +214 -0
- package/src/utils/style-utils.ts +183 -1
- package/src/utils/template-expression-balance.ts +196 -0
- package/src/utils/types.ts +25 -0
- package/src/utils/url-query-writer.ts +318 -0
- package/src/utils/url-search-param-sync.ts +303 -0
- package/src/utils/url-search-params.ts +122 -0
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import {
|
|
2
|
+
parseDynamicPathSegments,
|
|
3
|
+
pathHasDynamicSegment,
|
|
4
|
+
isDynamicRoute,
|
|
5
|
+
} from '../../src/utils/route-utils'
|
|
6
|
+
|
|
7
|
+
describe('parseDynamicPathSegments', () => {
|
|
8
|
+
it('splits a Next.js bracket segment into static text plus a param name', () => {
|
|
9
|
+
// The shape every details page's `navLink` actually carries. Before this
|
|
10
|
+
// was understood, the literal text "[id]" shipped inside canonical URLs,
|
|
11
|
+
// og:url and sitemap entries.
|
|
12
|
+
expect(parseDynamicPathSegments('/event-details/[id]')).toEqual({
|
|
13
|
+
staticParts: ['/event-details/', ''],
|
|
14
|
+
paramNames: ['id'],
|
|
15
|
+
})
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
it('still splits the template-literal form', () => {
|
|
19
|
+
expect(parseDynamicPathSegments('/news/' + '$' + '{' + 'slug}')).toEqual({
|
|
20
|
+
staticParts: ['/news/', ''],
|
|
21
|
+
paramNames: ['slug'],
|
|
22
|
+
})
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
it('handles a full absolute URL, keeping the origin in the static text', () => {
|
|
26
|
+
expect(parseDynamicPathSegments('https://example.com/rsvp-event/[id]')).toEqual({
|
|
27
|
+
staticParts: ['https://example.com/rsvp-event/', ''],
|
|
28
|
+
paramNames: ['id'],
|
|
29
|
+
})
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it('splits several parameters and keeps the text between them', () => {
|
|
33
|
+
expect(parseDynamicPathSegments('/g/[guildId]/e/[eventId]/rsvp')).toEqual({
|
|
34
|
+
staticParts: ['/g/', '/e/', '/rsvp'],
|
|
35
|
+
paramNames: ['guildId', 'eventId'],
|
|
36
|
+
})
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
it('mixes both spellings in one path', () => {
|
|
40
|
+
expect(parseDynamicPathSegments('/a/[id]/b/' + '$' + '{' + 'slug}')).toEqual({
|
|
41
|
+
staticParts: ['/a/', '/b/', ''],
|
|
42
|
+
paramNames: ['id', 'slug'],
|
|
43
|
+
})
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('reports no parameters for a plain static path', () => {
|
|
47
|
+
expect(parseDynamicPathSegments('https://example.com/add-guild')).toEqual({
|
|
48
|
+
staticParts: ['https://example.com/add-guild'],
|
|
49
|
+
paramNames: [],
|
|
50
|
+
})
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it('always returns exactly one more static part than parameters', () => {
|
|
54
|
+
for (const path of ['/a', '/a/[id]', '/a/[id]/b/[other]', '/' + '$' + '{' + 'x}']) {
|
|
55
|
+
const { staticParts, paramNames } = parseDynamicPathSegments(path)
|
|
56
|
+
expect(staticParts.length).toBe(paramNames.length + 1)
|
|
57
|
+
}
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
it('rebuilding staticParts + parameters reproduces the original path', () => {
|
|
61
|
+
const path = '/g/[guildId]/e/[eventId]/rsvp'
|
|
62
|
+
const { staticParts, paramNames } = parseDynamicPathSegments(path)
|
|
63
|
+
const rebuilt = staticParts.reduce(
|
|
64
|
+
(acc, part, i) => acc + part + (i < paramNames.length ? `[${paramNames[i]}]` : ''),
|
|
65
|
+
''
|
|
66
|
+
)
|
|
67
|
+
expect(rebuilt).toBe(path)
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
it('ignores a bracket that is not a whole path segment', () => {
|
|
71
|
+
// A literal bracket inside a segment or a query string is content, not a
|
|
72
|
+
// route parameter — turning it into router.query would corrupt the URL.
|
|
73
|
+
expect(parseDynamicPathSegments('/report[2024]/summary').paramNames).toEqual([])
|
|
74
|
+
expect(parseDynamicPathSegments('/search?tags[]=a').paramNames).toEqual([])
|
|
75
|
+
expect(parseDynamicPathSegments('/a/[id]x/b').paramNames).toEqual([])
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
it('ignores Next catch-all segments, whose value is an array', () => {
|
|
79
|
+
// `router.query.slug` is a string[] for a catch-all; interpolating it into
|
|
80
|
+
// a URL would emit a comma-joined path. Leaving it alone is the honest
|
|
81
|
+
// outcome until a caller needs a real join.
|
|
82
|
+
expect(parseDynamicPathSegments('/docs/[...slug]').paramNames).toEqual([])
|
|
83
|
+
expect(parseDynamicPathSegments('/docs/[[...slug]]').paramNames).toEqual([])
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
it('ends a segment at anything that cannot be part of a path', () => {
|
|
87
|
+
// The same template appears inside a serialized JSON-LD document, where the
|
|
88
|
+
// URL ends at the closing quote rather than at a slash.
|
|
89
|
+
const embedded = '{"item":"https://example.com/event-details/[id]"}'
|
|
90
|
+
const { paramNames } = parseDynamicPathSegments(embedded)
|
|
91
|
+
expect(paramNames).toEqual(['id'])
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
it('is total for empty input', () => {
|
|
95
|
+
expect(parseDynamicPathSegments('')).toEqual({ staticParts: [''], paramNames: [] })
|
|
96
|
+
})
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
describe('pathHasDynamicSegment', () => {
|
|
100
|
+
it('is true only for a whole-segment bracket parameter', () => {
|
|
101
|
+
expect(pathHasDynamicSegment('/add-character/[id]')).toBe(true)
|
|
102
|
+
expect(pathHasDynamicSegment('https://example.com/profile/[id]')).toBe(true)
|
|
103
|
+
expect(pathHasDynamicSegment('/add-guild')).toBe(false)
|
|
104
|
+
expect(pathHasDynamicSegment('/report[2024]')).toBe(false)
|
|
105
|
+
expect(pathHasDynamicSegment('')).toBe(false)
|
|
106
|
+
expect(pathHasDynamicSegment(undefined as unknown as string)).toBe(false)
|
|
107
|
+
})
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
describe('isDynamicRoute', () => {
|
|
111
|
+
it('reads the component output options, not a URL', () => {
|
|
112
|
+
expect(
|
|
113
|
+
isDynamicRoute({ outputOptions: { folderPath: ['event-details'], fileName: '[id]' } })
|
|
114
|
+
).toBe(true)
|
|
115
|
+
expect(isDynamicRoute({ outputOptions: { folderPath: [], fileName: 'add-guild' } })).toBe(false)
|
|
116
|
+
expect(isDynamicRoute({})).toBe(false)
|
|
117
|
+
})
|
|
118
|
+
})
|
|
@@ -36,6 +36,65 @@ describe('JSS Utils ', () => {
|
|
|
36
36
|
})
|
|
37
37
|
})
|
|
38
38
|
|
|
39
|
+
it('drops a value with unbalanced parentheses (truncated color-mix)', () => {
|
|
40
|
+
const styleValue: UIDLStyleDefinitions = {
|
|
41
|
+
color: {
|
|
42
|
+
type: 'static',
|
|
43
|
+
content: 'color-mix(in srgb, var(--color-surface) 50%,',
|
|
44
|
+
},
|
|
45
|
+
display: {
|
|
46
|
+
type: 'static',
|
|
47
|
+
content: 'flex',
|
|
48
|
+
},
|
|
49
|
+
}
|
|
50
|
+
// The corrupted `color` is dropped; the surrounding rule stays parseable.
|
|
51
|
+
expect(getContentOfStyleObject(styleValue)).toEqual({ display: 'flex' })
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it('drops a value with a stray closing parenthesis', () => {
|
|
55
|
+
const styleValue: UIDLStyleDefinitions = {
|
|
56
|
+
width: {
|
|
57
|
+
type: 'static',
|
|
58
|
+
content: 'calc(100% - 10px))',
|
|
59
|
+
},
|
|
60
|
+
}
|
|
61
|
+
expect(getContentOfStyleObject(styleValue)).toEqual({})
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
it('keeps valid values with balanced nested parentheses', () => {
|
|
65
|
+
const styleValue: UIDLStyleDefinitions = {
|
|
66
|
+
color: {
|
|
67
|
+
type: 'static',
|
|
68
|
+
content: 'color-mix(in srgb, var(--color-surface) 50%, transparent)',
|
|
69
|
+
},
|
|
70
|
+
background: {
|
|
71
|
+
type: 'static',
|
|
72
|
+
content: 'linear-gradient(90deg, rgba(0,0,0,0.5), #fff)',
|
|
73
|
+
},
|
|
74
|
+
}
|
|
75
|
+
expect(getContentOfStyleObject(styleValue)).toEqual({
|
|
76
|
+
color: 'color-mix(in srgb, var(--color-surface) 50%, transparent)',
|
|
77
|
+
background: 'linear-gradient(90deg, rgba(0,0,0,0.5), #fff)',
|
|
78
|
+
})
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
it('keeps values whose parentheses/colons live inside a quoted string', () => {
|
|
82
|
+
const styleValue: UIDLStyleDefinitions = {
|
|
83
|
+
background: {
|
|
84
|
+
type: 'static',
|
|
85
|
+
content: 'url("data:image/svg+xml;utf8,<svg viewBox=\'0 0 (1)\'></svg>")',
|
|
86
|
+
},
|
|
87
|
+
fontFamily: {
|
|
88
|
+
type: 'static',
|
|
89
|
+
content: '"Foo (Bold)", sans-serif',
|
|
90
|
+
},
|
|
91
|
+
}
|
|
92
|
+
expect(getContentOfStyleObject(styleValue)).toEqual({
|
|
93
|
+
background: 'url("data:image/svg+xml;utf8,<svg viewBox=\'0 0 (1)\'></svg>")',
|
|
94
|
+
fontFamily: '"Foo (Bold)", sans-serif',
|
|
95
|
+
})
|
|
96
|
+
})
|
|
97
|
+
|
|
39
98
|
it('fails with other type than static or nested-style', () => {
|
|
40
99
|
const styleValue: UIDLStyleDefinitions = {
|
|
41
100
|
content: {
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import {
|
|
2
|
+
balanceExpression,
|
|
3
|
+
countUnclosedStaticParens,
|
|
4
|
+
isBalancedExpression,
|
|
5
|
+
scanExpression,
|
|
6
|
+
} from '../../src/utils/template-expression-balance'
|
|
7
|
+
|
|
8
|
+
describe('scanExpression', () => {
|
|
9
|
+
it('reports a clean expression as fully closed', () => {
|
|
10
|
+
const scan = scanExpression('-(cameraX || 0)')
|
|
11
|
+
expect(scan.unclosed).toEqual([])
|
|
12
|
+
expect(scan.strayCloserCount).toBe(0)
|
|
13
|
+
expect(scan.unterminatedQuote).toBeNull()
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
it('never lets a bracket inside a string affect nesting', () => {
|
|
17
|
+
expect(isBalancedExpression(`name || '(unclosed'`)).toBe(true)
|
|
18
|
+
expect(isBalancedExpression(`name || ')'`)).toBe(true)
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
it('honours escapes inside a string', () => {
|
|
22
|
+
expect(isBalancedExpression(`name || 'it\\'s fine'`)).toBe(true)
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
it('reports an unterminated quote', () => {
|
|
26
|
+
expect(scanExpression(`name || 'oops`).unterminatedQuote).toBe("'")
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it('counts a closer that never had an opener as stray', () => {
|
|
30
|
+
expect(scanExpression('0)').strayCloserCount).toBe(1)
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('treats a mismatched closer as stray rather than guessing', () => {
|
|
34
|
+
expect(scanExpression('fn(a]').strayCloserCount).toBe(1)
|
|
35
|
+
})
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
describe('balanceExpression', () => {
|
|
39
|
+
it('closes the paren the upstream repair dropped', () => {
|
|
40
|
+
expect(balanceExpression(`-(cameraX || ''`)).toBe(`-(cameraX || '')`)
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it('closes an unterminated quote BEFORE the brackets around it', () => {
|
|
44
|
+
expect(balanceExpression(`fn(x, 'label`)).toBe(`fn(x, 'label')`)
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it('closes nested brackets outermost-last', () => {
|
|
48
|
+
expect(balanceExpression('fn(items[0')).toBe('fn(items[0])')
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
it('leaves a balanced expression byte-for-byte alone', () => {
|
|
52
|
+
for (const expr of [`-(cameraX || 0)`, `a.b`, `items[0].name`, `fn(a, 'b')`]) {
|
|
53
|
+
expect(balanceExpression(expr)).toBe(expr)
|
|
54
|
+
}
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
it('refuses to guess where a missing opener belonged', () => {
|
|
58
|
+
expect(balanceExpression('cameraX)')).toBe('cameraX)')
|
|
59
|
+
})
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
describe('countUnclosedStaticParens', () => {
|
|
63
|
+
it('ignores parentheses inside an interpolation', () => {
|
|
64
|
+
// The whole point: the `(` in the expression is not a CSS function call.
|
|
65
|
+
expect(countUnclosedStaticParens(`translateX(\${-(cameraX || 0)}px)`)).toBe(0)
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
it('counts a genuinely unclosed CSS function', () => {
|
|
69
|
+
expect(countUnclosedStaticParens(`translate(\${x}px, \${y}px`)).toBe(1)
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
it('does not go negative on extra closers', () => {
|
|
73
|
+
expect(countUnclosedStaticParens(`\${x}px))`)).toBe(0)
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
it('skips a brace inside a string inside an interpolation', () => {
|
|
77
|
+
expect(countUnclosedStaticParens(`translateX(${'${'}fn('}')${'}'}px)`)).toBe(0)
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
it('tolerates an unterminated interpolation', () => {
|
|
81
|
+
expect(countUnclosedStaticParens(`translateX(\${-(cameraX`)).toBe(1)
|
|
82
|
+
})
|
|
83
|
+
})
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import * as types from '@babel/types'
|
|
2
|
+
import generator from '@babel/generator'
|
|
3
|
+
import {
|
|
4
|
+
QUERY_SYNC_REF_ID,
|
|
5
|
+
QUERY_WRITER_ID,
|
|
6
|
+
buildQuerySettledExpr,
|
|
7
|
+
buildQuerySyncDeclarations,
|
|
8
|
+
buildQueryWriteCall,
|
|
9
|
+
ensureQuerySyncDeclarations,
|
|
10
|
+
hasQuerySyncDeclarations,
|
|
11
|
+
} from '../../src/utils/url-query-writer'
|
|
12
|
+
|
|
13
|
+
const codeOf = (nodes: types.Statement[]): string =>
|
|
14
|
+
generator(types.program(nodes) as types.Node).code
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* A router that behaves like Next's.
|
|
18
|
+
*
|
|
19
|
+
* The two properties that caused the defect are reproduced faithfully:
|
|
20
|
+
* • `replace` resolves ASYNCHRONOUSLY, and `query` is only updated when it
|
|
21
|
+
* does — so a second writer running in the same tick still sees the old one;
|
|
22
|
+
* • `query` is a fresh object each time, as `makePublicRouterInstance` makes it.
|
|
23
|
+
*/
|
|
24
|
+
const makeRouter = (initialQuery: Record<string, string>) => {
|
|
25
|
+
const replaced: Array<Record<string, string>> = []
|
|
26
|
+
let resolveAll: Array<() => void> = []
|
|
27
|
+
const router = {
|
|
28
|
+
pathname: '/products',
|
|
29
|
+
query: { ...initialQuery },
|
|
30
|
+
replace(url: { query: Record<string, string> }) {
|
|
31
|
+
replaced.push({ ...url.query })
|
|
32
|
+
return new Promise<boolean>((resolve) => {
|
|
33
|
+
resolveAll.push(() => {
|
|
34
|
+
// Next applies the navigation, THEN resolves.
|
|
35
|
+
router.query = { ...url.query }
|
|
36
|
+
resolve(true)
|
|
37
|
+
})
|
|
38
|
+
})
|
|
39
|
+
},
|
|
40
|
+
}
|
|
41
|
+
const settleAll = async (): Promise<void> => {
|
|
42
|
+
while (resolveAll.length > 0) {
|
|
43
|
+
const pending = resolveAll
|
|
44
|
+
resolveAll = []
|
|
45
|
+
pending.forEach((fn) => fn())
|
|
46
|
+
await Promise.resolve()
|
|
47
|
+
await Promise.resolve()
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return { router, replaced, settleAll }
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Evaluates the emitted declarations and hands back the writer they define. */
|
|
54
|
+
const instantiate = (router: unknown) => {
|
|
55
|
+
const code = codeOf(buildQuerySyncDeclarations())
|
|
56
|
+
// tslint:disable-next-line:function-constructor
|
|
57
|
+
const factory = new Function(
|
|
58
|
+
'useRef',
|
|
59
|
+
'router',
|
|
60
|
+
`${code}\nreturn { ref: ${QUERY_SYNC_REF_ID}, write: ${QUERY_WRITER_ID} }`
|
|
61
|
+
)
|
|
62
|
+
// tslint:disable-next-line:no-any
|
|
63
|
+
const useRef = (initial: any) => ({ current: initial })
|
|
64
|
+
return factory(useRef, router) as {
|
|
65
|
+
ref: { current: { pending: Record<string, string> | null; inFlight: number } }
|
|
66
|
+
// tslint:disable-next-line:no-any
|
|
67
|
+
write: (key: string, value: any) => void
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
describe('URLQueryWriter — emitted shape', () => {
|
|
72
|
+
it('merges into the pending query, not into router.query', () => {
|
|
73
|
+
// Babel prints objects multi-line; the assertions read better flattened.
|
|
74
|
+
const code = codeOf(buildQuerySyncDeclarations()).replace(/\s+/g, ' ')
|
|
75
|
+
|
|
76
|
+
expect(code).toContain('const base = __tqQuerySyncRef.current.pending || router.query')
|
|
77
|
+
expect(code).toContain('const next = { ...base }')
|
|
78
|
+
// The only bail: this key already holds the value we would write.
|
|
79
|
+
expect(code).toContain('if (next[key] === base[key]) return')
|
|
80
|
+
expect(code).toContain('__tqQuerySyncRef.current.pending = next')
|
|
81
|
+
expect(code).toContain('__tqQuerySyncRef.current.inFlight += 1')
|
|
82
|
+
// Released once every replace it was built from has settled, so an external
|
|
83
|
+
// navigation is never merged into a stale snapshot.
|
|
84
|
+
expect(code).toContain('__tqQuerySyncRef.current.pending = null')
|
|
85
|
+
expect(code).toContain('shallow: true')
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
it('builds a call and a settled-check callers can compose', () => {
|
|
89
|
+
expect(generator(buildQueryWriteCall('page', types.identifier('__statePage'))).code).toBe(
|
|
90
|
+
'__tqWriteQueryParam("page", __statePage);'
|
|
91
|
+
)
|
|
92
|
+
expect(generator(buildQuerySettledExpr()).code).toBe('__tqQuerySyncRef.current.inFlight === 0')
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
it('declares itself once, after `const router = useRouter()`', () => {
|
|
96
|
+
const routerDecl = types.variableDeclaration('const', [
|
|
97
|
+
types.variableDeclarator(
|
|
98
|
+
types.identifier('router'),
|
|
99
|
+
types.callExpression(types.identifier('useRouter'), [])
|
|
100
|
+
),
|
|
101
|
+
])
|
|
102
|
+
const body: types.Statement[] = [routerDecl, types.returnStatement(types.nullLiteral())]
|
|
103
|
+
|
|
104
|
+
expect(hasQuerySyncDeclarations(body)).toBe(false)
|
|
105
|
+
expect(ensureQuerySyncDeclarations(body)).toBe(true)
|
|
106
|
+
expect(body.length).toBe(4)
|
|
107
|
+
expect(codeOf(body).indexOf('useRouter')).toBeLessThan(codeOf(body).indexOf(QUERY_SYNC_REF_ID))
|
|
108
|
+
|
|
109
|
+
// A second plugin needing the same pair must not emit a duplicate hook —
|
|
110
|
+
// that would also shift the hook order between renders.
|
|
111
|
+
expect(ensureQuerySyncDeclarations(body)).toBe(false)
|
|
112
|
+
expect(body.length).toBe(4)
|
|
113
|
+
})
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* ⛔ These run the generated code. Every other guard in this repo is a
|
|
118
|
+
* source-text assertion, and effect INTERLEAVING is exactly what source text
|
|
119
|
+
* cannot see — which is why the defect below shipped green.
|
|
120
|
+
*/
|
|
121
|
+
describe('URLQueryWriter — behaviour under a Next-like async router', () => {
|
|
122
|
+
it('keeps both keys when two controls write in the same flush', async () => {
|
|
123
|
+
// The live repro: on `?page=5`, picking a category writes the filter AND
|
|
124
|
+
// resets the page, so both writers run before either replace has landed.
|
|
125
|
+
const { router, replaced, settleAll } = makeRouter({ page: '5' })
|
|
126
|
+
const { write } = instantiate(router)
|
|
127
|
+
|
|
128
|
+
write('categoryFilter', 'Rings')
|
|
129
|
+
write('page', undefined) // page 1 ⇒ remove the key
|
|
130
|
+
|
|
131
|
+
expect(replaced).toHaveLength(2)
|
|
132
|
+
// The SECOND write built on the first's payload rather than on the stale
|
|
133
|
+
// `router.query` — this is the whole fix.
|
|
134
|
+
expect(replaced[1]).toEqual({ categoryFilter: 'Rings' })
|
|
135
|
+
|
|
136
|
+
await settleAll()
|
|
137
|
+
expect(router.query).toEqual({ categoryFilter: 'Rings' })
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
it('does not resurrect a key another writer removed in the same flush', async () => {
|
|
141
|
+
// The mirror image: clearing the search box on page 3 removes
|
|
142
|
+
// `?searchKeyword` and resets the page. A stale base would have put the
|
|
143
|
+
// search term the visitor just deleted straight back into the URL.
|
|
144
|
+
const { router, replaced, settleAll } = makeRouter({ page: '3', searchKeyword: 'shoes' })
|
|
145
|
+
const { write } = instantiate(router)
|
|
146
|
+
|
|
147
|
+
write('searchKeyword', '')
|
|
148
|
+
write('page', undefined)
|
|
149
|
+
|
|
150
|
+
expect(replaced[1]).toEqual({})
|
|
151
|
+
await settleAll()
|
|
152
|
+
expect(router.query).toEqual({})
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
it('settles, and never oscillates when both sides are re-asserted', async () => {
|
|
156
|
+
// Re-running every writer after the URL lands — which is what a re-render
|
|
157
|
+
// does — must produce NO further navigation. An extra replace here is the
|
|
158
|
+
// first step of the infinite loop.
|
|
159
|
+
const { router, replaced, settleAll } = makeRouter({ page: '5' })
|
|
160
|
+
const { write } = instantiate(router)
|
|
161
|
+
|
|
162
|
+
write('categoryFilter', 'Rings')
|
|
163
|
+
write('page', undefined)
|
|
164
|
+
await settleAll()
|
|
165
|
+
|
|
166
|
+
const settledCount = replaced.length
|
|
167
|
+
write('categoryFilter', 'Rings')
|
|
168
|
+
write('page', undefined)
|
|
169
|
+
expect(replaced).toHaveLength(settledCount)
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
it('bails without a replace when the key already holds the value', () => {
|
|
173
|
+
const { router, replaced } = makeRouter({ sortBy: 'price-asc' })
|
|
174
|
+
const { write } = instantiate(router)
|
|
175
|
+
|
|
176
|
+
write('sortBy', 'price-asc')
|
|
177
|
+
expect(replaced).toHaveLength(0)
|
|
178
|
+
|
|
179
|
+
// ...and removing an absent key is equally a no-op.
|
|
180
|
+
write('missing', undefined)
|
|
181
|
+
expect(replaced).toHaveLength(0)
|
|
182
|
+
})
|
|
183
|
+
|
|
184
|
+
it('coerces to a string, so a numeric page and a string filter round-trip alike', () => {
|
|
185
|
+
const { router, replaced } = makeRouter({})
|
|
186
|
+
const { write } = instantiate(router)
|
|
187
|
+
|
|
188
|
+
write('page', 3)
|
|
189
|
+
expect(replaced[0]).toEqual({ page: '3' })
|
|
190
|
+
})
|
|
191
|
+
|
|
192
|
+
it('reports settled only once every replace it issued has landed', async () => {
|
|
193
|
+
const { router, settleAll } = makeRouter({ page: '5' })
|
|
194
|
+
const { ref, write } = instantiate(router)
|
|
195
|
+
|
|
196
|
+
expect(ref.current.inFlight).toBe(0)
|
|
197
|
+
write('categoryFilter', 'Rings')
|
|
198
|
+
write('page', undefined)
|
|
199
|
+
// The page ⇄ URL effect reads this to know that `router.query` is a value
|
|
200
|
+
// the page is on its way OUT of, and must not be adopted.
|
|
201
|
+
expect(ref.current.inFlight).toBe(2)
|
|
202
|
+
|
|
203
|
+
await settleAll()
|
|
204
|
+
expect(ref.current.inFlight).toBe(0)
|
|
205
|
+
expect(ref.current.pending).toBeNull()
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
it('releases the pending query even when a navigation is rejected', async () => {
|
|
209
|
+
// A cancelled route must not freeze every later write against a stale base.
|
|
210
|
+
const router = {
|
|
211
|
+
pathname: '/products',
|
|
212
|
+
query: { page: '5' } as Record<string, string>,
|
|
213
|
+
replace: () => Promise.reject(new Error('route cancelled')),
|
|
214
|
+
}
|
|
215
|
+
const { ref, write } = instantiate(router)
|
|
216
|
+
|
|
217
|
+
write('categoryFilter', 'Rings')
|
|
218
|
+
await Promise.resolve()
|
|
219
|
+
await Promise.resolve()
|
|
220
|
+
|
|
221
|
+
expect(ref.current.inFlight).toBe(0)
|
|
222
|
+
expect(ref.current.pending).toBeNull()
|
|
223
|
+
})
|
|
224
|
+
|
|
225
|
+
it('picks up an external navigation once nothing is in flight', async () => {
|
|
226
|
+
// Browser back/forward moves the URL without asking us. The next write must
|
|
227
|
+
// build on where the browser actually is, not on our last request.
|
|
228
|
+
const { router, replaced, settleAll } = makeRouter({ page: '5' })
|
|
229
|
+
const { write } = instantiate(router)
|
|
230
|
+
|
|
231
|
+
write('categoryFilter', 'Rings')
|
|
232
|
+
await settleAll()
|
|
233
|
+
|
|
234
|
+
router.query = { locale: 'fr' } // an external navigation
|
|
235
|
+
write('page', 2)
|
|
236
|
+
|
|
237
|
+
expect(replaced[replaced.length - 1]).toEqual({ locale: 'fr', page: '2' })
|
|
238
|
+
})
|
|
239
|
+
})
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import * as types from '@babel/types'
|
|
2
|
+
import generator from '@babel/generator'
|
|
3
|
+
import {
|
|
4
|
+
buildUrlWriteBackEffect,
|
|
5
|
+
buildUrlReadBackEffect,
|
|
6
|
+
} from '../../src/utils/url-search-param-sync'
|
|
7
|
+
|
|
8
|
+
// NOTE: these assertions check the RAW `@babel/generator` output (double
|
|
9
|
+
// quotes, semicolons, no prettier), not the prettified code that lands in a
|
|
10
|
+
// generated project — so string literals appear as `""`, not `''`.
|
|
11
|
+
const codeOf = (node: types.Node): string => generator(node).code
|
|
12
|
+
|
|
13
|
+
describe('URLSearchParamSync.buildUrlWriteBackEffect', () => {
|
|
14
|
+
it('delegates the write to the shared query writer (dropdown case)', () => {
|
|
15
|
+
const effect = buildUrlWriteBackEffect(
|
|
16
|
+
'categoryFilter',
|
|
17
|
+
types.identifier('selectedCategory'),
|
|
18
|
+
types.identifier('selectedCategory')
|
|
19
|
+
)
|
|
20
|
+
const code = codeOf(effect)
|
|
21
|
+
|
|
22
|
+
// SSR + router-ready guards.
|
|
23
|
+
expect(code).toContain('if (typeof window === "undefined") return')
|
|
24
|
+
expect(code).toContain('if (!router.isReady) return')
|
|
25
|
+
|
|
26
|
+
// ⛔ The effect must NOT build the next query itself. Spreading
|
|
27
|
+
// `router.query` here is what let two controls writing in one flush erase
|
|
28
|
+
// each other's keys — and, through their read-backs, oscillate forever.
|
|
29
|
+
expect(code).not.toContain('__nextQuery')
|
|
30
|
+
expect(code).not.toContain('...router.query')
|
|
31
|
+
expect(code).not.toContain('router.replace')
|
|
32
|
+
|
|
33
|
+
// One key, one call. `undefined` is the writer's "remove this key".
|
|
34
|
+
expect(code).toContain(
|
|
35
|
+
'__tqWriteQueryParam("categoryFilter", selectedCategory === "" || selectedCategory == null ? undefined : selectedCategory)'
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
// ⛔ `router.query` is deliberately NOT a dependency. It was added once as a
|
|
39
|
+
// way to RE-ASSERT a key another writer had dropped, which is precisely the
|
|
40
|
+
// edge that closed the loop; with clobbering fixed at its source there is
|
|
41
|
+
// nothing to re-assert.
|
|
42
|
+
expect(code).toContain('}, [selectedCategory, router.isReady])')
|
|
43
|
+
expect(code).not.toContain('router.isReady, router.query]')
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('emits the write-back reading a member expression (search debounced-query case)', () => {
|
|
47
|
+
const debounced = () =>
|
|
48
|
+
types.memberExpression(types.identifier('ds_0_state'), types.identifier('debouncedQuery'))
|
|
49
|
+
const effect = buildUrlWriteBackEffect('searchKeyword', debounced(), debounced())
|
|
50
|
+
const code = codeOf(effect)
|
|
51
|
+
|
|
52
|
+
// The value pushed to the URL is read from the combined state object,
|
|
53
|
+
// not a bare identifier — this is the generalization the search input needs.
|
|
54
|
+
expect(code).toContain(
|
|
55
|
+
'__tqWriteQueryParam("searchKeyword", ds_0_state.debouncedQuery === "" || ds_0_state.debouncedQuery == null ? undefined : ds_0_state.debouncedQuery)'
|
|
56
|
+
)
|
|
57
|
+
// Dependency is the debounced member expression, so the URL only updates
|
|
58
|
+
// after the debounce commits — never on every keystroke.
|
|
59
|
+
expect(code).toContain('}, [ds_0_state.debouncedQuery, router.isReady])')
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
it('passes a non-identifier URL key through as a plain string argument', () => {
|
|
63
|
+
const effect = buildUrlWriteBackEffect(
|
|
64
|
+
'search-keyword',
|
|
65
|
+
types.identifier('q'),
|
|
66
|
+
types.identifier('q')
|
|
67
|
+
)
|
|
68
|
+
const code = codeOf(effect)
|
|
69
|
+
|
|
70
|
+
// The writer indexes with `next[key]`, so an awkward key needs no bracket
|
|
71
|
+
// gymnastics at the call site any more.
|
|
72
|
+
expect(code).toContain('__tqWriteQueryParam("search-keyword"')
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
it('also removes the key when the value equals a non-empty default (clean canonical URL)', () => {
|
|
76
|
+
const effect = buildUrlWriteBackEffect(
|
|
77
|
+
'sortBy',
|
|
78
|
+
types.identifier('sortBy'),
|
|
79
|
+
types.identifier('sortBy'),
|
|
80
|
+
types.stringLiteral('name-asc')
|
|
81
|
+
)
|
|
82
|
+
const code = codeOf(effect)
|
|
83
|
+
|
|
84
|
+
// The default routes to the remove branch, so loading the page at its
|
|
85
|
+
// default sort never writes a sticky `?sortBy=name-asc`.
|
|
86
|
+
expect(code).toContain('sortBy === "" || sortBy == null || sortBy === "name-asc" ? undefined')
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
it('omits the default clause when no default is supplied', () => {
|
|
90
|
+
const withoutDefault = codeOf(
|
|
91
|
+
buildUrlWriteBackEffect('sortBy', types.identifier('sortBy'), types.identifier('sortBy'))
|
|
92
|
+
)
|
|
93
|
+
expect(withoutDefault).toContain('sortBy === "" || sortBy == null ? undefined : sortBy')
|
|
94
|
+
expect(withoutDefault).not.toContain('=== "name-asc"')
|
|
95
|
+
})
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
describe('URLSearchParamSync.buildUrlReadBackEffect', () => {
|
|
99
|
+
it('emits a loop-free URL→state read-back via functional setState', () => {
|
|
100
|
+
const effect = buildUrlReadBackEffect('searchKeyword', 'setDs_0_searchQuery')
|
|
101
|
+
const code = codeOf(effect)
|
|
102
|
+
|
|
103
|
+
expect(code).toContain('if (!router.isReady) return')
|
|
104
|
+
expect(code).toContain('const __urlValue = router.query.searchKeyword')
|
|
105
|
+
// Normalizes string | string[] | undefined to a string.
|
|
106
|
+
expect(code).toContain('typeof __urlValue === "string"')
|
|
107
|
+
expect(code).toContain('Array.isArray(__urlValue)')
|
|
108
|
+
// Functional setState bail-out — the loop breaker on the state side.
|
|
109
|
+
expect(code).toContain('setDs_0_searchQuery(prev => prev === __nextValue ? prev : __nextValue)')
|
|
110
|
+
expect(code).toContain('}, [router.query.searchKeyword, router.isReady])')
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
it('uses bracket notation in the deps + read for non-identifier keys', () => {
|
|
114
|
+
const effect = buildUrlReadBackEffect('search-keyword', 'setQ')
|
|
115
|
+
const code = codeOf(effect)
|
|
116
|
+
|
|
117
|
+
expect(code).toContain('const __urlValue = router.query["search-keyword"]')
|
|
118
|
+
expect(code).toContain('}, [router.query["search-keyword"], router.isReady])')
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
it('resolves an absent/empty URL value to a non-empty default rather than ""', () => {
|
|
122
|
+
const effect = buildUrlReadBackEffect('sortBy', 'setSortBy', types.stringLiteral('name-asc'))
|
|
123
|
+
const code = codeOf(effect)
|
|
124
|
+
|
|
125
|
+
// The normalized (string | string[] | undefined) value falls through to the
|
|
126
|
+
// default when empty — this is what stops the default from being clobbered
|
|
127
|
+
// to "" on first load (which forced an extra unsorted fetch).
|
|
128
|
+
expect(code).toContain('|| "name-asc"')
|
|
129
|
+
expect(code).toContain('setSortBy(prev => prev === __nextValue ? prev : __nextValue)')
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
it('emits no default fallback when none is supplied (byte-identical fallback)', () => {
|
|
133
|
+
const code = codeOf(buildUrlReadBackEffect('sortBy', 'setSortBy'))
|
|
134
|
+
expect(code).not.toContain('name-asc')
|
|
135
|
+
// The bare normalized expression still ends in the "" missing-key fallback.
|
|
136
|
+
expect(code).toContain('const __nextValue =')
|
|
137
|
+
})
|
|
138
|
+
})
|