Skip to content

Commit

Permalink
Migrate all key to symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyalIcing committed Feb 2, 2024
1 parent 012e9e5 commit da126a7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
11 changes: 8 additions & 3 deletions src/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { screen, waitFor } from '@testing-library/dom';
import user from '@testing-library/user-event';
import { AllDescriptor, RoleDescriptor, optional } from './roles/types';
import {
type AllDescriptor,
type RoleDescriptor,
allKey,
optionalKey,
} from './roles/types';

export type DescriptorResult<T> = T extends RoleDescriptor & AllDescriptor
? Array<HTMLElement>
Expand Down Expand Up @@ -30,10 +35,10 @@ export function resolverFor(
<D extends RoleDescriptor>(
descriptor: RoleDescriptor | (RoleDescriptor & AllDescriptor)
): D extends AllDescriptor ? ReadonlyArray<HTMLElement> : HTMLElement => {
if ('all' in descriptor && descriptor.all === true) {
if (allKey in descriptor) {
const { name } = descriptor;
return source.queryAllByRole(descriptor.role, { name }) as any;
} else if (optional in descriptor) {
} else if (optionalKey in descriptor) {
const { name, isSelected: selected } = descriptor;
return source.queryByRole(descriptor.role, { name, selected }) as any;
} else {
Expand Down
13 changes: 9 additions & 4 deletions src/roles/shared.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { prettyDOM } from '@testing-library/dom';
import { optional, type AllDescriptor, type RoleDescriptor } from './types';
import {
type AllDescriptor,
type RoleDescriptor,
allKey,
optionalKey,
} from './types';

export type RoleObject<
Role extends string,
Expand All @@ -23,10 +28,10 @@ export function role<
role,
name,
get all(): RoleDescriptor & AllDescriptor {
return Object.create(this, { all: { value: true } });
return Object.create(this, { [allKey]: { value: true } });
},
get optional() {
return Object.create(this, { [optional]: { value: true } });
return Object.create(this, { [optionalKey]: { value: true } });
},
get click(): RoleDescriptor {
return Object.create(this, { event: { value: 'click' } });
Expand All @@ -42,7 +47,7 @@ export function roleSelectable(role: string, name?: string | RegExp) {
return Object.create(this, { isSelected: { value: true } });
},
get all(): RoleDescriptor & AllDescriptor {
return Object.create(this, { all: { value: true } });
return Object.create(this, { [allKey]: { value: true } });
},
});
}
Expand Down
14 changes: 9 additions & 5 deletions src/roles/types.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import { queries, BoundFunctions, Queries } from '@testing-library/dom';
import {
queries,
type BoundFunctions,
type Queries,
} from '@testing-library/dom';

const all = Symbol('all');
export const optional = Symbol('optional');
export const allKey = Symbol('all');
export const optionalKey = Symbol('optional');

export interface RoleDescriptor {
readonly role: string;
readonly name?: string | RegExp;
readonly [optional]?: true;
readonly [optionalKey]?: true;
readonly isSelected?: boolean;
readonly event?: 'click';
}

export interface AllDescriptor {
readonly all: true;
readonly [allKey]?: true;
}

export interface SingleDescriptor<Q extends Queries = typeof queries> {
Expand Down

0 comments on commit da126a7

Please sign in to comment.