Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | 9x 9x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | // noinspection DuplicatedCode import type { StoryObj } from '@storybook/web-components'; import {expect, userEvent, within, spyOn} from '@storybook/test'; import '../custom-element/custom-element.js'; type TProps = { title: string; body:string}; type Story = StoryObj<TProps>; function sleep(ms: number) { return new Promise((resolve) => setTimeout(resolve, ms)); } function render(args: TProps) { const {title, body} = args; return ` <fieldset> <legend>${ title }</legend> ${ body } </fieldset> `; } const meta = { title: 'slice-events' , render }; export default meta; export const SliceInitChangeEvent:Story = { args : {title: 'Slice initialization, change on event', body:` <p>initial value should be 0; + and - should change the number in input field</p> <custom-element> <template> <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 data-testid="slice-value">{ //clickcount }</code> </template> </custom-element> `} , play: async ({canvasElement}) => { const titleText = SliceInitChangeEvent.args!.title as string; const canvas = within(canvasElement); await canvas.findByText(titleText); const code = await canvas.findByTestId('slice-value'); const input = await canvasElement.querySelector('[type="number"]') as HTMLInputElement; await expect(code).toBeInTheDocument(); expect(code.textContent).to.equal('0', 'initial slot value 0'); expect(input.value).to.equal('0', 'initial input value 0'); canvasElement.querySelector('[slice-value="//clickcount + 1"]').click() await sleep(10); expect(code.textContent).to.equal('1', 'increment to 1'); expect(input.value).to.equal('1', 'increment input 1'); canvasElement.querySelector('[slice-value="//clickcount + 1"]').click(); await sleep(10); canvasElement.querySelector('[slice-value="//clickcount + 1"]').click(); await sleep(10); expect(code.textContent).to.equal('3', 'double increment to 3'); expect(input.value).to.equal('3', 'double increment input to 3'); canvasElement.querySelector('[slice-value="//clickcount - 1"]').click(); await sleep(10); expect(code.textContent).to.equal('2', 'decrement to 2'); expect(input.value).to.equal('2', 'decrement input to 2'); }, }; export const RealtimeEventInSlice:Story = { args : {title: 'Realtime Slice data on event', body:` <p>move the mouse over TEXTAREA and click to see slice and slice event changed</p> <custom-element> <template> <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 : <code data-testid="//slice/s" >{ //slice/s }</code> <br/> //slice/s/event/@offsetY : <code data-testid="//slice/s/event/@offsetY" >{ //slice/s/event/@offsetY }</code> <br/> event type : <code data-testid="//slice/s/event/@type" >{ //slice/s/event/@type }</code> </template> </custom-element> `} , play: async ({canvasElement}) => { const titleText = RealtimeEventInSlice.args!.title as string; const canvas = within(canvasElement); await canvas.findByText(titleText); const sliceText = ()=> canvas.getByTestId('//slice/s').textContent , offsetY = ()=> canvas.getByTestId('//slice/s/event/@offsetY').textContent , eventType = ()=> canvas.getByTestId('//slice/s/event/@type').textContent; const input = await canvasElement.querySelector('textarea'); // expect(input).toBeInTheDocument(); expect( sliceText() ).to.equal('', 'initial slot value blank'); expect( offsetY() ).to.equal('', 'initial slot offsetY blank'); expect( eventType() ).to.equal('', 'initial slot event blank'); 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(20,20,'click'); await sleep(10); expect( sliceText() ).to.equal('x:20', 'click slot value 20'); expect( Number(offsetY()) ).to.be.lessThan(0, 'offsetY click'); expect( eventType() ).to.equal('click', 'click event type'); }, }; export const DoubleEventInSlice:Story = { args : {title: 'slice-event="change submit change submit" ', body:` <p> double same event should be treated as one.</p> <custom-element> <template> <form slice-event="submit submit change change" slice="form-1"> <input slice-event="change change" required slice="field-1" data-testid="f1" name="f1"/> <input name="f2" value="populated in form-data"/> <button>next</button> <code>slices count {count(/datadom/slice/*)}</code> </form> </template> </custom-element> `} , play: async ({canvasElement}) => { const canvas = within(canvasElement); const input = await canvas.findByTestId('f1'); input.focus(); await userEvent.type ( input, 'AB'); canvas.getByRole('button').focus() await userEvent.clear( input ); await userEvent.click( canvas.getByRole('button')); expect( await canvas.findByText('slices count 2')).toBeInTheDocument(); }, }; export const MultipleSlices:Story = { args : {title: 'slice="/datadom/attributes/emotion | s1" ', body:` <p> double same event should be treated as one.</p> <custom-element> <template> <input slice="s1|s2" slice-event="input" data-testid="f1" /><br/> Type to update s1 and s2 slices <br/> slice <code>s1: {//slice/s1}</code><br/> slice <code>s2: {//slice/s2}</code><br/> </template> </custom-element> `} , play: async ({canvasElement}) => { const canvas = within(canvasElement); const input = await canvas.findByTestId('f1'); input.focus(); await userEvent.type ( input, 'AB'); await expect( await canvas.findByText('s1: AB')).toBeInTheDocument(); await expect( await canvas.findByText('s2: AB')).toBeInTheDocument(); }, }; export const SlicesInAttrAndName:Story = { args : {title: 'slice="/datadom/attributes/emotion | s1" ', body:` <p> double same event should be treated as one.</p> <custom-element> <template> <attribute name="emotion">😃</attribute> <input slice-event="input" slice="/datadom/attributes/emotion | s1" data-testid="f1"/> <p>Type to update </p> <p>emotion attribute: {emotion}</p> <p>slice: {//slice/s1}</p> </template> </custom-element> `} , play: async ({canvasElement}) => { const canvas = within(canvasElement); const input = await canvas.findByTestId('f1'); input.focus(); await userEvent.type ( input, 'AB'); await expect( await canvas.findByText( 'emotion attribute: AB')).toBeInTheDocument(); }, }; export const CheckboxChecked:Story = { args : {title: 'Checkbox value in slice ', body:` <p> Checked value propagated into slice</p> <custom-element> <template> <input type="checkbox" slice="s1" value="V1" checked/> <p>slice: {//s1}</p> </template> </custom-element> `} , play: async ({canvasElement}) => { const canvas = within(canvasElement); await expect( await canvas.findByText( 'slice: V1')).toBeInTheDocument(); }, }; export const CheckboxUnchecked:Story = { args : {title: 'UncheckedCheckbox value not in slice ', body:` <p> Check to see the value propagated into slice. Uncheck to observe the empty string in the slice. </p> <custom-element> <template> <input type="checkbox" slice="s1" value="V1" data-testid="i1"/> <p data-testid="t1">slice: {//s1}</p> </template> </custom-element> `} , play: async ({canvasElement}) => { const canvas = within(canvasElement); const p =await canvas.findByTestId( 't1'); await expect(p.textContent ).toEqual("slice: "); const cb = await canvas.findByTestId( 'i1'); await userEvent.click ( cb ); await expect( await canvas.findByText('slice: V1')).toBeInTheDocument(); await userEvent.click ( cb ); await expect( p.textContent).toEqual('slice: '); }, }; export const CheckboxSliceValue:Story = { args : {title: 'UncheckedCheckbox slice-value not in slice ', body:` <p> Check to see the value propagated into slice. Uncheck to observe the empty string in the slice. </p> <custom-element> <template> <input type="checkbox" slice="s1" slice-value="'V1'" data-testid="i1"/> <p data-testid="t1">slice: {//s1}</p> </template> </custom-element> `} , play: async ({canvasElement}) => { const canvas = within(canvasElement); const p =await canvas.findByTestId( 't1'); await expect(p.textContent ).toEqual("slice: "); const cb = await canvas.findByTestId( 'i1'); await userEvent.click ( cb ); await expect( await canvas.findByText('slice: V1')).toBeInTheDocument(); await userEvent.click ( cb ); await expect( p.textContent).toEqual('slice: '); }, }; export const RadiogroupSliceValue:Story = { args : {title: 'Radiogroup value', body:` <p> The value propagated into slice from the last checked radiobutton. </p> <custom-element> <template> <p>V1: <input type="radio" slice="s1" value="V1" data-testid="i1" name="group1"/></p> <p>V2: <input type="radio" slice="s1" value="V2" data-testid="i2" name="group1"/></p> <p data-testid="t1">slice: {//s1}</p> </template> </custom-element> `} , play: async ({canvasElement}) => { const canvas = within(canvasElement); await userEvent.click ( await canvas.findByTestId( 'i1') ); await expect( await canvas.findByText('slice: V1')).toBeInTheDocument(); await userEvent.click ( await canvas.findByTestId( 'i2') ); await expect( await canvas.findByText('slice: V2')).toBeInTheDocument(); }, }; //#region unit tests /* istanbul ignore else -- @preserve */ if( 'test' === import.meta.env.MODE && !import.meta.url.includes('skiptest') ) { const mod = await import('./slice-events.test.stories.ts?skiptest'); const { testStoryBook } = await import('./testStoryBook') const { describe } = await import('vitest') describe(meta.title, () => testStoryBook( mod, meta ) ); } //#endregion |