Last active
September 17, 2023 04:48
-
-
Save YonatanKra/6adf9f84d9e2d4f47d844c567bd340e0 to your computer and use it in GitHub Desktop.
Taudi-demo: hide and show greeter according to logged in state
This file contains hidden or 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 { App } from './app'; | |
| customElements.define('yag-app', App); | |
| class MockAuth extends HTMLElement { | |
| constructor() { | |
| super(); | |
| authComponent = this; | |
| } | |
| isLoggedIn() { | |
| return isLoggedIn; | |
| } | |
| } | |
| customElements.define('yag-auth', MockAuth); | |
| let authComponent: MockAuth | HTMLElement = document.createElement('div'); | |
| let isLoggedIn = true; | |
| describe('app', () => { | |
| let app: App; | |
| beforeEach(() => { | |
| app = document.createElement('yag-app') as App; | |
| }); | |
| afterEach(() => { | |
| app.remove(); | |
| }); | |
| it('should be have an open shadow root', () => { | |
| expect(app.shadowRoot?.mode).toBe('open'); | |
| }); | |
| it('should remove `yag-greeter` when user is not logged in', () => { | |
| isLoggedIn = false; | |
| app.connectedCallback(); | |
| expect(app.shadowRoot?.querySelector('yag-greeter')).toBeFalsy(); | |
| }); | |
| it('should display `yag-greeter` when user is logged in', () => { | |
| isLoggedIn = true; | |
| app.connectedCallback(); | |
| expect(app.shadowRoot?.querySelector('yag-greeter')).toBeTruthy(); | |
| }); | |
| }); |
This file contains hidden or 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 = ``; | |
| const templateElement = document.createElement('template'); | |
| templateElement.innerHTML = template; | |
| export class App extends HTMLElement{ | |
| #authComponent?: HTMLElement; | |
| constructor(){ | |
| super(); | |
| this.attachShadow({mode: 'open'}); | |
| const templateHTML = templateElement.content.cloneNode(true); | |
| this.shadowRoot?.appendChild(templateHTML); | |
| } | |
| connectedCallback(){ | |
| this.#authComponent = document.createElement('yag-auth') as HTMLElement; | |
| if (this.#authComponent.isLoggedIn?.() === false) { | |
| this.shadowRoot!.innerHTML = ``; | |
| } else { | |
| this.shadowRoot!.innerHTML = `<yag-greeter></yag-greeter>`; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment