Skip to content

Commit c23f2a4

Browse files
committed
user-event/base tests
1 parent 764472f commit c23f2a4

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

AGENTS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ The project uses `yarn` for dependency management and script execution.
4141
- **Location:** Tests are located within `src`, typically co-located in `__tests__` directories.
4242
- **Setup:** `jest-setup.ts` configures the test environment. `src/index.ts` automatically configures cleanup after each test unless skipped.
4343
- **Coverage:** Collected from `src`, excluding tests.
44+
- **Organization:** Use `describe` to group test by theme. Avoid putting all tests in the same `describe` block. Avoid `describe` nesting. Avoid `describe` with only single test, make that test top-level. Prefere `test` over `it`.
4445

4546
- **Commits & Releases:**
4647
- **Commits:** Follow the **Conventional Commits** specification (e.g., `fix:`, `feat:`, `chore:`). This is enforced and used for changelog generation.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { baseSyntheticEvent } from '../base';
2+
3+
test('returns object with all required properties and default values', () => {
4+
const event = baseSyntheticEvent();
5+
6+
expect(event.currentTarget).toEqual({});
7+
expect(event.target).toEqual({});
8+
expect(event.timeStamp).toBe(0);
9+
expect(event.isDefaultPrevented?.()).toBe(false);
10+
expect(event.isPropagationStopped?.()).toBe(false);
11+
expect(event.isPersistent?.()).toBe(false);
12+
expect(typeof event.stopPropagation).toBe('function');
13+
expect(typeof event.preventDefault).toBe('function');
14+
expect(typeof event.persist).toBe('function');
15+
});
16+
17+
test('returns a new object instance on each call', () => {
18+
const event1 = baseSyntheticEvent();
19+
const event2 = baseSyntheticEvent();
20+
21+
expect(event1).not.toBe(event2);
22+
expect(event1.currentTarget).not.toBe(event2.currentTarget);
23+
expect(event1.target).not.toBe(event2.target);
24+
});
25+
26+
test('can be spread into other objects', () => {
27+
const extendedEvent = {
28+
...baseSyntheticEvent(),
29+
nativeEvent: { test: 'value' },
30+
};
31+
32+
expect(extendedEvent).toHaveProperty('currentTarget');
33+
expect(extendedEvent).toHaveProperty('preventDefault');
34+
expect(extendedEvent.nativeEvent).toEqual({ test: 'value' });
35+
});

src/user-event/event-builder/base.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import type { BaseSyntheticEvent } from 'react';
22

33
/** Builds base syntentic event stub, with prop values as inspected in RN runtime. */
4-
export function baseSyntheticEvent(): Partial<BaseSyntheticEvent<object, unknown, unknown>> {
4+
type BaseEvent = Partial<BaseSyntheticEvent<object, unknown, unknown>> & {
5+
// `isPersistent` is not a standard prop, but it's used in RN runtime. See: https://react.dev/reference/react-dom/components/common#react-event-object-methods
6+
isPersistent: () => boolean;
7+
};
8+
9+
export function baseSyntheticEvent(): BaseEvent {
510
return {
611
currentTarget: {},
712
target: {},
@@ -10,7 +15,6 @@ export function baseSyntheticEvent(): Partial<BaseSyntheticEvent<object, unknown
1015
stopPropagation: () => {},
1116
isPropagationStopped: () => false,
1217
persist: () => {},
13-
// @ts-expect-error: `isPersistent` is not a standard prop, but it's used in RN runtime. See: https://react.dev/reference/react-dom/components/common#react-event-object-methods
1418
isPersistent: () => false,
1519
timeStamp: 0,
1620
};

0 commit comments

Comments
 (0)