-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fd50d45
commit 0a17511
Showing
4 changed files
with
121 additions
and
1 deletion.
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
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,17 @@ | ||
export class InputTextElement extends HTMLElement | ||
{ | ||
constructor() | ||
{ | ||
super(); | ||
const i = this.ownerDocument.createElement('input'); | ||
for(let a of this.attributes) | ||
a.namespaceURI ? i.setAttributeNS(a.namespaceURI,a.name,a.value) : i.setAttribute(a.name,a.value) | ||
this.append(i) | ||
} | ||
get value(){ return this.firstChild.value } | ||
set value(v){ return this.firstChild.value = v } | ||
disconnectedCallback(){ } | ||
} | ||
|
||
window.customElements.define( 'input-text', InputTextElement ); | ||
export default InputTextElement; |
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,53 @@ | ||
import '../src/custom-element.js'; | ||
|
||
export default | ||
{ title: 'dom-merge', component: 'custom-element', argTypes: | ||
{ title: { control: 'text', defaultValue: 'simple payload' } | ||
, tag: { control: 'text', defaultValue: 'my-component' } | ||
, attributes: { control: 'text', defaultValue: '' } | ||
, slot: { control: 'text', defaultValue: `Hello World` } | ||
, payload: { control: 'text', defaultValue: `🍋` } | ||
} | ||
}; | ||
|
||
function Template( { title, tag , attributes, slot, payload } ) | ||
{ | ||
return ` | ||
<fieldset> | ||
<legend>${ title }</legend> | ||
<custom-element | ||
tag="${ tag }" | ||
hidden | ||
> | ||
${ slot } | ||
</custom-element> | ||
<${ tag } ${attributes}>${ payload }</${ tag }> | ||
</fieldset> | ||
`; | ||
} | ||
|
||
export const TextareaOnBlur = Template.bind( {} ); | ||
TextareaOnBlur.args = | ||
{ title: 'Textarea value, type and observe char count update on focus change' | ||
, tag: '' | ||
, slot: ` <textarea slice="text-container" >Hello world!</textarea> | ||
<p> Char count: | ||
<b>{string-length(//slice/text-container/text())}</b> | ||
</p> | ||
<button> click to focus change </button> | ||
` | ||
}; | ||
|
||
export const InputOnChange = Template.bind( {} ); | ||
InputOnChange.args = | ||
{ title: 'input value, type and observe char count update on keyup' | ||
, slot: ` <input type="text" value="Type time update" slice="txt" slice-update="keyup"/> | ||
<span> Character count: | ||
<b> {string-length(//slice/txt)} </b> | ||
</span> | ||
<span> Word count: | ||
<i> {string-length(normalize-space(//slice/txt)) - | ||
string-length(translate(normalize-space(//slice/txt), ' ', '')) + 1} </i> | ||
</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,50 @@ | ||
import { fixture, expect, aTimeout } from '@open-wc/testing'; | ||
|
||
import '../src/custom-element.js'; | ||
import defaults, {TextareaOnBlur, InputOnChange} from "../stories/dom-merge.stories.js"; | ||
|
||
const defs = {}; Object.keys(defaults.argTypes).map( k=> defs[k]= defaults.argTypes[k].defaultValue); | ||
const renderStory = async (story) => | ||
{ const el = await fixture(story({...defs, ...story.args})); | ||
return { el | ||
, $: css => el.querySelector(css) | ||
, $$: css => el.querySelectorAll(css) | ||
, txt: css => el.querySelector(css).innerText | ||
} | ||
} | ||
const TXT = "BE CURIOUS, LEARN AND ADOPT KNOWLEDGE, SEEK THE PATH, STATE THE GOALS AND OVERCOME."; | ||
|
||
describe('dom-merge', () => { | ||
|
||
it('LocalStorageLoad slice init from value', async () => { | ||
const {el, $,$$,txt} = await renderStory(TextareaOnBlur); | ||
expect( txt('textarea') ).to.equal(`Hello world!`); | ||
expect( $('textarea').value ).to.equal(`Hello world!`); | ||
await aTimeout(10); // wait for update from slice | ||
expect( txt('b') ).to.equal(`Hello world!`.length+'' ); | ||
}); | ||
|
||
it('LocalStorageLoad slot change on blur', async () => { | ||
const {el, $,$$,txt} = await renderStory(TextareaOnBlur); | ||
await aTimeout(10); // wait for update from slice | ||
expect( txt('b') ).to.equal(`Hello world!`.length+'' ); | ||
const ta = $('textarea'); | ||
ta.value = TXT; | ||
ta.dispatchEvent( new Event('change') ); | ||
await aTimeout(10); // wait for update from slice | ||
expect( txt('b') ).to.equal(TXT.length+'' ); | ||
}); | ||
|
||
it('LocalStorageLoad slot change on blur', async () => { | ||
const {el, $,$$,txt} = await renderStory(InputOnChange); | ||
await aTimeout(10); // wait for update from slice | ||
expect( txt('b[data-dce-id]') ) // from instance instead of in-template | ||
.to.equal(`Type time update`.length+'' ); | ||
const ta = $('input[data-dce-id]'); | ||
ta.value = TXT; | ||
ta.dispatchEvent( new Event('keyup') ); | ||
await aTimeout(10); // wait for update from slice | ||
expect( txt('b[data-dce-id]') ).to.equal(TXT.length+'' ); | ||
}); | ||
|
||
}); |