// noinspection DuplicatedCode import type { StoryObj } from '@storybook/web-components'; import {expect, fireEvent, userEvent, within} from '@storybook/test'; import '../custom-element/custom-element.js'; import {click} from "webdriverio/build/commands/element/click"; type TProps = { title: string; tag: string; template: string; payload: string }; type Story = StoryObj; function render(args: TProps) { const { title, tag, template, payload } = args; return `
${title} ${payload}
`; } const meta = { title: 'xslt/for-each', render, parameters: { test: { dangerouslyIgnoreUnhandledErrors: true, }, }, }; export default meta; const Head6Table = `
cem-color-hue-variant
Token Hue Variant Hex Label Intended use
--cem-color-blue-xl blue xl #faf9fd Lightest blue Comfort/trust emotion (light scene)
--cem-color-blue-l blue l #d7e3ff Light blue Trust palette, attention semantic
--cem-color-blue-d blue d #002f65 Dark blue Trust palette (dark theme)
--cem-color-blue-xd blue xd #1a1b1f Darkest blue Comfort/trust emotion (dark scene)
--cem-color-brown-xl brown xl #d7ccc8 Extra light brown Conservative palette (light theme)
--cem-color-brown-l brown l #a1887f Light brown Conservative emotion, brand-3
--cem-color-brown-d brown d #4e342e Dark brown Conservative palette (dark theme)
--cem-color-brown-xd brown xd #3e2723 Extra dark brown Conservative palette (dark theme, extreme)
--cem-color-cyan-xl cyan xl #f1fefe Extra light cyan Used for comfort palette (light scenes)
--cem-color-cyan-l cyan l #00fbfb Light cyan Calm palette, creativity accent
--cem-color-cyan-d cyan d #006a6a Dark cyan Calm palette (dark theme)
--cem-color-cyan-xd cyan xd #001010 Extra dark cyan Used for comfort palette (dark scenes)
--cem-color-grey-l grey l #f1f1eb Light grey Conservative palette, neutral backgrounds
--cem-color-grey-d grey d #1a1c18 Dark grey Conservative palette (dark theme)
--cem-color-orange-xl orange xl #f0f070 Extra light yellow Enthusiasm palette (light theme)
--cem-color-orange-l orange l #ffe082 Light orange Enthusiasm emotion, brand-2
--cem-color-orange-d orange d #723600 Dark orange Enthusiasm hype (dark theme)
--cem-color-orange-xd orange xd #502400 Extra dark orange Enthusiasm palette (dark theme, extreme)
--cem-color-purple-xl purple xl #f3e5f5 Extra light purple Creativity palette (light theme)
--cem-color-purple-l purple l #e1bee7 Light purple Creativity emotion, brand-1
--cem-color-purple-d purple d #6a1b9a Dark purple Creativity palette (dark theme)
--cem-color-purple-xd purple xd #4a148c Extra dark purple Creativity palette (dark theme, extreme)
--cem-color-red-xl red xl #ffb4ab Extra light red Danger palette (light theme)
--cem-color-red-l red l #ba1a1a Light red Danger emotion, alert semantic
--cem-color-red-d red d #690005 Dark red Danger palette (dark theme)
--cem-color-red-xd red xd #410002 Extra dark red Danger palette (dark theme, extreme)
`; export const ForEach: Story = { args: { title: 'KNOWN ISSUE: Multiple IF blocks - out of order', tag: 'multi-for-each', template: `
${Head6Table}
{./*[1]}
`, payload: ` ` }, play: async ({ canvasElement }) => { const canvas = within(canvasElement); expect( await await canvas.findByTestId('color-1')).toBeInTheDocument(); expect( await await canvas.findByText('--cem-color-blue-l')).toBeInTheDocument(); expect( await await canvas.findByTestId('color-26')).toBeInTheDocument(); expect( await await canvas.findByText('--cem-color-red-xd')).toBeInTheDocument(); } }; export const ForEach_0_2_N: Story = { args: { title: 'initially none, on check - all items in order 0 to N. N should be in the end', tag: 'for-each-0-to-n', template: `
${Head6Table}
0
{position()} {./*[1]}
N
`, payload: ` ` }, play: async ({ canvasElement }) => { const canvas = within(canvasElement); const container = await canvas.findByTestId('whole-text'); await expect(container.textContent).toMatch(/0\s+N/); await fireEvent.click(await canvas.findByTestId('toggle-all')); await canvas.findByText('1--cem-color-blue-xl'); await expect(await canvas.findByText('1--cem-color-blue-xl')).toBeInTheDocument(); // should start with 0 and end with N await expect(container.textContent).toMatch(/0\s+1--cem-color-blue-xl[\s\S]*26--cem-color-red-xd\s+N/); } }; const Table3x3Data = ` A1A2A3 B1B2B3 C1C2C3 `; export const ForEach_Table_3x3: Story = { args: { title: 'Table 3x3: initially empty, rows shown by checkbox, content stays inside table', tag: 'for-each-table-3x3', template: ` ${Table3x3Data}
JUST_TEXT_BEFORE
    
        
            {.} #
        
    
JUST_TEXT_AFTER
TEXT_BEFORE Col ACol BCol C {.} TEXT_AFTER
`, payload: ` ` }, play: async ({ canvasElement }) => { const canvas = within(canvasElement); const justTextContainer = await canvas.findByTestId('just-text'); expect(justTextContainer.textContent).toMatch(/JUST_TEXT_BEFORE[\s\S]*JUST_TEXT_AFTER/); const container = await canvas.findByTestId('container'); const table = await canvas.findByTestId('the-table'); const tbody = await canvas.findByTestId('table-body'); // Initially no rows visible expect(tbody.querySelectorAll('tr').length).toBe(0); // TEXT_BEFORE ... TEXT_AFTER with table in between (but no row content) expect(container.textContent).toMatch(/TEXT_BEFORE[\s\S]*Col A[\s\S]*TEXT_AFTER/); // Toggle checkbox to show rows await fireEvent.click(await canvas.findByTestId('toggle-rows')); // Now 3 rows should be visible expect(await canvas.findByTestId('row-1')).toBeInTheDocument(); expect(await canvas.findByTestId('row-2')).toBeInTheDocument(); expect(await canvas.findByTestId('row-3')).toBeInTheDocument(); expect(tbody.querySelectorAll('tr').length).toBe(3); expect(justTextContainer.textContent).toMatch(/JUST_TEXT_BEFORE[\s\S]*A1 #/); expect(justTextContainer.textContent).toMatch(/C3 #[\s\S]*JUST_TEXT_AFTER/); // Verify rows are inside the table (TR should be children of TBODY) const rows = tbody.querySelectorAll('tr'); rows.forEach(row => { expect(row.parentElement).toBe(tbody); }); // Verify table content is between TEXT_BEFORE and TEXT_AFTER expect(container.textContent).toMatch(/TEXT_BEFORE[\s\S]*?Col A[\s\S]*?Col B[\s\S]*?Col C[\s\S]*?A1[\s\S]*?A2[\s\S]*?A3[\s\S]*?B1[\s\S]*?B2[\s\S]*?B3[\s\S]*?C1[\s\S]*?C2[\s\S]*?C3[\s\S]*?TEXT_AFTER/); // Verify no content leaked outside the table const tableHtml = table.outerHTML; expect(tableHtml).toContain('A1'); expect(tableHtml).toContain('C3'); } }; //#region unit tests /* istanbul ignore else -- @preserve */ if ('test' === import.meta.env.MODE && !import.meta.url.includes('skiptest')) { const mod = await import('./xslt-if.test.stories.ts?skiptest'); const { testStoryBook } = await import('./testStoryBook'); const { describe } = await import('vitest'); describe(meta.title, () => testStoryBook(mod, meta)); } //#endregion