mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2026-03-13 10:22:08 +08:00
97 lines
2.9 KiB
TypeScript
97 lines
2.9 KiB
TypeScript
import { newSpecPage } from '@stencil/core/testing';
|
|
import { getAriaLabel } from '../helpers';
|
|
import { Item } from '../../components/item/item.tsx';
|
|
import { Label } from '../../components/label/label.tsx';
|
|
import { Toggle } from '../../components/toggle/toggle.tsx';
|
|
|
|
describe('getAriaLabel()', () => {
|
|
it('should correctly link component to label', async () => {
|
|
const page = await newSpecPage({
|
|
components: [Item, Label, Toggle],
|
|
html: `
|
|
<ion-item>
|
|
<ion-label>My Label</ion-label>
|
|
<ion-toggle></ion-toggle>
|
|
</ion-item>
|
|
`
|
|
});
|
|
|
|
const toggle = page.body.querySelector('ion-toggle');
|
|
|
|
const { label, labelId, labelText } = getAriaLabel(toggle, 'ion-tg-0');
|
|
|
|
expect(labelText).toEqual('My Label');
|
|
expect(labelId).toEqual('ion-tg-0-lbl');
|
|
expect(label).toEqual(page.body.querySelector('ion-label'));
|
|
});
|
|
|
|
it('should correctly link component when using custom label', async () => {
|
|
const page = await newSpecPage({
|
|
components: [Toggle],
|
|
html: `
|
|
<div id="my-label">Hello World</div>
|
|
<ion-toggle aria-labelledby="my-label"></ion-toggle>
|
|
`
|
|
});
|
|
|
|
const toggle = page.body.querySelector('ion-toggle');
|
|
|
|
const { label, labelId, labelText } = getAriaLabel(toggle, 'ion-tg-0');
|
|
|
|
expect(labelText).toEqual('Hello World');
|
|
expect(labelId).toEqual('my-label');
|
|
expect(label).toEqual(page.body.querySelector('#my-label'));
|
|
});
|
|
|
|
it('should correctly link component when special characters are used', async () => {
|
|
const page = await newSpecPage({
|
|
components: [Toggle],
|
|
html: `
|
|
<div id="id.1">Hello World</div>
|
|
<ion-toggle aria-labelledby="id.1"></ion-toggle>
|
|
`
|
|
});
|
|
|
|
const toggle = page.body.querySelector('ion-toggle');
|
|
|
|
const { label, labelId, labelText } = getAriaLabel(toggle, 'ion-tg-0');
|
|
|
|
expect(labelText).toEqual('Hello World');
|
|
expect(labelId).toEqual('id.1');
|
|
});
|
|
|
|
it('should only set the label id if one was not set already', async () => {
|
|
const page = await newSpecPage({
|
|
components: [Toggle],
|
|
html: `
|
|
<label id="my-id" for="id.1">Hello World</label>
|
|
<ion-toggle id="id.1"></ion-toggle>
|
|
`
|
|
});
|
|
|
|
const toggle = page.body.querySelector('ion-toggle');
|
|
|
|
const { label, labelId, labelText } = getAriaLabel(toggle, 'ion-tg-0');
|
|
|
|
expect(labelText).toEqual('Hello World');
|
|
expect(labelId).toEqual('my-id');
|
|
});
|
|
|
|
it('should set label id', async () => {
|
|
const page = await newSpecPage({
|
|
components: [Toggle],
|
|
html: `
|
|
<label for="id.1">Hello World</label>
|
|
<ion-toggle id="id.1"></ion-toggle>
|
|
`
|
|
});
|
|
|
|
const toggle = page.body.querySelector('ion-toggle');
|
|
|
|
const { label, labelId, labelText } = getAriaLabel(toggle, 'ion-tg-0');
|
|
|
|
expect(labelText).toEqual('Hello World');
|
|
expect(labelId).toEqual('id.1-lbl');
|
|
});
|
|
});
|