forked from BeOnAuto/auto-engineer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting.ts
More file actions
103 lines (93 loc) · 3.5 KB
/
testing.ts
File metadata and controls
103 lines (93 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { recordGwtSpec, recordGiven, recordWhen, recordWhenEvents, recordThen } from './flow-context';
function ensureMessageFormat(item: unknown): { type: string; data: Record<string, unknown> } {
if (typeof item !== 'object' || item === null) {
throw new Error('Invalid message format');
}
const obj = item as Record<string, unknown>;
if ('type' in obj && typeof obj.type === 'string' && 'data' in obj && typeof obj.data === 'object') {
return { type: obj.type, data: obj.data as Record<string, unknown> };
}
if ('__messageCategory' in obj && typeof obj.type === 'string') {
const { type, ...rest } = obj;
return {
type,
data: rest as Record<string, unknown>,
};
}
throw new Error('Message must have type and data properties');
}
export const createFlowSpec = () => {
return {
given: (events: unknown[]) => {
recordGwtSpec();
const formattedEvents = events.map(ensureMessageFormat);
recordGiven(formattedEvents);
return {
when: (command: unknown) => {
const formattedCommand = ensureMessageFormat(command);
recordWhen(formattedCommand);
return {
then: (expectedEvents: unknown[]) => {
const formattedEvents = expectedEvents.map(ensureMessageFormat);
recordThen(...formattedEvents);
},
thenThrows: (_errorMatcher: (error: Error) => boolean) => {
// Handle error cases
recordThen({
type: 'Error',
data: { errorType: 'IllegalStateError', message: 'Error occurred' },
});
},
};
},
then: (expectedData: unknown | unknown[]) => {
// For query slices - given events, then state
const items = Array.isArray(expectedData) ? expectedData : [expectedData];
const formattedData = items.map(ensureMessageFormat);
recordThen(...formattedData);
},
};
},
when: (commandOrEvents: unknown | unknown[]) => {
recordGwtSpec();
if (Array.isArray(commandOrEvents)) {
// React slice - when events
const formattedEvents = commandOrEvents.map(ensureMessageFormat);
recordWhenEvents(formattedEvents);
return {
then: (expectedCommands: unknown[]) => {
const formattedCommands = expectedCommands.map(ensureMessageFormat);
recordThen(...formattedCommands);
},
};
} else {
// Command slice - when command
const formattedCommand = ensureMessageFormat(commandOrEvents);
recordWhen(formattedCommand);
return {
then: (expectedEvents: unknown[]) => {
const formattedEvents = expectedEvents.map(ensureMessageFormat);
recordThen(...formattedEvents);
},
thenThrows: (_errorMatcher: (error: Error) => boolean) => {
recordThen({
type: 'Error',
data: { errorType: 'IllegalStateError', message: 'Error occurred' },
});
},
};
}
},
};
};
export const given = (events: unknown[]) => createFlowSpec().given(events);
export const when = (commandOrEvents: unknown | unknown[]) => createFlowSpec().when(commandOrEvents);
// GraphQL query testing helper
export const gqlQuery = (query: string) => ({
query,
then: (_expectedResponse: unknown) => {},
});
// Alternative when function for GraphQL queries
export const whenQuery = (_query: unknown) => ({
then: (_expectedResponse: unknown) => {},
});