@teleporthq/teleport-project-generator-next 0.45.14 → 0.45.16

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.
@@ -0,0 +1,124 @@
1
+ import * as types from '@babel/types'
2
+ import generate from '@babel/generator'
3
+ import { ChunkType, ComponentStructure } from '@teleporthq/teleport-types'
4
+ import { elementNode } from '@teleporthq/teleport-uidl-builders'
5
+ import generateJSXSyntax from '../../teleport-plugin-common/src/node-handlers/node-to-jsx'
6
+ import {
7
+ JSXGenerationOptions,
8
+ JSXGenerationParams,
9
+ } from '../../teleport-plugin-common/src/node-handlers/node-to-jsx/types'
10
+ import { createNextGlobalStateComponentPlugin } from '../src/global-state/component-plugin'
11
+
12
+ // A rendering condition can read a global state on either side. Whichever side
13
+ // reads it, the component has to take that state out of `useGlobalState()`, or
14
+ // the condition names a variable the file never declares.
15
+
16
+ const SELECTED_ID = 'f3a1c2d4-selected'
17
+ const PROFILE_ID = 'b7e9d0a2-profile'
18
+
19
+ const globalStateDefinitions = {
20
+ [SELECTED_ID]: { id: SELECTED_ID, name: 'selectedRow', type: 'string', defaultValue: '' },
21
+ [PROFILE_ID]: { id: PROFILE_ID, name: 'profile', type: 'object', defaultValue: {} },
22
+ } as unknown as JSXGenerationParams['globalStateDefinitions']
23
+
24
+ const globalState = (id: string, refPath?: string[]) => ({
25
+ type: 'dynamic',
26
+ content: { referenceType: 'globalState', id, ...(refPath ? { refPath } : {}) },
27
+ })
28
+
29
+ const options: JSXGenerationOptions = {
30
+ dynamicReferencePrefixMap: { prop: 'props', state: '', local: '' },
31
+ dependencyHandling: 'import',
32
+ stateHandling: 'hooks',
33
+ slotHandling: 'props',
34
+ localIdentifier: 'row',
35
+ }
36
+
37
+ const generateComponent = async (renderingConditions: unknown): Promise<string> => {
38
+ const conditionalChild = elementNode('container', {}, [])
39
+ conditionalChild.content.renderingConditions = renderingConditions as never
40
+
41
+ const params: JSXGenerationParams = {
42
+ propDefinitions: {},
43
+ stateDefinitions: {},
44
+ globalStateDefinitions,
45
+ nodesLookup: {},
46
+ dependencies: {},
47
+ windowImports: {},
48
+ localeReferences: [],
49
+ localeAttributeReferences: [],
50
+ globalReferences: [],
51
+ globalStateReferences: [],
52
+ hoistedConstants: [],
53
+ }
54
+ const jsx = generateJSXSyntax(elementNode('container', {}, [conditionalChild]), params, options)
55
+
56
+ const component = types.variableDeclaration('const', [
57
+ types.variableDeclarator(
58
+ types.identifier('Rows'),
59
+ types.arrowFunctionExpression(
60
+ [types.identifier('props')],
61
+ types.blockStatement([types.returnStatement(jsx)])
62
+ )
63
+ ),
64
+ ])
65
+ const structure = {
66
+ uidl: { name: 'Rows', node: { type: 'element', content: {} } },
67
+ options: {},
68
+ dependencies: {},
69
+ chunks: [
70
+ {
71
+ name: 'jsx-component',
72
+ type: ChunkType.AST,
73
+ fileType: 'js',
74
+ content: component,
75
+ linkAfter: [],
76
+ meta: { globalStateReferences: params.globalStateReferences },
77
+ },
78
+ ],
79
+ } as unknown as ComponentStructure
80
+
81
+ const result = await createNextGlobalStateComponentPlugin()(structure)
82
+ return generate(result.chunks[0].content as types.Node).code
83
+ }
84
+
85
+ const listItemField = { type: 'dynamic', content: { referenceType: 'local', refPath: ['id'] } }
86
+
87
+ describe('a rendering condition that reads a global state on its right side', () => {
88
+ it('takes the state out of useGlobalState() and compares against it by name', async () => {
89
+ const code = await generateComponent({
90
+ reference: listItemField,
91
+ condition: { conditions: [{ operation: '===', operand: globalState(SELECTED_ID) }] },
92
+ })
93
+
94
+ expect(code).toContain('selectedRow,')
95
+ expect(code).toContain('} = useGlobalState();')
96
+ expect(code).toContain('row.id === selectedRow &&')
97
+ expect(code).not.toContain(SELECTED_ID)
98
+ })
99
+
100
+ it('takes an object state read through a field path', async () => {
101
+ const code = await generateComponent({
102
+ reference: listItemField,
103
+ condition: {
104
+ conditions: [{ operation: '===', operand: globalState(PROFILE_ID, ['team', 'id']) }],
105
+ },
106
+ })
107
+
108
+ expect(code).toMatch(/\{\s*profile,\s*setProfile\s*\} = useGlobalState\(\);/)
109
+ expect(code).toContain('row.id === profile?.team?.id &&')
110
+ expect(code).not.toContain(PROFILE_ID)
111
+ })
112
+
113
+ it('takes both states of a global state compared against a global state', async () => {
114
+ const code = await generateComponent({
115
+ reference: globalState(SELECTED_ID),
116
+ condition: { conditions: [{ operation: '===', operand: globalState(PROFILE_ID, ['id']) }] },
117
+ })
118
+
119
+ expect(code).toMatch(
120
+ /\{\s*selectedRow,\s*setSelectedRow,\s*profile,\s*setProfile\s*\} = useGlobalState\(\);/
121
+ )
122
+ expect(code).toContain('selectedRow === profile?.id &&')
123
+ })
124
+ })