-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(web-components): Add Powerbox-compatible petname component
- Loading branch information
1 parent
a9895c4
commit 4afa736
Showing
6 changed files
with
390 additions
and
93 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { AgoricPetname } from './src/AgoricPetname.js'; | ||
|
||
window.customElements.define('agoric-petname', AgoricPetname); |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { html, LitElement } from 'lit'; | ||
import Powerbox from './Powerbox.js'; | ||
|
||
export class AgoricPetname extends LitElement { | ||
static get properties() { | ||
return { | ||
uid: { type: String }, | ||
}; | ||
} | ||
|
||
// eslint-disable-next-line class-methods-use-this | ||
updated() { | ||
Powerbox.expandPetdata(); | ||
} | ||
|
||
render() { | ||
return html` | ||
<span | ||
data-powerbox-target="petname-if-known" | ||
data-powerbox-id="${this.uid}" | ||
> | ||
${this.uid} | ||
</span> | ||
`; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* global powerbox */ | ||
|
||
class Powerbox { | ||
constructor() { | ||
this.loaded = false; | ||
window.addEventListener('load', () => { | ||
this.loaded = true; | ||
this.expandPetdata(); | ||
}); | ||
} | ||
|
||
expandPetdata() { | ||
if (!this.loaded) return; | ||
if (typeof powerbox === 'undefined') { | ||
throw new Error('powerbox not found'); | ||
} | ||
powerbox.expandPetdata(); | ||
} | ||
} | ||
|
||
export default new Powerbox(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { html } from 'lit'; | ||
import { stub, restore } from 'sinon'; | ||
import { fixture, expect } from '@open-wc/testing'; | ||
import '../agoric-petname.js'; | ||
import Powerbox from '../src/Powerbox.js'; | ||
|
||
describe('AgoricPetname', () => { | ||
beforeEach(() => {}); | ||
|
||
afterEach(() => { | ||
restore(); | ||
}); | ||
|
||
it('passes the a11y audit', async () => { | ||
const el = await fixture( | ||
html` | ||
<agoric-petname uid="AG.1"></agoric-petname> | ||
`, | ||
); | ||
|
||
await expect(el).shadowDom.to.be.accessible(); | ||
}); | ||
|
||
it('calls Powerbox.expandPetdata on update', async () => { | ||
const expandPetdataStub = stub(Powerbox, 'expandPetdata'); | ||
const el = await fixture( | ||
html` | ||
<agoric-petname uid="AG.1"></agoric-petname> | ||
`, | ||
); | ||
expect(expandPetdataStub).to.have.callCount(1); | ||
|
||
await el.setAttribute('uid', 'AG.2'); | ||
|
||
expect(expandPetdataStub).to.have.callCount(2); | ||
}); | ||
|
||
it('renders a span with the correct data', async () => { | ||
const el = await fixture( | ||
html` | ||
<agoric-petname uid="AG.1"></agoric-petname> | ||
`, | ||
); | ||
|
||
const inner = el.shadowRoot.querySelector('span'); | ||
expect(inner.getAttribute('data-powerbox-target')).to.equal( | ||
'petname-if-known', | ||
); | ||
expect(inner.getAttribute('data-powerbox-id')).to.equal('AG.1'); | ||
expect(inner.innerText).to.equal('AG.1'); | ||
}); | ||
}); |
Oops, something went wrong.