Skip to content

Commit

Permalink
fix(Root): Fix StyleObject type: now it accepts Objects inside Object…
Browse files Browse the repository at this point in the history
…s, not just string values
  • Loading branch information
eanorambuena committed Jul 23, 2024
1 parent 62fb433 commit 7701d02
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 30 deletions.
4 changes: 2 additions & 2 deletions dist/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ export declare const javascript: (template: {
}, ...substitutions: any[]) => string;
export declare const routerClassNames = "flex flex-col justify-center items-center space-y-3 text-center w-full h-fit box-border";
export type RouteString = `/${string}`;
export type StyleObject = {
[key: string]: string;
export type StyleObject = string | {
[key: string]: StyleObject;
};
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@ export const javascript = String.raw
export const routerClassNames = 'flex flex-col justify-center items-center space-y-3 text-center w-full h-fit box-border'

export type RouteString = `/${string}`
export type StyleObject = {
[key: string]: string
export type StyleObject = string | {
[key: string]: StyleObject
}
56 changes: 30 additions & 26 deletions test/components.test.js → test/components.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, it, expect } from 'vitest'
import { Component, LightComponent, FunctionalComponent, load } from '../src/index.ts'
import { awaitDidMount } from './utils.ts'
import { describe, it, expect, assert } from 'vitest'
import { Component, LightComponent, FunctionalComponent, load, HTMLGenerator } from '../src/index.ts'
import { attachToDocument, awaitDidMount } from './utils.ts'
import { HTMLElement } from 'happy-dom'

// @vitest-environment happy-dom
Expand Down Expand Up @@ -45,7 +45,7 @@ describe('Component', () => {
}
customElements.define('emmy-a', A)
document.body.innerHTML = '<emmy-a></emmy-a>'
return document.querySelector('emmy-a').Style['this']
return (document.querySelector('emmy-a') as Component).Style['this']
})()).toBe('background-color: red;')
})
it('addStyle method should add a React style', () => {
Expand All @@ -63,7 +63,7 @@ describe('Component', () => {
}
customElements.define('emmy-a', A)
document.body.innerHTML = '<emmy-a></emmy-a>'
return document.querySelector('emmy-a').Style['this']
return (document.querySelector('emmy-a') as Component).Style['this']
})()).toBe('background-color: red;')
})
it('querySelector method should return an HTMLElement', () => {
Expand All @@ -76,12 +76,13 @@ describe('Component', () => {
}
customElements.define('emmy-a', A)
document.body.innerHTML = '<emmy-a></emmy-a>'
return document.querySelector('emmy-a').querySelector('div')
return document.querySelector('emmy-a')?.querySelector('div')
})()).toBeInstanceOf(HTMLElement)
})
it('should render', () => {
expect((() => {
class A extends Component {
didMount: boolean
constructor() {
super()
this.didMount = false
Expand All @@ -93,7 +94,7 @@ describe('Component', () => {
customElements.define('emmy-a', A)
document.body.innerHTML = '<emmy-a></emmy-a>'
awaitDidMount('emmy-a')
return document.querySelector('emmy-a').querySelector('div')
return document.querySelector('emmy-a')?.querySelector('div')
})()).toBeDefined()
})
})
Expand Down Expand Up @@ -138,7 +139,7 @@ describe('LightComponent', () => {
}
customElements.define('emmy-a', A)
document.body.innerHTML = '<emmy-a></emmy-a>'
return document.querySelector('emmy-a').Style['this']
return (document.querySelector('emmy-a') as Component).Style['this']
})()).toBe('background-color: red;')
})
it('addStyle method should add a React style', () => {
Expand All @@ -156,12 +157,13 @@ describe('LightComponent', () => {
}
customElements.define('emmy-a', A)
document.body.innerHTML = '<emmy-a></emmy-a>'
return document.querySelector('emmy-a').Style['this']
return (document.querySelector('emmy-a') as Component).Style['this']
})()).toBe('background-color: red;')
})
it('querySelector method should return an HTMLElement', () => {
expect((() => {
class A extends LightComponent {
didMount: boolean
constructor() {
super()
this.didMount = false
Expand All @@ -173,12 +175,13 @@ describe('LightComponent', () => {
customElements.define('emmy-a', A)
document.body.innerHTML = '<emmy-a></emmy-a>'
awaitDidMount('emmy-a')
return document.querySelector('emmy-a').querySelector('div')
return document.querySelector('emmy-a')?.querySelector('div')
})()).toBeInstanceOf(HTMLElement)
})
it('should render', () => {
expect((() => {
class A extends LightComponent {
didMount: boolean
constructor() {
super()
this.didMount = false
Expand All @@ -190,7 +193,7 @@ describe('LightComponent', () => {
customElements.define('emmy-a', A)
document.body.innerHTML = '<emmy-a></emmy-a>'
awaitDidMount('emmy-a')
return document.querySelector('emmy-a').querySelector('div')
return document.querySelector('emmy-a')?.querySelector('div')
})()).toBeDefined()
})
})
Expand Down Expand Up @@ -234,7 +237,7 @@ describe('FunctionalComponent', () => {
}
customElements.define('emmy-a', A)
document.body.innerHTML = '<emmy-a></emmy-a>'
return document.querySelector('emmy-a').Style['this']
return (document.querySelector('emmy-a') as Component)?.Style['this']
})()).toBe('background-color: red;')
})
it('addStyle method should add a React style', () => {
Expand All @@ -251,12 +254,13 @@ describe('FunctionalComponent', () => {
}
customElements.define('emmy-a', A)
document.body.innerHTML = '<emmy-a></emmy-a>'
return document.querySelector('emmy-a').Style['this']
return (document.querySelector('emmy-a') as Component)?.Style['this']
})()).toBe('background-color: red;')
})
it('querySelector method should return an HTMLElement', () => {
expect((() => {
class A extends FunctionalComponent {
didMount: boolean
constructor() {
super(() => '<div></div>')
this.didMount = false
Expand All @@ -268,12 +272,13 @@ describe('FunctionalComponent', () => {
customElements.define('emmy-a', A)
document.body.innerHTML = '<emmy-a></emmy-a>'
awaitDidMount('emmy-a')
return document.querySelector('emmy-a').querySelector('div')
return document.querySelector('emmy-a')?.querySelector('div')
})()).toBeInstanceOf(HTMLElement)
})
it('should render', () => {
expect((() => {
class A extends FunctionalComponent {
didMount: boolean
constructor() {
super(() => '<div></div>')
this.didMount = false
Expand All @@ -283,28 +288,27 @@ describe('FunctionalComponent', () => {
}
}
customElements.define('emmy-a', A)
document.body.innerHTML = '<emmy-a></emmy-a>'
awaitDidMount('emmy-a')
return document.querySelector('emmy-a').querySelector('div')
return attachToDocument('emmy-a').querySelector('div')
})()).toBeDefined()
})
it('should render with functional syntax', () => {
expect((() => {
const a = () => '<div></div>'
load(a, 'B')
document.body.innerHTML = '<emmy-b></emmy-b>'
return document.querySelector('emmy-b').querySelector('div')
load(a, 'A')
document.body.innerHTML = '<emmy-a></emmy-a>'
return document.querySelector('emmy-a')?.querySelector('div')
})()).toBeDefined()
})
/* Test not working, probably it is a bug in the mocking or test
it ('should render with functional syntax and props', () => {
expect((() => {
function a({ props }) {
return `<div>${props().state()}</div>`
}
load(a, 'C')
document.body.innerHTML = '<emmy-c></emmy-c>'
awaitDidMount('emmy-c')
return String(document.querySelector('emmy-c').querySelector('div'))
})()).toBe('<div>{\'rerenderCount\':0}</div>')
})
load(a as HTMLGenerator, 'A')
document.body.innerHTML = '<emmy-a state="1"></emmy-a>'
awaitDidMount('emmy-a')
return String(document.querySelector('emmy-a')?.querySelector('div'))
})()).toBe('<div>1</div>')
})*/
})
8 changes: 8 additions & 0 deletions test/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Component } from '../src/index.ts'

export function awaitDidMount(componentName) {
return new Promise((resolve, reject) => {
const interval = setInterval(() => {
Expand All @@ -8,3 +10,9 @@ export function awaitDidMount(componentName) {
}, 100)
})
}

export function attachToDocument(elementName: string = 'emmy-a') {
document.body.innerHTML = `<${elementName}></${elementName}>`
awaitDidMount(elementName)
return document.querySelector(elementName) as Component
}

0 comments on commit 7701d02

Please sign in to comment.