Skip to content

Commit d430d9e

Browse files
committed
chore: fixes-for-form-fields
form-field validations were not being sent in requests.
1 parent b2291dc commit d430d9e

File tree

8 files changed

+54
-29
lines changed

8 files changed

+54
-29
lines changed
+12-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { PasswordCollector, Updater } from '@forgerock/davinci-client/types';
2+
import { dotToCamelCase } from '../helper.js';
23

34
export default function passwordComponent(
45
formEl: HTMLFormElement,
@@ -8,19 +9,21 @@ export default function passwordComponent(
89
const label = document.createElement('label');
910
const input = document.createElement('input');
1011

11-
label.htmlFor = collector.output.key;
12+
label.htmlFor = dotToCamelCase(collector.output.key);
1213
label.innerText = collector.output.label;
1314
input.type = 'password';
14-
input.id = collector.output.key;
15-
input.name = collector.output.key;
15+
input.id = dotToCamelCase(collector.output.key);
16+
input.name = dotToCamelCase(collector.output.key);
1617

1718
formEl?.appendChild(label);
1819
formEl?.appendChild(input);
1920

20-
formEl?.querySelector(`#${collector.output.key}`)?.addEventListener('blur', (event: Event) => {
21-
const error = updater((event.target as HTMLInputElement).value);
22-
if (error && 'error' in error) {
23-
console.error(error.error.message);
24-
}
25-
});
21+
formEl
22+
?.querySelector(`#${dotToCamelCase(collector.output.key)}`)
23+
?.addEventListener('blur', (event: Event) => {
24+
const error = updater((event.target as HTMLInputElement).value);
25+
if (error && 'error' in error) {
26+
console.error(error.error.message);
27+
}
28+
});
2629
}

e2e/davinci-app/components/text.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import type {
33
ValidatedTextCollector,
44
Updater,
55
} from '@forgerock/davinci-client/types';
6+
import { dotToCamelCase } from '../helper.js';
67

78
export default function usernameComponent(
89
formEl: HTMLFormElement,
910
collector: TextCollector | ValidatedTextCollector,
1011
updater: Updater,
1112
) {
12-
const collectorKey = collector.output.key;
13+
const collectorKey = dotToCamelCase(collector.output.key);
1314
const label = document.createElement('label');
1415
const input = document.createElement('input');
1516

e2e/davinci-app/helper.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function dotToCamelCase(str: string) {
2+
return str
3+
.split('.')
4+
.map((part: string, index: number) =>
5+
index === 0 ? part.toLowerCase() : part.charAt(0).toUpperCase() + part.slice(1).toLowerCase(),
6+
)
7+
.join('');
8+
}

e2e/davinci-app/tsconfig.app.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"moduleResolution": "Bundler"
66
},
77
"exclude": ["**/*.spec.ts", "**/*.test.ts"],
8-
"include": ["./main.ts", "components/**/*.ts"],
8+
"include": ["./*.ts", "components/**/*.ts"],
99
"references": [
1010
{
1111
"path": "../../packages/davinci-client/tsconfig.lib.json"

e2e/davinci-suites/src/form-fields.test.ts

+13-13
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ test('Should render form fields', async ({ page }) => {
2424
const data = parsedData.parameters.data;
2525
expect(data.actionKey).toBe('submit');
2626
expect(data.formData).toEqual({
27-
// leaving this here because it should be fixed and we would have a failing test when we do fix import { } from "// to remind us to update the test"
28-
['undefined']: '',
2927
['text-input-key']: 'This is some text',
3028
['dropdown-field-key']: '',
3129
['radio-group-key']: '',
@@ -39,29 +37,31 @@ test('should render form validation fields', async ({ page }) => {
3937
await expect(page.locator('#form')).toContainText('Form Validation');
4038
await page.getByRole('button', { name: 'Form Validation' }).click();
4139
await expect(page.getByRole('heading', { name: 'Form Fields Validation' })).toBeVisible();
42-
await expect(page.getByText('Username')).toBeVisible();
43-
await expect(page.getByRole('textbox', { name: 'Username' })).toBeVisible();
44-
await expect(page.getByText('Email Address')).toBeVisible();
45-
await expect(page.getByRole('textbox', { name: 'Email Address' })).toBeVisible();
46-
await expect(page.getByRole('button', { name: 'Submit' })).toBeVisible();
47-
await page.getByRole('textbox', { name: 'Username' }).click();
40+
4841
await page.getByRole('textbox', { name: 'Username' }).fill('sdk-user');
4942
await expect(page.getByRole('textbox', { name: 'Username' })).toHaveValue('sdk-user');
50-
await page.getByRole('textbox', { name: 'Email Address' }).click();
43+
44+
const password = page.getByRole('textbox', { name: 'Password' });
45+
await password.type('password');
46+
await expect(password).toHaveValue('password');
47+
5148
await page.getByRole('textbox', { name: 'Email Address' }).fill('[email protected]');
5249
await expect(page.getByRole('textbox', { name: 'Email Address' })).toHaveValue(
5350
5451
);
52+
5553
const requestPromise = page.waitForRequest((request) => request.url().includes('/customForm'));
5654
await page.getByRole('button', { name: 'Submit' }).click();
55+
5756
const request = await requestPromise;
5857
const parsedData = JSON.parse(request.postData());
58+
5959
const data = parsedData.parameters.data;
60+
6061
expect(data.actionKey).toBe('submit');
6162
expect(data.formData).toEqual({
62-
['undefined']: '',
63-
'user.username': '',
64-
'user.password': '',
65-
'user.email': '',
63+
'user.username': 'sdk-user',
64+
'user.password': 'password',
65+
'user.email': '[email protected]',
6666
});
6767
});

packages/davinci-client/src/lib/client.store.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,19 @@ export async function davinci({ config }: { config: DaVinciConfig }) {
136136
};
137137
}
138138

139-
if (collectorToUpdate.category !== 'SingleValueCollector') {
140-
console.error('Collector is not a SingleValueCollector and cannot be updated');
139+
if (
140+
collectorToUpdate.category !== 'SingleValueCollector' &&
141+
collectorToUpdate.category !== 'ValidatedSingleValueCollector'
142+
) {
143+
console.error(
144+
'Collector is not a SingleValueCollector or ValidatedSingleValueCollector and cannot be updated',
145+
);
141146
return function () {
142147
return {
143148
type: 'internal_error',
144149
error: {
145-
message: 'Collector is not a SingleValueCollector and cannot be updated',
150+
message:
151+
'Collector is not a SingleValueCollector or ValidatedSingleValueCollector and cannot be updated',
146152
type: 'state_error',
147153
},
148154
};
@@ -151,6 +157,7 @@ export async function davinci({ config }: { config: DaVinciConfig }) {
151157

152158
return function (value: string, index?: number) {
153159
try {
160+
console.log('the value', value);
154161
store.dispatch(nodeSlice.actions.update({ id, value, index }));
155162
return null;
156163
} catch (err) {

packages/davinci-client/src/lib/davinci.utils.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ import type { ContinueNode } from './node.types.js';
2323
export function transformSubmitRequest(node: ContinueNode): DaVinciRequest {
2424
// Filter out ActionCollectors as they are not used in form submissions
2525
const collectors = node.client?.collectors?.filter(
26-
(collector) => collector.category === 'SingleValueCollector',
26+
(collector) =>
27+
collector.category === 'SingleValueCollector' ||
28+
collector.category === 'ValidatedSingleValueCollector',
2729
);
2830

2931
const formData = collectors?.reduce<{

packages/davinci-client/src/lib/node.reducer.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ export const nodeCollectorReducer = createReducer(initialCollectorValues, (build
9292

9393
// *Some* collectors may have default or existing data to display
9494
const data = action.payload.formData[field.key];
95+
9596
// Match specific collectors
9697
switch (field.type) {
9798
case 'CHECKBOX':
@@ -165,7 +166,10 @@ export const nodeCollectorReducer = createReducer(initialCollectorValues, (build
165166
throw new Error('Value argument cannot be undefined');
166167
}
167168

168-
if (collector.category === 'SingleValueCollector') {
169+
if (
170+
collector.category === 'SingleValueCollector' ||
171+
collector.category === 'ValidatedSingleValueCollector'
172+
) {
169173
if (Array.isArray(action.payload.value)) {
170174
throw new Error('SingleValueCollector does not accept an array');
171175
}

0 commit comments

Comments
 (0)