// noinspection DuplicatedCode import type { StoryObj } from '@storybook/web-components'; import {expect, userEvent, within} from '@storybook/test'; import '../custom-element/custom-element.js'; import '../custom-element/local-storage.js'; import {localStorage_clear, localStorage_removeItem, localStorageSetItem} from "../custom-element"; type TProps = { title: string; slice: string; key: string; value:string; live:string; body:string}; const defs: TProps = { title: '' , slice: 'ls-slice' , key: 'sb-ls-key' , value: '' , live: '' , body: `` }; type Story = StoryObj; function sleep(ms: number) { return new Promise((resolve) => setTimeout(resolve, ms)); } function render(args: TProps) { const {title, slice, key, value, live, body} = {...defs, ...args}; return `
${ title }
`; } const meta = { title: 'local-storage' , render }; export default meta; window['localStorageSetItem'] = localStorageSetItem; window['localStorage_clear'] = localStorage_clear; window['localStorage_removeItem'] = localStorage_removeItem; export const Demo:Story = { args : {title: 'live value', live:'live', body:` `} , play: async ({canvasElement}) => { localStorage_clear(); const canvas = within(canvasElement); await canvas.findByText(Demo.args!.title as string); const val = ()=> canvas.getByTestId('slice-value').textContent await userEvent.click(await canvas.findByText('set blank')); await expect(localStorage.getItem(defs.key)).toEqual(null, 'from localStorage'); await expect( val() ).toEqual(''); await userEvent.click(canvas.getByText('text value')); await sleep(10); await expect(localStorage.getItem(defs.key)).toEqual('text value', 'from localStorage'); await expect( val() ).toEqual('text value'); window['textinput'].value = 'textinput.value'; await userEvent.click(canvas.getByText('set')); await sleep(10); expect( val() ).toEqual('textinput.value'); await userEvent.click(canvas.getByText('another text')); await sleep(10); expect( val() ).toEqual('another text'); await userEvent.click(canvas.getByText('set blank')); await sleep(10); expect( val() ).toEqual(''); }, }; export const AlwaysOverride:Story = { args : {title: 'AlwaysOverride', live:'', value:'ABC', body:` buttons are changing the localStorage value, but without 'live' attribute slice ^^ from local-storage is not updated
`} , play: async ({canvasElement}) => { const canvas = within(canvasElement); await canvas.findByText(AlwaysOverride.args!.title as string); const val = ()=> canvas.getByTestId('slice-value').textContent; const click = async (text)=> await userEvent.click( await canvas.findByText(text) ); await sleep(10); await expect( localStorage.getItem(defs.key)).toEqual('ABC'); await click('set blank') await expect(localStorage.getItem(defs.key)).toEqual(null, 'from localStorage'); await sleep(10); await expect( val() ).toEqual('ABC'); await click('text value'); await sleep(10); await expect(localStorage.getItem(defs.key)).toEqual('text value', 'from localStorage'); await expect( val() ).toEqual('ABC'); await click('set blank'); await sleep(10); await expect( val() ).toEqual('ABC'); }, }; export const FromStorageWithDefault:Story = { args : {title: 'live value with defaults', live:'', value:'ABC', body:` //attr2-key: {//attr2-key}
//attr3-key: {//attr3-key}
`} , play: async ({canvasElement}) => { const canvas = within(canvasElement); await canvas.findByText(FromStorageWithDefault.args!.title as string); const byText = txt => canvas.getByText(txt); expect(localStorage.getItem(defs.key)).toEqual('ABC', 'from localStorage'); byText('clear localStorage').click(); expect( canvas.getByTestId('slice-value').textContent).toEqual('ABC') expect( canvas.getByTestId('key2-value' ).textContent).toEqual('DEF2') expect( canvas.getByTestId('key3-value' ).textContent).toEqual('DEF3') byText('update attr2-key value').click(); expect(localStorage.getItem('attr2Key')).toEqual('attr2Key value', 'from localStorage'); await sleep(10); expect( canvas.getByTestId('slice-value').textContent).toEqual('ABC') expect( canvas.getByTestId('key2-value' ).textContent).toEqual('attr2Key value') expect( canvas.getByTestId('key3-value' ).textContent).toEqual('DEF3') byText('update attr3-key value').click(); expect(localStorage.getItem('attr3Key')).toEqual('attr3Key value', 'from localStorage'); await sleep(10); expect( canvas.getByTestId('slice-value').textContent).toEqual('ABC') expect( canvas.getByTestId('key2-value' ).textContent).toEqual('attr2Key value') expect( canvas.getByTestId('key3-value' ).textContent).toEqual('attr3Key value') }, }; export const TypeAttribute:Story = { args : {title: 'local-storage type attribute variations', body:`

text-key: {//text-key }
date-key: {//date-key }
time-key: {//time-key }
local-date-time: {//local-date-time}
number-key: {//number-key }
json-key:
json-key:
{name()} : {.}
`} , play: async ({canvasElement}) => { window['JsonSample'] = {a:1,b:'B'}; const canvas = within(canvasElement); await canvas.findByText(TypeAttribute.args!.title as string); const byText = txt => canvas.getByText(txt) , val = testId => canvas.getByTestId(testId).textContent , expectVal = (key,value) => { expect(val( key ).trim()).toEqual( value, key ) }; localStorage_clear(); // cleanup before test await sleep(10); expectVal('text-key' ,''); expectVal('date-key' ,''); expectVal('time-key' ,''); expectVal('local-date-time',''); expectVal('number-key' ,'NaN'); expectVal('json-key' ,''); const input = canvasElement.querySelector('input'); input.value='ABC'; byText('set to all').click(); await sleep(10); expectVal('text-key' ,'ABC'); expectVal('date-key' ,'' ); expectVal('time-key' ,'' ); expectVal('local-date-time','' ); expectVal('number-key' ,'NaN'); expectVal('json-key' ,''); input.value='22'; byText('set to all').click(); await sleep(10); expectVal('text-key' ,'22'); expectVal('date-key' ,'' ); expectVal('time-key' ,'' ); expectVal('local-date-time','' ); expectVal('number-key' ,'22'); expectVal('json-key' ,'22'); input.value='2024'; byText('set to all').click(); await sleep(10); expectVal('text-key' ,'2024' ); expectVal('date-key' ,'2024-01-01' ); expectVal('time-key' ,'' ); expectVal('local-date-time','' ); expectVal('number-key' ,'2024' ); expectVal('json-key' ,'2024' ); input.value='2024-04-20T03:58:42.131Z'; byText('set to all').click(); await sleep(10); expectVal('text-key' ,'2024-04-20T03:58:42.131Z' ); expectVal('date-key' ,'2024-04-20' ); expectVal('time-key' ,'' ); expectVal('local-date-time','' ); expectVal('number-key' ,'NaN' ); expectVal('json-key' ,'' ); const nowStr = new Date(Date.now()).toISOString(); input.value=nowStr; byText('set to all').click(); await sleep(10); expectVal('text-key' ,nowStr ); expectVal('date-key' ,nowStr.substring(0,10) ); expectVal('time-key' ,'' ); expectVal('local-date-time','' ); expectVal('number-key' ,'NaN' ); expectVal('json-key' ,'' ); input.value='23:25'; byText('set to all').click(); await sleep(10); expectVal('text-key' ,'23:25' ); expectVal('date-key' ,'' ); expectVal('time-key' ,'23:25' ); expectVal('local-date-time','' ); expectVal('number-key' ,'NaN' ); expectVal('json-key' ,'' ); input.value='1977-04-01T14:00:30'; byText('set to all').click(); await sleep(10); expectVal('text-key' ,'1977-04-01T14:00:30' ); expectVal('date-key' ,'1977-04-01' ); expectVal('time-key' ,'' ); expectVal('local-date-time','1977-04-01T14:00:30' ); expectVal('number-key' ,'NaN' ); expectVal('json-key' ,'' ); input.value='1.23456e+5'; byText('set to all').click(); await sleep(10); expectVal('text-key' ,'1.23456e+5'); expectVal('date-key' ,'' ); expectVal('time-key' ,'' ); expectVal('local-date-time','' ); expectVal('number-key' ,'123456' ); expectVal('json-key' ,'123456' ); input.value='0001';// as YYYY for year byText('set to all').click(); await sleep(10); expectVal('text-key' ,'0001'); expectVal('date-key' ,'0001-01-01'); expectVal('time-key' ,'' ); expectVal('local-date-time','' ); expectVal('number-key' ,'1' ); expectVal('json-key' ,'' ); input.value='001'; // as 2001 year byText('set to all').click(); await sleep(10); expectVal('text-key' ,'001' ); expectVal('date-key' ,'2001-01-01'); expectVal('time-key' ,'' ); expectVal('local-date-time','' ); expectVal('number-key' ,'1' ); expectVal('json-key' ,'' ); input.value='000'; // as 2001 year byText('set to all').click(); await sleep(10); expectVal('text-key' ,'000' ); expectVal('date-key' ,'2000-01-01'); expectVal('time-key' ,'' ); expectVal('local-date-time','' ); expectVal('number-key' ,'0' ); expectVal('json-key' ,'' ); input.value='0'; // as 2000 year byText('set to all').click(); await sleep(10); expectVal('text-key' ,'0' ); expectVal('date-key' ,'2000-01-01'); expectVal('time-key' ,'' ); expectVal('local-date-time','' ); expectVal('number-key' ,'0' ); expectVal('json-key' ,'0' ); input.value='"abc"'; byText('set to all').click(); await sleep(10); expectVal('text-key' ,'"abc"' ); expectVal('date-key' ,'' ); expectVal('time-key' ,'' ); expectVal('local-date-time','' ); expectVal('number-key' ,'NaN' ); expectVal('json-key' ,'abc' ); input.value='12.345'; byText('set to all').click(); await sleep(10); expectVal('text-key' ,'12.345' ); expectVal('date-key' ,'' ); expectVal('time-key' ,'' ); expectVal('local-date-time','' ); expectVal('number-key' ,'12.345' ); expectVal('json-key' ,'12.345' ); input.value='{"a":1,"b":"B"}'; byText('set to all').click(); await sleep(10); expectVal('text-key' ,'{"a":1,"b":"B"}' ); expectVal('date-key' ,'' ); expectVal('time-key' ,'' ); expectVal('local-date-time','' ); expectVal('number-key' ,'NaN' ); expectVal('json-key' ,'a : 1b : B' ); }, }; //#region unit tests /* istanbul ignore else -- @preserve */ if( 'test' === import.meta.env.MODE && !import.meta.url.includes('skiptest') ) { const mod = await import('./local-storage.test.stories.ts?skiptest'); const { testStoryBook } = await import('./testStoryBook') const { describe } = await import('vitest') describe(meta.title, () => testStoryBook( mod, meta ) ); } //#endregion