@@ -4,12 +4,14 @@ import { transformHoistInlineDirective } from "./hoist";
44import { debugSourceMap } from "./test-utils" ;
55
66describe ( transformHoistInlineDirective , ( ) => {
7- async function testTransform ( input : string ) {
7+ async function testTransform ( input : string , options ?: { encode ?: boolean } ) {
88 const ast = await parseAstAsync ( input ) ;
99 const { output } = transformHoistInlineDirective ( input , ast , {
1010 runtime : ( value , name ) =>
1111 `$$register(${ value } , "<id>", ${ JSON . stringify ( name ) } )` ,
1212 directive : "use server" ,
13+ encode : options ?. encode ? ( v ) => `__enc(${ v } )` : undefined ,
14+ decode : options ?. encode ? ( v ) => `__dec(${ v } )` : undefined ,
1315 } ) ;
1416 if ( ! output . hasChanged ( ) ) {
1517 return ;
@@ -84,6 +86,11 @@ export default function w() {
8486 /* #__PURE__ */ Object.defineProperty($$hoist_2_w, "name", { value: "w" });
8587 "
8688 ` ) ;
89+
90+ // nothing to encode
91+ expect ( await testTransform ( input , { encode : true } ) ) . toBe (
92+ await testTransform ( input ) ,
93+ ) ;
8794 } ) ;
8895
8996 it ( "closure" , async ( ) => {
@@ -207,6 +214,28 @@ function Counter() {
207214 /* #__PURE__ */ Object.defineProperty($$hoist_0_anonymous_server_function, "name", { value: "anonymous_server_function" });
208215 "
209216 ` ) ;
217+
218+ expect ( await testTransform ( input , { encode : true } ) ) . toMatchInlineSnapshot ( `
219+ "
220+ let count = 0;
221+
222+ function Counter() {
223+ const name = "value";
224+
225+ return {
226+ type: "form",
227+ action: /* #__PURE__ */ $$register($$hoist_0_anonymous_server_function, "<id>", "$$hoist_0_anonymous_server_function").bind(null, __enc([name]))
228+ }
229+ }
230+
231+ ;export function $$hoist_0_anonymous_server_function($$hoist_encoded, formData) {
232+ const [name] = __dec($$hoist_encoded);
233+ "use server";
234+ count += Number(formData.get(name));
235+ };
236+ /* #__PURE__ */ Object.defineProperty($$hoist_0_anonymous_server_function, "name", { value: "anonymous_server_function" });
237+ "
238+ ` ) ;
210239 } ) ;
211240
212241 it ( "higher order" , async ( ) => {
@@ -252,5 +281,32 @@ function validator(action) {
252281 /* #__PURE__ */ Object.defineProperty($$hoist_1_anonymous_server_function, "name", { value: "anonymous_server_function" });
253282 "
254283 ` ) ;
284+
285+ expect ( await testTransform ( input , { encode : true } ) ) . toMatchInlineSnapshot ( `
286+ "
287+ export default function Page() {
288+ const x = 0;
289+ const action = validator(/* #__PURE__ */ $$register($$hoist_0_anonymous_server_function, "<id>", "$$hoist_0_anonymous_server_function").bind(null, __enc([x])))
290+ }
291+
292+ function validator(action) {
293+ return /* #__PURE__ */ $$register($$hoist_1_anonymous_server_function, "<id>", "$$hoist_1_anonymous_server_function").bind(null, __enc([action]));
294+ }
295+
296+ ;export async function $$hoist_0_anonymous_server_function($$hoist_encoded, y) {
297+ const [x] = __dec($$hoist_encoded);
298+ "use server";
299+ return x + y;
300+ };
301+ /* #__PURE__ */ Object.defineProperty($$hoist_0_anonymous_server_function, "name", { value: "anonymous_server_function" });
302+
303+ ;export async function $$hoist_1_anonymous_server_function($$hoist_encoded, arg) {
304+ const [action] = __dec($$hoist_encoded);
305+ "use server";
306+ return action(arg);
307+ };
308+ /* #__PURE__ */ Object.defineProperty($$hoist_1_anonymous_server_function, "name", { value: "anonymous_server_function" });
309+ "
310+ ` ) ;
255311 } ) ;
256312} ) ;
0 commit comments