Skip to content

Commit 52dc796

Browse files
authored
chore: add tests to check KeepAlive multi children
1 parent 58abba1 commit 52dc796

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

packages/compiler-core/__tests__/transforms/transformElement.spec.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,4 +1224,55 @@ describe('compiler: element transform', () => {
12241224
isBlock: false
12251225
})
12261226
})
1227+
1228+
describe('<KeepAlive> multiple children', () => {
1229+
1230+
function checkWarning(
1231+
template: string,
1232+
shouldWarn: boolean,
1233+
message = '<KeepAlive> expects exactly one child component.'
1234+
) {
1235+
const spy = jest.fn()
1236+
1237+
parseWithBind(template.trim(), {
1238+
onError: err => {
1239+
spy(err.message)
1240+
}
1241+
})
1242+
1243+
if (shouldWarn) expect(spy).toHaveBeenCalledWith(message)
1244+
else expect(spy).not.toHaveBeenCalled()
1245+
}
1246+
1247+
test('does not warn if has one children', () => {
1248+
checkWarning(
1249+
`<KeepAlive>
1250+
<component :is="activeComponent"/>
1251+
</KeepAlive>`,
1252+
false
1253+
)
1254+
})
1255+
1256+
test('warn if has multiple children', () => {
1257+
checkWarning(
1258+
`<KeepAlive>
1259+
<component :is="activeComponent"/>
1260+
<component :is="activeComponent2"/>
1261+
</KeepAlive>`,
1262+
true
1263+
)
1264+
})
1265+
1266+
test('should ignore comments', () => {
1267+
expect(baseCompile(
1268+
`<KeepAlive>
1269+
<!--this should be ignored -->
1270+
<component :is="activeComponent"/>
1271+
<!--this should be ignored -->
1272+
</KeepAlive>`,
1273+
{prefixIdentifiers: true}
1274+
).code).toMatchSnapshot()
1275+
})
1276+
})
1277+
12271278
})

0 commit comments

Comments
 (0)