Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import invalidProjectUidlSample from './project-invalid-sample.json'
// @ts-ignore
import noRouteProjectUidlSample from './project-invalid-sample-no-route.json'
import {
component,
definition,
repeatNode,
dynamicNode,
elementNode,
} from '@teleporthq/teleport-uidl-builders'
const uidl = component(
'Repeat Component',
elementNode('container', {}, [
repeatNode(
elementNode('div', {}, [dynamicNode('local', 'item')]),
dynamicNode('prop', 'items'),
{
useIndex: true,
}
),
]),
{ items: definition('array', ['hello', 'world']) },
{ items: definition('array', ['hello', 'world']) }
)
describe('Validate UIDL', () => {
describe('Component UIDL Format', () => {
it('returns object with valid=true and errorMsg="" if uidl is valid', () => {
const validator = new Validator()
const validationResult = validator.validateComponentSchema(uidl)
expect(typeof validationResult).toBe('object')
// @ts-ignore
import invalidProjectUidlSample from './project-invalid-sample.json'
// @ts-ignore
import noRouteProjectUidlSample from './project-invalid-sample-no-route.json'
import {
component,
definition,
repeatNode,
dynamicNode,
elementNode,
} from '@teleporthq/teleport-uidl-builders'
const uidl = component(
'Repeat Component',
elementNode('container', {}, [
repeatNode(
elementNode('div', {}, [dynamicNode('local', 'item')]),
dynamicNode('prop', 'items'),
{
useIndex: true,
}
),
]),
{ items: definition('array', ['hello', 'world']) },
{ items: definition('array', ['hello', 'world']) }
)
describe('Validate UIDL', () => {
describe('Component UIDL Format', () => {
it('returns object with valid=true and errorMsg="" if uidl is valid', () => {
const validator = new Validator()
const validationResult = validator.validateComponentSchema(uidl)
{
value: 'about',
pageOptions: { fileName: 'about', componentName: 'About', navLink: '/about' },
},
{
value: 'contact',
pageOptions: { fileName: 'contact', componentName: 'Contact', navLink: '/contact' },
},
]
const structure: ComponentStructure = {
chunks: [],
options: {},
uidl: component(
'Test',
elementNode('Router', {}, [
conditionalNode(dynamicNode('state', 'route'), elementNode('container'), 'home'),
conditionalNode(dynamicNode('state', 'route'), elementNode('container'), 'about'),
conditionalNode(dynamicNode('state', 'route'), elementNode('container'), 'contact'),
]),
{},
{
route: routeDefinition,
}
),
dependencies: {},
}
const result = await plugin(structure)
// no change to the input UIDL
expect(JSON.stringify(result.uidl)).toBe(JSON.stringify(structure.uidl))
pageOptions: { fileName: 'about', componentName: 'About', navLink: '/about' },
},
{
value: 'contact',
pageOptions: { fileName: 'contact', componentName: 'Contact', navLink: '/contact' },
},
]
const structure: ComponentStructure = {
chunks: [],
options: {},
uidl: component(
'Test',
elementNode('Router', {}, [
conditionalNode(dynamicNode('state', 'route'), elementNode('container'), 'home'),
conditionalNode(dynamicNode('state', 'route'), elementNode('container'), 'about'),
conditionalNode(dynamicNode('state', 'route'), elementNode('container'), 'contact'),
]),
{},
{
route: routeDefinition,
}
),
dependencies: {},
}
const result = await plugin(structure)
// no change to the input UIDL
expect(JSON.stringify(result.uidl)).toBe(JSON.stringify(structure.uidl))
// AST chunks created
expect(result.chunks.length).toBe(1)
expect(result.chunks[0].type).toBe(ChunkType.AST)
{
value: 'about',
pageOptions: { fileName: 'about', componentName: 'About', navLink: '/about' },
},
{
value: 'contact',
pageOptions: { fileName: 'contact', componentName: 'Contact', navLink: '/contact' },
},
]
const structure: ComponentStructure = {
chunks: [],
options: {},
uidl: component(
'Test',
elementNode('Router', {}, [
conditionalNode(dynamicNode('state', 'route'), elementNode('container'), 'home'),
conditionalNode(dynamicNode('state', 'route'), elementNode('container'), 'about'),
conditionalNode(dynamicNode('state', 'route'), elementNode('container'), 'contact'),
]),
{},
{
route: routeDefinition,
}
),
dependencies: {},
}
const result = await plugin(structure)
// no change to the input UIDL
expect(JSON.stringify(result.uidl)).toBe(JSON.stringify(structure.uidl))
// AST chunks created
},
{
value: 'contact',
pageOptions: { fileName: 'contact', componentName: 'Contact', navLink: '/contact' },
},
]
const structure: ComponentStructure = {
chunks: [],
options: {},
uidl: component(
'Test',
elementNode('Router', {}, [
conditionalNode(dynamicNode('state', 'route'), elementNode('container'), 'home'),
conditionalNode(dynamicNode('state', 'route'), elementNode('container'), 'about'),
conditionalNode(dynamicNode('state', 'route'), elementNode('container'), 'contact'),
]),
{},
{
route: routeDefinition,
}
),
dependencies: {},
}
const result = await plugin(structure)
// no change to the input UIDL
expect(JSON.stringify(result.uidl)).toBe(JSON.stringify(structure.uidl))
// AST chunks created
expect(result.chunks.length).toBe(2)
expect(result.chunks[0].type).toBe(ChunkType.AST)
value: 'about',
pageOptions: { fileName: 'about', componentName: 'About', navLink: '/about' },
},
{
value: 'contact',
pageOptions: { fileName: 'contact', componentName: 'Contact', navLink: '/contact' },
},
]
const structure: ComponentStructure = {
chunks: [],
options: {},
uidl: component(
'Test',
elementNode('Router', {}, [
conditionalNode(dynamicNode('state', 'route'), elementNode('container'), 'home'),
conditionalNode(dynamicNode('state', 'route'), elementNode('container'), 'about'),
conditionalNode(dynamicNode('state', 'route'), elementNode('container'), 'contact'),
]),
{},
{
route: routeDefinition,
}
),
dependencies: {},
}
const result = await plugin(structure)
// no change to the input UIDL
expect(JSON.stringify(result.uidl)).toBe(JSON.stringify(structure.uidl))
// AST chunks created
it('counts duplicate nodes inside the UIDL', async () => {
const node = elementNode('container', {}, [
elementNode('container', {}, [elementNode('text'), elementNode('text'), elementNode('text')]),
])
const lookup: Record = {}
createNodesLookup(node, lookup)
expect(lookup.container.count).toBe(2)
expect(lookup.container.nextKey).toBe('0')
expect(lookup.text.count).toBe(3)
expect(lookup.container.nextKey).toBe('0')
})
it('adds a <style> element', async () => {
const style = {
height: staticNode('100px'),
}
const element = elementNode('div', {}, [], null, style)
element.content.key = 'container'
const group = elementNode('Fragment', {}, [element])
group.content.key = 'group'
const uidlSample = component('StyledJSX', group)
const structure: ComponentStructure = {
uidl: uidlSample,
options: {},
chunks: [componentChunk],
dependencies: {},
}
const { chunks } = await plugin(structure)
expect(chunks.length).toBe(1)
const nodeReference = componentChunk.meta.nodesLookup.group
expect(nodeReference.children.length).toBe(1)
</style>
describe('checkForIllegalNames', () => {
const comp = component(
'Component',
elementNode('container'),
{
'my-title': definition('string', 'test'),
},
{
isVisible: definition('boolean', false),
}
)
comp.outputOptions = {
componentClassName: 'Component',
fileName: 'component',
}
it('checks component name', () => {
checkForIllegalNames(comp, mapping)
expect(comp.outputOptions.componentClassName).toBe('AppComponent')