1
+ import { fixture , assert , expect } from '@open-wc/testing' ;
2
+ import { FlowStoreElement } from '../src/store/FlowStoreElement' ;
3
+ import { getComponent , mockAPI , mockGET } from './utils.test' ;
4
+ import { FlowDetails } from '../src/interfaces' ;
5
+
6
+ describe ( 'FlowStoreElement' , ( ) => {
7
+ beforeEach ( ( ) => {
8
+ mockAPI ( ) ;
9
+ } ) ;
10
+
11
+ it ( 'can be created' , async ( ) => {
12
+ const element = await getComponent ( 'temba-flow-details' ) as FlowStoreElement ;
13
+ assert . instanceOf ( element , FlowStoreElement ) ;
14
+ } ) ;
15
+
16
+ it ( 'initializes with default properties' , async ( ) => {
17
+ const element = await getComponent ( 'temba-flow-details' ) as FlowStoreElement ;
18
+
19
+ expect ( element . flow ) . to . be . undefined ;
20
+ expect ( element . data ) . to . be . undefined ;
21
+ expect ( element . endpoint ) . to . equal ( '/api/v2/flows.json?uuid=' ) ;
22
+ // url might be undefined initially
23
+ expect ( element . url == null ) . to . be . true ;
24
+ } ) ;
25
+
26
+ it ( 'sets properties correctly' , async ( ) => {
27
+ const element = await getComponent ( 'temba-flow-details' , {
28
+ flow : 'test-flow-uuid' ,
29
+ endpoint : '/custom/flows.json?uuid='
30
+ } ) as FlowStoreElement ;
31
+
32
+ expect ( element . flow ) . to . equal ( 'test-flow-uuid' ) ;
33
+ expect ( element . endpoint ) . to . equal ( '/custom/flows.json?uuid=' ) ;
34
+ } ) ;
35
+
36
+ describe ( 'updated method' , ( ) => {
37
+ it ( 'sets url when flow is provided' , async ( ) => {
38
+ const element = await getComponent ( 'temba-flow-details' ) as FlowStoreElement ;
39
+
40
+ element . flow = 'test-flow-123' ;
41
+ await element . updateComplete ;
42
+
43
+ expect ( element . url ) . to . equal ( '/api/v2/flows.json?uuid=test-flow-123' ) ;
44
+ } ) ;
45
+
46
+ it ( 'sets url to null when flow is empty' , async ( ) => {
47
+ const element = await getComponent ( 'temba-flow-details' , {
48
+ flow : 'initial-flow'
49
+ } ) as FlowStoreElement ;
50
+
51
+ // Verify initial state
52
+ expect ( element . url ) . to . equal ( '/api/v2/flows.json?uuid=initial-flow' ) ;
53
+
54
+ // Clear the flow
55
+ element . flow = '' ;
56
+ await element . updateComplete ;
57
+
58
+ expect ( element . url ) . to . be . null ;
59
+ } ) ;
60
+
61
+ it ( 'sets url to null when flow is null' , async ( ) => {
62
+ const element = await getComponent ( 'temba-flow-details' , {
63
+ flow : 'initial-flow'
64
+ } ) as FlowStoreElement ;
65
+
66
+ // Clear the flow to null
67
+ element . flow = null ;
68
+ await element . updateComplete ;
69
+
70
+ expect ( element . url ) . to . be . null ;
71
+ } ) ;
72
+
73
+ it ( 'updates url when flow changes' , async ( ) => {
74
+ const element = await getComponent ( 'temba-flow-details' , {
75
+ flow : 'flow-1'
76
+ } ) as FlowStoreElement ;
77
+
78
+ expect ( element . url ) . to . equal ( '/api/v2/flows.json?uuid=flow-1' ) ;
79
+
80
+ element . flow = 'flow-2' ;
81
+ await element . updateComplete ;
82
+
83
+ expect ( element . url ) . to . equal ( '/api/v2/flows.json?uuid=flow-2' ) ;
84
+ } ) ;
85
+
86
+ it ( 'uses custom endpoint when provided' , async ( ) => {
87
+ const element = await getComponent ( 'temba-flow-details' , {
88
+ endpoint : '/custom/endpoint?id=' ,
89
+ flow : 'test-flow'
90
+ } ) as FlowStoreElement ;
91
+
92
+ expect ( element . url ) . to . equal ( '/custom/endpoint?id=test-flow' ) ;
93
+ } ) ;
94
+ } ) ;
95
+
96
+ describe ( 'prepareData method' , ( ) => {
97
+ it ( 'returns first item from array if data has length' , async ( ) => {
98
+ const element = await getComponent ( 'temba-flow-details' ) as FlowStoreElement ;
99
+
100
+ const inputData = [
101
+ { uuid : 'flow-1' , name : 'Test Flow 1' } ,
102
+ { uuid : 'flow-2' , name : 'Test Flow 2' }
103
+ ] ;
104
+
105
+ const result = element . prepareData ( inputData ) ;
106
+
107
+ expect ( result ) . to . deep . equal ( { uuid : 'flow-1' , name : 'Test Flow 1' } ) ;
108
+ } ) ;
109
+
110
+ it ( 'returns data as-is if not an array' , async ( ) => {
111
+ const element = await getComponent ( 'temba-flow-details' ) as FlowStoreElement ;
112
+
113
+ const inputData = { uuid : 'flow-1' , name : 'Test Flow' } ;
114
+
115
+ const result = element . prepareData ( inputData ) ;
116
+
117
+ expect ( result ) . to . deep . equal ( inputData ) ;
118
+ } ) ;
119
+
120
+ it ( 'returns data as-is if array is empty' , async ( ) => {
121
+ const element = await getComponent ( 'temba-flow-details' ) as FlowStoreElement ;
122
+
123
+ const inputData = [ ] ;
124
+
125
+ const result = element . prepareData ( inputData ) ;
126
+
127
+ expect ( result ) . to . deep . equal ( [ ] ) ;
128
+ } ) ;
129
+
130
+ it ( 'returns null if data is null' , async ( ) => {
131
+ const element = await getComponent ( 'temba-flow-details' ) as FlowStoreElement ;
132
+
133
+ const result = element . prepareData ( null ) ;
134
+
135
+ expect ( result ) . to . be . null ;
136
+ } ) ;
137
+
138
+ it ( 'returns undefined if data is undefined' , async ( ) => {
139
+ const element = await getComponent ( 'temba-flow-details' ) as FlowStoreElement ;
140
+
141
+ const result = element . prepareData ( undefined ) ;
142
+
143
+ expect ( result ) . to . be . undefined ;
144
+ } ) ;
145
+ } ) ;
146
+
147
+ describe ( 'render method' , ( ) => {
148
+ it ( 'returns undefined when no data is available' , async ( ) => {
149
+ const element = await getComponent ( 'temba-flow-details' ) as FlowStoreElement ;
150
+
151
+ const result = element . render ( ) ;
152
+
153
+ expect ( result ) . to . be . undefined ;
154
+ } ) ;
155
+
156
+ it ( 'renders div when data is available' , async ( ) => {
157
+ const element = await getComponent ( 'temba-flow-details' ) as FlowStoreElement ;
158
+
159
+ element . data = { uuid : 'test-flow' , name : 'Test Flow' } as FlowDetails ;
160
+ await element . updateComplete ;
161
+
162
+ const result = element . render ( ) ;
163
+
164
+ expect ( result ) . to . exist ;
165
+ } ) ;
166
+
167
+ it ( 'renders empty div in DOM when data is set' , async ( ) => {
168
+ const element = await getComponent ( 'temba-flow-details' ) as FlowStoreElement ;
169
+
170
+ element . data = { uuid : 'test-flow' , name : 'Test Flow' } as FlowDetails ;
171
+ await element . updateComplete ;
172
+
173
+ const div = element . shadowRoot ?. querySelector ( 'div' ) ;
174
+ expect ( div ) . to . exist ;
175
+ } ) ;
176
+ } ) ;
177
+
178
+ describe ( 'integration tests' , ( ) => {
179
+ it ( 'handles flow property change workflow without triggering store' , async ( ) => {
180
+ const element = await getComponent ( 'temba-flow-details' ) as FlowStoreElement ;
181
+
182
+ // Initially no flow or data
183
+ expect ( element . flow ) . to . be . undefined ;
184
+ expect ( element . data ) . to . be . undefined ;
185
+ // url might be undefined initially
186
+ expect ( element . url == null ) . to . be . true ;
187
+
188
+ // Simulate data being set directly (without triggering store)
189
+ const mockFlowData = { uuid : 'integration-test-flow' , name : 'Integration Test Flow' } ;
190
+ element . data = mockFlowData as FlowDetails ;
191
+ await element . updateComplete ;
192
+
193
+ // Should be able to render
194
+ const result = element . render ( ) ;
195
+ expect ( result ) . to . exist ;
196
+ } ) ;
197
+ } ) ;
198
+ } ) ;
0 commit comments