From aebfa5997b5bacd41195722b6780848b290f95b2 Mon Sep 17 00:00:00 2001 From: RheeseyB <1044774+Rheeseyb@users.noreply.github.com> Date: Tue, 9 Jan 2024 15:23:20 +0000 Subject: [PATCH] chore(tests) Adding a test for top level if statement parsing --- ...rser-printer-functional-components.spec.ts | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/editor/src/core/workers/parser-printer/parser-printer-functional-components.spec.ts b/editor/src/core/workers/parser-printer/parser-printer-functional-components.spec.ts index bf7b36691ba9..220018ac7fe2 100644 --- a/editor/src/core/workers/parser-printer/parser-printer-functional-components.spec.ts +++ b/editor/src/core/workers/parser-printer/parser-printer-functional-components.spec.ts @@ -167,6 +167,16 @@ export var whatever = (props) => { } ` +const codeWithComponentWithTopLevelIf = `import React from "react"; +import { View } from "utopia-api"; +export var whatever = (props) => { + if (props.showA) { + return + } else { + return + } +}` + describe('Parsing a function component with props', () => { it('Correctly parses a basic props object', () => { const actualResult = clearParseResultUniqueIDsAndEmptyBlocks( @@ -903,6 +913,63 @@ describe('Parsing a function component with props', () => { ) expect(actualResult).toEqual(expectedResult) }) + + it('Correctly parses a component with a top level if statement', () => { + const actualResult = clearParseResultUniqueIDsAndEmptyBlocks( + testParseCode(codeWithComponentWithTopLevelIf), + ) + const viewA = clearJSXElementChildUniqueIDs( + jsxElement( + 'View', + 'aaa', + jsxAttributesFromMap({ + 'data-uid': jsExpressionValue('aaa', emptyComments), + }), + [], + ), + ) + const viewB = clearJSXElementChildUniqueIDs( + jsxElement( + 'View', + 'bbb', + jsxAttributesFromMap({ + 'data-uid': jsExpressionValue('bbb', emptyComments), + }), + [], + ), + ) + const exported = utopiaJSXComponent( + 'whatever', + true, + 'var', + 'block', + defaultPropsParam, + ['showA'], + expect.objectContaining({ + javascript: `if (props.showA) {\n return \n } else {\n return \n }`, + definedElsewhere: expect.arrayContaining(['props']), + elementsWithin: { + aaa: viewA, + bbb: viewB, + }, + }), + expect.objectContaining({}), + false, + emptyComments, + ) + + const topLevelElements = [exported] + const expectedResult = parseSuccess( + JustImportViewAndReact, + expect.arrayContaining(topLevelElements), + expect.objectContaining({}), + null, + null, + [exportFunction('whatever')], + expect.objectContaining({}), + ) + expect(actualResult).toEqual(expectedResult) + }) }) describe('Parsing, printing, reparsing a function component with props', () => { @@ -996,4 +1063,8 @@ describe('Parsing, printing, reparsing a function component with props', () => { it('Correctly parses back and forth a component with a renamed function', () => { testParsePrintParse(codeWithARenamedFunction) }) + + it('Correctly parses back and forth a component with a top level if statement', () => { + testParsePrintParse(codeWithComponentWithTopLevelIf) + }) })