Skip to content

Commit

Permalink
added: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GianlucaGuarini committed Aug 1, 2022
1 parent 773be21 commit aa2b08c
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 8 deletions.
4 changes: 1 addition & 3 deletions src/utils/create-runtime-slots.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {COMPONENTS_IMPLEMENTATION_MAP, DOM_COMPONENT_INSTANCE_PROPERTY} from '@riotjs/util'
import {
bindingTypes,
expressionTypes,
Expand All @@ -23,7 +22,6 @@ export default function createRuntimeSlots(el) {
return Function(`return ${slotsCode}`)()(
template,
expressionTypes,
bindingTypes,
(name) => el[DOM_COMPONENT_INSTANCE_PROPERTY].components[name] || COMPONENTS_IMPLEMENTATION_MAP.get(name)
bindingTypes
)
}
15 changes: 15 additions & 0 deletions test/components/runtime-slot-with-children.riot
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<runtime-slot-with-children>
<slot {...props}/>
<child/>
<title-prop title={'hello'}/>

<script>
import Child from './child.riot'
export default {
components: {
Child
}
}
</script>
</runtime-slot-with-children>
3 changes: 3 additions & 0 deletions test/components/runtime-slot.riot
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<runtime-slot>
<slot {...props}/>
</runtime-slot>
56 changes: 51 additions & 5 deletions test/specs/compiler.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as riot from '../../src/riot+compiler'
import {GLOBAL_REGISTRY} from '../../src/compiler/global-registry'
import SimpleSlotComponent from '../components/simple-slot.riot'
import RuntimeSlotComponent from '../components/runtime-slot.riot'
import RuntimeSlotWithChildrenComponent from '../components/runtime-slot-with-children.riot'
import TitlePropComponent from '../components/title-prop.riot'
import {expect} from 'chai'

describe('Riot compiler api', () => {
Expand Down Expand Up @@ -51,16 +53,60 @@ describe('Riot compiler api', () => {
expect(code).to.be.ok
})

it.skip('Runtime slots get properly evaluated', () => {
const el = document.createElement('simple-slot')
el.innerHTML = '<p>{props.message}</p>'
it('Runtime slots get properly evaluated with riot.component', () => {
const el = document.createElement('runtime-slot')
el.innerHTML = '<p>{message}</p>'

const component = riot.component(SimpleSlotComponent)(el, {
const component = riot.component(RuntimeSlotComponent)(el, {
message: 'hello'
})

expect(el.querySelector('p').innerHTML).to.be.equal('hello')

component.unmount()
})

it('Runtime slots with children components get properly evaluated with riot.component', () => {
const el = document.createElement('runtime-slot-with-children')
el.innerHTML = '<p>{message}</p>'
riot.register('title-prop', TitlePropComponent)

const component = riot.component(RuntimeSlotWithChildrenComponent)(el, {
message: 'hello'
})

expect(el.querySelector('p').innerHTML).to.be.equal('hello')
expect(el.querySelector('child').innerHTML).to.be.not.empty
expect(el.querySelector('title-prop').innerHTML).to.be.not.empty

component.unmount()
riot.unregister('title-prop')
})

it('Empty DOM Components will not mount runtime slots', () => {
const el = document.createElement('runtime-slot')

const component = riot.component(RuntimeSlotComponent)(el, {
message: 'hello'
})

expect(el.innerHTML).to.be.equal('')

component.unmount()
})

it('Runtime slots get properly evaluated with riot.mount', () => {
const el = document.createElement('runtime-slot')
el.innerHTML = '<p>{message}</p>'
riot.register('runtime-slot', RuntimeSlotComponent)

const [component] = riot.mount(el, {
message: 'hello'
}, 'runtime-slot')

expect(el.querySelector('p').innerHTML).to.be.equal('hello')

component.unmount()
riot.unregister('runtime-slot')
})
})

0 comments on commit aa2b08c

Please sign in to comment.