Skip to content

Commit

Permalink
fix: example with nestedtabs
Browse files Browse the repository at this point in the history
  • Loading branch information
Kezhik Kyzyl-ool committed Mar 7, 2024
1 parent 46602e8 commit 05e1b0e
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions test/runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {callPlugin, tokenize} from './utils';
import Token from 'markdown-it/lib/token';
import {DOMWindow, JSDOM} from "jsdom";
import {TabsController} from "../src/runtime/TabsController";
import {GROUP_DATA_KEY, TAB_CLASSNAME, TABS_LIST_CLASSNAME} from "../src/common";

const defaultContent = `
{% list tabs group=g0 %}
Expand Down Expand Up @@ -46,10 +47,6 @@ const defaultContent = `
Contents of nested tab 2
- Nested tab 3
Contents of nested tab 3
{% endlist %}
{% endlist %}
Expand All @@ -67,6 +64,7 @@ describe('Testing runtime features', () => {
let dom: JSDOM;
let window: DOMWindow;
let tabs: NodeListOf<HTMLElement>;
let nestedTabs: NodeListOf<HTMLElement>;

beforeEach(() => {
const {tokens, env, md} = makeTransform();
Expand All @@ -78,7 +76,8 @@ describe('Testing runtime features', () => {
new TabsController(window.document);
window.document.body.append(fragment);

tabs = window.document.querySelectorAll('.yfm-tab');
tabs = window.document.querySelectorAll(`[${GROUP_DATA_KEY}="g0"] > .${TABS_LIST_CLASSNAME} > .${TAB_CLASSNAME}`);
nestedTabs = window.document.querySelectorAll(`[${GROUP_DATA_KEY}="g1"] > .${TABS_LIST_CLASSNAME} > .${TAB_CLASSNAME}`);

if (!tabs.length) {
throw new Error('No tabs found');
Expand Down Expand Up @@ -179,4 +178,36 @@ describe('Testing runtime features', () => {
expect(tabs[2].classList.contains('active')).not.toBeTruthy();
expect(window.document.activeElement).toBe(fakeButton);
});

test.each(['ArrowRight', 'ArrowLeft'])('roving tabindex works on nested tabs when pressing right/left arrow keys', (key) => {
tabs[2].click();
nestedTabs[0].click();
nestedTabs[0].focus();

expect(nestedTabs[0].classList.contains('active')).toBeTruthy();
expect(nestedTabs[1].classList.contains('active')).not.toBeTruthy();
expect(tabs[0].classList.contains('active')).not.toBeTruthy();
expect(tabs[1].classList.contains('active')).not.toBeTruthy();
expect(tabs[2].classList.contains('active')).toBeTruthy();

let keyDownEvent = new window.KeyboardEvent('keydown', {key});
keyDownEvent.initEvent('keydown', true, true);
nestedTabs[0].dispatchEvent(keyDownEvent);

expect(nestedTabs[0].classList.contains('active')).not.toBeTruthy();
expect(nestedTabs[1].classList.contains('active')).toBeTruthy();
expect(window.document.activeElement).toBe(nestedTabs[1]);
expect(tabs[0].classList.contains('active')).not.toBeTruthy();
expect(tabs[1].classList.contains('active')).not.toBeTruthy();
expect(tabs[2].classList.contains('active')).toBeTruthy();

nestedTabs[1].dispatchEvent(keyDownEvent);

expect(nestedTabs[0].classList.contains('active')).toBeTruthy();
expect(nestedTabs[1].classList.contains('active')).not.toBeTruthy();
expect(window.document.activeElement).toBe(nestedTabs[0]);
expect(tabs[0].classList.contains('active')).not.toBeTruthy();
expect(tabs[1].classList.contains('active')).not.toBeTruthy();
expect(tabs[2].classList.contains('active')).toBeTruthy();
});
});

0 comments on commit 05e1b0e

Please sign in to comment.