Skip to content

Commit

Permalink
data-slices.test
Browse files Browse the repository at this point in the history
  • Loading branch information
sashafirsov committed Apr 20, 2024
1 parent 31b1e0d commit 4f9f6e9
Show file tree
Hide file tree
Showing 11 changed files with 441 additions and 29 deletions.
9 changes: 5 additions & 4 deletions src/mocks/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ import { rest } from 'msw'

import pokemonsMock from "../../stories/pokemons.mock";

const resolveAsPokemons = (res,ctx) => res(ctx.json(pokemonsMock));

export const handlers =
[ rest.get("*/api/v2/pokemon", (req, res, ctx) =>
{
return res(ctx.json(pokemonsMock));
}),
rest.get("*/noreturn", (req, res, ctx) =>
{ return new Promise((resolve)=>{ setTimeout(()=>
{ resolve(res(ctx.json(pokemonsMock)))
}, 10000)}); // 1 second to be able to catch the initial state before the full data returned;
{ return new Promise((resolve)=>{ setTimeout(()=> resolve( resolveAsPokemons(res,ctx) ), 10000)});
// 10 seconds to be able to catch the initial state before the full data returned;
}),
rest.get("*/reflect", (req, res, ctx) =>
{ const headersMap = {};
[...req.headers.entries()].map( ([key,val]) => headersMap[key] = val );
ctx.set(headersMap);
return res(ctx.json(pokemonsMock));
return resolveAsPokemons(res,ctx);
})
];
129 changes: 129 additions & 0 deletions stories/data-slices.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import '../src/custom-element.js';

export default
{ title: 'Events and data Slice', component: 'custom-element', argTypes:
{ title: { control: 'text', defaultValue: 'simple aDceTemplate' }
, description: { control: 'text', defaultValue: '' }
, aDceTemplate: { control: 'text', defaultValue: `Hi` }
}
};

function Template( { title, description, aDceTemplate, tag, dce } )
{
return `
<fieldset>
<legend>${ title }</legend>
<custom-element ${ tag ? ` tag="${tag}" `:''} >
<template>
${ aDceTemplate }
</template>
</custom-element>
<hr/><p>${description}</p>
<hr/>${dce}
</fieldset>
`;
}

function demo( title, description, aDceTemplate, tag='', dce='' )
{ const ret = Template.bind( {} );
ret.args = { title, description, aDceTemplate,tag,dce}
return ret;
}
export const SliceInitializationChangeOnEvent = demo( `A. slice initialization, change on event`,
'initial value should be 0; + and - should change the number in input field',
`<button slice="clickcount" slice-event="click" slice-value="//clickcount + 1">
+
</button>
<button slice="clickcount" slice-event="click" slice-value="//clickcount - 1">
-
</button>
<input slice="clickcount" type="number" value="{//clickcount ?? 0}"/>
<code>{//clickcount}</code>
`);

export const EventWithinSlice = demo( `B. event within slice`,
'move the mouse over TEXTAREA and click to see slice and slice event changed',
`
<textarea slice="s" slice-value="concat('x:', //@pageX)" slice-event="mousemove click" style="width:16rem;height:16rem;box-shadow: inset {//@offsetX}px {//@offsetY}px gold;"></textarea><br>
//slice/s : <var>{//slice/s}</var> <br>
//slice/s/event/@offsetY: <code>{//slice/s/event/@offsetY}</code> <br>
event type: {//slice/s/event/@type}
`);

export const SliceChangeOnEvent = demo( `1. slice change on event. 1:1 slice⮂value`,
'initial value blank; type and unfocus to see slice changed',
`
<input slice="typed"> //slice/typed : <code>{//slice/typed}</code>
`);

export const InitDefaultEvent = demo( `2. initial slice value, slice change on event. slice⮂value, w/ initial`,
'initial value from input; type and unfocus to see slice changed',
`
<input slice="s" value="{//s ?? 'A'}"> //slice/s : <code>{//slice/s}</code>
`);

export const InitAndKeyUp = demo( `3. initial slice value, slice change on event. slice⮂value, w/ initial`,
'initial value from input; type to see slice changed',
`
<input slice="s" value="{//s ?? 'B'}" slice-event="input"> //slice/s : <code>{//slice/s}</code>
`);

export const InitByAttribute = demo( `4. initial slice value from attribute, slice change on event.`,
'initial value from input; type to see slice changed',
`
<attribute name="a">😁</attribute>
<input slice="s" value="{//s ?? $a}" slice-event="input">
attribute 'a' : {$a}
//slice/s : <code>{//slice/s}</code>
`,'dce-1','<dce-1 id="e1"></dce-1> <dce-1 id="e2" a="🤗"></dce-1>');

export const InitByAttributeChangeByExpression = demo( `5. initial slice value from attribute, slice change on event.`,
'initial value from input as \'xB\'; type and unfocus to see slice changed',
`
<input slice="s" value="{substring(//s, 2) ?? 'B'}" slice-value="concat('x', @value )">
//slice/s : <code>{//slice/s}<code>
`);

export const InitByInputValueByButton = demo( `6. initial slice value from input, button ignored till change on click.`,
'initial value from input as \'anonymous\'; on button click change to \'broccoli\'',
`
<input slice="nickname" value="anonymous">
<button slice="nickname" slice-value="'broccoli'" slice-event="click">🥦</button>
<code>{//nickname}</code>
`);

export const InitBySliceElement = demo( `7. initial slice value from SLICE element, button ignored till change on click.`,
'synthetic SLICE element serves as initial value holder',
`
<button slice="clickcount" slice-event="click tap" slice-value="//clickcount + 1">
<slice slice="clickcount" value="0"></slice>
click/tap
</button>
//clickcount : <code>{//clickcount}</code>
`);

export const InitByFewSliceElement = demo( `8. multiple slices by SLICE element, button ignored till change on click.`,
'few synthetic SLICE elements serve as initial value holder',
`
<button>
<slice slice="clicked" value="{0}"></slice>
<slice slice="focused" value="{0}"></slice>
<slice slice-event="click tap" slice="clicked" slice-value="//clicked+1"></slice>
<slice slice-event="focus" slice="focused" slice-value="1"></slice>
<slice slice-event="blur" slice="focused" slice-value="0"></slice>
click/tap, focus/blur
</button> <br>
//clicked : <code>{//clicked}</code> <br/>
//focused : <var>{//focused}</var>
`);

export const SlicePointsAttribute = demo( `9. slice in attribute`,
'initial attribute value should be smile as emoji and :) on blur from input it should be updated from value',
`
<attribute name="emotion" select="//emotion ?? '😃'"></attribute>
<input slice="/datadom/attributes/emotion">
Type and unfocus to update emotion attribute: <code>{emotion}</code>
`,'emotional-element', `
<emotional-element id="e1" emotion=":)"></emotional-element>
<emotional-element id="e2"></emotional-element>
`);
2 changes: 1 addition & 1 deletion stories/dom-merge.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ TextareaOnBlur.args =
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"/>
, slot: ` <input type="text" value="Type time update" slice="txt" slice-event="keyup"/>
<span> Character count:
<b> {string-length(//slice/txt)} </b>
</span>
Expand Down
21 changes: 11 additions & 10 deletions stories/http-request.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ function Template( { title, tag , slice, url } )
url="${url}"
slice="${slice}"
></http-request>
<if test="not(//slice/${slice}/data/results/*)">
<p>Pokemon Count: {count(/datadom/slice/${slice}//results)}</p>
<if test="count(/datadom/slice/${slice}//results) &lt; 0">
<h3>loading...</h3>
</if>
<for-each select="//slice/${slice}/data/results/*">
<for-each select="/datadom/slice/${slice}//results">
<variable name="pokeid"
select="substring-before( substring-after( @url, 'https://pokeapi.co/api/v2/pokemon/'),'/')"
></variable>
Expand Down Expand Up @@ -66,7 +67,7 @@ LifecycleInitialized = ()=>`
type="text"
></http-request>
Content of <code>//slice/request_slice</code> before <b>response</b> available
<for-each select="//slice/request_slice/*">
<for-each select="//slice/request_slice/value/*">
<ul>
<var data-testid="request-section"><value-of select='name(.)'/></var>
<for-each select="@*">
Expand Down Expand Up @@ -106,14 +107,14 @@ from <code>${url}</code>
<h3>Samples</h3>
<table>
<tr><th>//slice/request_slice/request/headers/@mode</th>
<td><value-of select="//slice/request_slice/request/@mode"/></td></tr>
<tr><th>//slice/request_slice/response/headers/@content-type</th>
<td><value-of select="//slice/request_slice/response/headers/@content-type"/></td></tr>
<tr><th>//slice/request_slice/response/@status</th>
<td><value-of select="//slice/request_slice/response/@status"/></td></tr>
<tr><th>//slice/request_slice/value/request/headers/@mode</th>
<td><value-of select="//slice/request_slice/value/request/@mode"/></td></tr>
<tr><th>//slice/request_slice/value/response/headers/@content-type</th>
<td><value-of select="//slice/request_slice/value/response/headers/@content-type"/></td></tr>
<tr><th>//slice/request_slice/value/response/@status</th>
<td><value-of select="//slice/request_slice/value/response/@status"/></td></tr>
</table>
<for-each select="//slice/request_slice/*">
<for-each select="//slice/request_slice/value/*">
<ul data-request-section="{name(.)}">
<b data-testid="request-section"><value-of select='name(.)'/></b>
<for-each select="@*">
Expand Down
4 changes: 2 additions & 2 deletions stories/local-storage.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ LocalStorageLive.args =
value: '{"cherries": 12, "lemons":1 }',
tag: 'ls-live-component',
body: `<html:table xmlns:html="http://www.w3.org/1999/xhtml">
<for-each select="//slice/basket/@*">
<for-each select="//slice/basket/value/@*">
<html:tr>
<html:th><value-of select="name()"/></html:th>
<html:td><value-of select="."/></html:td>
Expand All @@ -59,7 +59,7 @@ LocalStorageLive.args =
<html:tfoot>
<html:tr>
<html:td><slot>🤔</slot></html:td>
<html:th><value-of select="sum(//slice/basket/@*)"/></html:th>
<html:th><value-of select="sum(//slice/basket/value/@*)"/></html:th>
</html:tr>
</html:tfoot>
</html:table>`
Expand Down
4 changes: 2 additions & 2 deletions stories/location-element.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function Template( { title, tag, src, slice, live } )
<template>
<location-element slice="${ slice }" ${live?'live':''} ${src?'src="https://my.example/?a=1&b=2#3"':''}></location-element>
<html:table xmlns:html="http://www.w3.org/1999/xhtml">
<for-each select="//slice/${slice}/@*">
<for-each select="//slice/${slice}/value/@*">
<html:tr>
<html:th><value-of select="name()"/></html:th>
<html:td><value-of select="."/></html:td>
Expand All @@ -46,7 +46,7 @@ function Template( { title, tag, src, slice, live } )
<html:th><u>params</u></html:th>
<html:th></html:th>
</html:tr>
<for-each select="//slice/${slice}/params/*">
<for-each select="//slice/${slice}//params/*/*">
<html:tr>
<html:th><value-of select="name()"/></html:th>
<html:td><value-of select="."/></html:td>
Expand Down
2 changes: 1 addition & 1 deletion stories/parameters.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function TemplateSlice( { title } )
<custom-element>
<template>
<attribute name="title" select="//title ?? '😃'"></attribute>
<input slice="/datadom/attributes/title" slice-update="keyup">
<input slice="/datadom/attributes/title" slice-event="keyup">
title attribute: {$title}
</template>
</custom-element>
Expand Down
50 changes: 50 additions & 0 deletions test/data-slices-mouse.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {fixture, expect, aTimeout, oneEvent} from '@open-wc/testing';

import '../src/custom-element.js';
import defaults, {
EventWithinSlice,
SliceInitializationChangeOnEvent
} from "../stories/data-slices.stories";

const defs = {};
Object.keys(defaults.argTypes).map(k => defs[k] = defaults.argTypes[k].defaultValue);
const renderStory = async (story) => fixture(story({...defs, ...story.args}));

const $ = (css, node) => node.querySelector(css);

describe('mouse events', () =>
{
it('B. event within slice', async () =>
{ // works only in manual mode with breakpoint after render
const el = await renderStory(EventWithinSlice);
const X = $('var',el);
const Y = $('code',el);
const input = $('textarea',el);
const validateValue = async (x,y)=>
{ await aTimeout(20);
expect(X.innerText).to.equal(x);
// if( Y.innerText !== y){debugger}
// expect(Y.innerText).to.equal(y);
};

await validateValue('','')
const emitXy = ( x, y, eventName ) =>
{ const ev = new MouseEvent(eventName,
{
screenX: x,
screenY: y,
clientX: x,
clientY: y,
offsetX: x,
offsetY: y,
}) ;

input.dispatchEvent( ev );
};

emitXy(10,20,'click');
await validateValue('x:10','20');
emitXy(30,40,'click');
await validateValue('x:30','40');
});
});
Loading

0 comments on commit 4f9f6e9

Please sign in to comment.