Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright (c) Cratis. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

import 'chai/register-should.js';
import { act, createElement, type ReactNode } from 'react';
import { createRoot, type Root } from 'react-dom/client';
import { afterEach, describe, it } from 'vitest';
import { DesignsystemetCheckboxSlot, DesignsystemetRadioSlot, DesignsystemetSwitchSlot } from '../src/choice.js';

let root: Root | undefined;
let container: HTMLDivElement;

const render = async (element: ReactNode) => {
container = document.createElement('div');
document.body.append(container);
root = createRoot(container);
await act(async () => root?.render(element));
return container.querySelector('input')!;
};

afterEach(async () => {
await act(async () => root?.unmount());
container?.remove();
root = undefined;
});

const slots = [
{ name: 'checkbox', Slot: DesignsystemetCheckboxSlot },
{ name: 'radio', Slot: DesignsystemetRadioSlot },
{ name: 'switch', Slot: DesignsystemetSwitchSlot },
] as const;

for (const { name, Slot } of slots) {
describe(`when resolving ${name} part state`, () => {
it('should let explicit false public props override true input-part state', async () => {
const input = await render(createElement(Slot, {
id: 'public-choice',
name: 'example-choice',
value: 'selected',
label: 'Example choice',
checked: false,
disabled: false,
readOnly: false,
'aria-invalid': false,
onChange: () => undefined,
pt: { input: { id: 'part-choice', checked: true, disabled: true, readOnly: true, 'aria-invalid': true } },
}));

[input.checked, input.disabled, input.readOnly].should.deep.equal([false, false, false]);
input.getAttribute('aria-invalid')!.should.equal('false');
input.id.should.equal('public-choice');
container.querySelector('label')!.htmlFor.should.equal('public-choice');
container.querySelectorAll('[data-selected], [data-disabled], [data-readonly], [data-invalid]').length.should.equal(0);
});

it('should inherit native state and label association from the input part', async () => {
const input = await render(createElement(Slot, {
label: 'Example choice',
name: 'example-choice',
value: 'selected',
pt: { input: { id: 'part-choice', checked: true, disabled: true, readOnly: true, 'aria-invalid': 'true' } },
}));

[input.checked, input.disabled, input.readOnly].should.deep.equal([true, true, true]);
container.querySelector('label')!.htmlFor.should.equal('part-choice');
for (const part of container.querySelectorAll('[data-cratis-part]')) {
for (const state of ['selected', 'disabled', 'readonly', 'invalid']) {
part.hasAttribute(`data-${state}`).should.equal(true);
}
}
});

it('should preserve click order and run the part change before the value-first callback', async () => {
const calls: string[] = [];
const observed: unknown[] = [];
const input = await render(createElement(Slot, {
label: 'Example choice',
name: 'example-choice',
value: 'selected',
defaultChecked: false,
onClick: () => calls.push('public-click'),
onChange: (checked, meta) => {
calls.push('public-change');
observed.push(checked, meta?.source, meta?.nativeEvent instanceof Event);
},
pt: { input: {
onClick: () => calls.push('part-click'),
onChange: () => calls.push('part-change'),
} },
}));

await act(async () => input.click());

calls.should.deep.equal(['part-click', 'public-click', 'part-change', 'public-change']);
observed.should.deep.equal([true, 'user', true]);
input.checked.should.equal(true);
container.querySelector('[data-cratis-part="root"]')!.hasAttribute('data-selected').should.equal(true);
});

it('should prevent read-only user changes', async () => {
let changes = 0;
const input = await render(createElement(Slot, {
label: 'Example choice',
name: 'example-choice',
value: 'selected',
defaultChecked: false,
readOnly: true,
onChange: () => { changes += 1; },
}));

await act(async () => input.click());

changes.should.equal(0);
input.checked.should.equal(false);
container.querySelectorAll('[data-selected]').length.should.equal(0);
});
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright (c) Cratis. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

import 'chai/register-should.js';
import { act, createElement, type ReactNode } from 'react';
import { createRoot, type Root } from 'react-dom/client';
import { afterEach, describe, it } from 'vitest';
import type { TextAreaProps, TextInputProps } from '@cratis/components/Common';
import { DesignsystemetTextAreaSlot, DesignsystemetTextInputSlot } from '../src/fields.js';

let root: Root | undefined;
let container: HTMLDivElement;

const render = async (element: ReactNode) => {
container = document.createElement('div');
document.body.append(container);
root = createRoot(container);
await act(async () => root?.render(element));
return container.querySelector<HTMLInputElement | HTMLTextAreaElement>('input, textarea')!;
};

afterEach(async () => {
await act(async () => root?.unmount());
container?.remove();
root = undefined;
});

const fields = [
{
name: 'text input',
Slot: (props: TextInputProps & TextAreaProps) => createElement(DesignsystemetTextInputSlot, props),
prototype: HTMLInputElement.prototype,
},
{
name: 'text area',
Slot: (props: TextInputProps & TextAreaProps) => createElement(DesignsystemetTextAreaSlot, props),
prototype: HTMLTextAreaElement.prototype,
},
] as const;

for (const { name, Slot, prototype } of fields) {
describe(`when resolving ${name} part state`, () => {
it('should let explicit false public props override true root-part state', async () => {
const field = await render(createElement(Slot, {
disabled: false,
readOnly: false,
'aria-invalid': false,
pt: { root: { disabled: true, readOnly: true, 'aria-invalid': true } },
}));

[field.disabled, field.readOnly].should.deep.equal([false, false]);
field.getAttribute('aria-invalid')!.should.equal('false');
container.querySelectorAll('[data-disabled], [data-readonly], [data-invalid]').length.should.equal(0);
});

it('should inherit native state from the root part', async () => {
const field = await render(createElement(Slot, {
pt: { root: { disabled: true, readOnly: true, 'aria-invalid': 'true' } },
}));

[field.disabled, field.readOnly].should.deep.equal([true, true]);
for (const state of ['disabled', 'readonly', 'invalid']) {
field.getAttribute(`data-${state}`)!.should.equal('true');
}
});

it('should keep canonical invalid state even when aria-invalid is explicitly false', async () => {
const field = await render(createElement(Slot, { invalid: true, 'aria-invalid': false }));

field.getAttribute('aria-invalid')!.should.equal('false');
field.getAttribute('data-invalid')!.should.equal('true');
});

it('should merge root styling before public styling', async () => {
const field = await render(createElement(Slot, {
className: 'public-field',
style: { color: 'blue' },
pt: { root: { className: 'part-field', style: { color: 'red', margin: '2px' } } },
}));

field.classList.contains('part-field').should.equal(true);
field.classList.contains('public-field').should.equal(true);
field.style.color.should.equal('blue');
field.style.margin.should.equal('2px');
});

it('should call the root handler before the public value-first handler with the native event', async () => {
const calls: string[] = [];
const observed: unknown[] = [];
const field = await render(createElement(Slot, {
defaultValue: 'before',
onChange: (value, meta) => {
calls.push('public');
observed.push(value, meta?.source, meta?.nativeEvent instanceof Event);
},
pt: { root: { onChange: () => calls.push('part') } },
}));

await act(async () => {
Object.getOwnPropertyDescriptor(prototype, 'value')!.set!.call(field, 'after');
field.dispatchEvent(new Event('input', { bubbles: true }));
});

calls.should.deep.equal(['part', 'public']);
observed.should.deep.equal(['after', 'user', true]);
});
});
}
Loading
Loading