Skip to content

Commit

Permalink
chore(tests) Adding a test for top level if statement parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Rheeseyb committed Jan 9, 2024
1 parent 703255c commit aebfa59
Showing 1 changed file with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 <View data-uid={'aaa'} />
} else {
return <View data-uid={'bbb'} />
}
}`

describe('Parsing a function component with props', () => {
it('Correctly parses a basic props object', () => {
const actualResult = clearParseResultUniqueIDsAndEmptyBlocks(
Expand Down Expand Up @@ -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 <View data-uid={'aaa'} />\n } else {\n return <View data-uid={'bbb'} />\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', () => {
Expand Down Expand Up @@ -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)
})
})

0 comments on commit aebfa59

Please sign in to comment.