Created
September 12, 2023 03:41
-
-
Save YonatanKra/275f932f5857aabd18d3abd7cc7902fb to your computer and use it in GitHub Desktop.
Tauri-demo - adding the DOM elements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Greeter } from './index'; | |
describe('greeter', () => { | |
it('should be defined', () => { | |
expect(Greeter).toBeDefined(); | |
}); | |
it ('should set a shadow DOM with mode open', () => { | |
customElements.define('yag-greeter', Greeter); | |
const greeter = document.createElement('yag-greeter'); | |
expect(greeter.shadowRoot).toBeDefined(); | |
expect(greeter.shadowRoot?.mode).toBe('open'); | |
}); | |
it ('should have a shadow root with mode open', () => { | |
const greeter = document.createElement('yag-greeter'); | |
expect(greeter.shadowRoot?.mode).toBe('open'); | |
}); | |
it ('should have greet button inside', () => { | |
const greeter = document.createElement('yag-greeter'); | |
expect(greeter.shadowRoot?.querySelector('#greet-form button')).toBeTruthy(); | |
}); | |
it ('should have greet form inside', () => { | |
const greeter = document.createElement('yag-greeter'); | |
expect(greeter.shadowRoot?.querySelector('#greet-form')).toBeTruthy(); | |
}); | |
it ('should have greet input inside', () => { | |
const greeter = document.createElement('yag-greeter'); | |
const greetInput = greeter.shadowRoot?.querySelector('#greet-input'); | |
expect(greetInput?.form).toEqual(greeter.shadowRoot?.querySelector('#greet-form')); | |
}); | |
it ('should have greet message area', () => { | |
const greeter = document.createElement('yag-greeter'); | |
expect(greeter.shadowRoot?.querySelector('#greet-msg')).toBeTruthy(); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const template = ` | |
<form class="row" id="greet-form"> | |
<input id="greet-input" placeholder="Enter a name..." /> | |
<button type="submit">Greet</button> | |
</form> | |
<p id="greet-msg"></p> | |
`; | |
const templateElement = document.createElement('template'); | |
templateElement.innerHTML = template; | |
export class Greeter extends HTMLElement { | |
constructor() { | |
super(); | |
this.attachShadow({ mode: "open" }); | |
const templateHTML = templateElement.content.cloneNode(true); | |
this.shadowRoot?.appendChild(templateHTML); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment