Skip to content

Commit

Permalink
chore: ignore default group nodes in form state parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-jonas committed Nov 25, 2024
1 parent 68b9d93 commit 9aad2df
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/elements-react/src/components/form/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ export function OryForm({ children, onAfterSubmit }: OryFormProps) {
break
}
}
methods.setValue("password", "")
onAfterSubmit?.(data.method)
}

Expand Down
61 changes: 60 additions & 1 deletion packages/elements-react/src/context/form-state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ test(`should parse state from flow on "action_flow_update" provide_identifier`,
expect(state).toEqual({ current: "provide_identifier" })
})

test(`should parse state from flow on "action_flow_update" provide_identifier`, () => {
test(`should parse state from flow on "action_flow_update" if password has validation error`, () => {
const { result } = renderHook(() => useFormStateReducer(init))
const [, dispatch] = result.current

Expand Down Expand Up @@ -152,6 +152,65 @@ test(`should parse state from flow on "action_flow_update" provide_identifier`,
expect(state).toEqual({ current: "method_active", method: "password" })
})

test(`should ignore validation message on default group nodes`, () => {
const { result } = renderHook(() => useFormStateReducer(init))
const [, dispatch] = result.current

const mockFlow = {
flowType: FlowType.Login,
flow: {
active: "",
ui: {
nodes: [
{
attributes: {
autocomplete: "email",
disabled: false,
name: "traits.email",
node_type: "input",
required: true,
type: "email",
value: "fdghj",
},
group: "default",
messages: [
{
context: {
reason: '"fdghj" is not valid "email"',
},
id: 4000001,
text: '"fdghj" is not valid "email"',
type: "error",
},
],
meta: {
label: {
context: {
title: "E-Mail",
},
id: 1070002,
text: "E-Mail",
type: "info",
},
},
type: "input",
},
],
}, // Assuming nodes structure
},
} as unknown as OryFlowContainer

act(() => {
dispatch({
type: "action_flow_update",
flow: mockFlow,
})
})

const [state] = result.current
expect(state).toEqual({ current: "provide_identifier" })
})

test(`should parse state from flow on "action_flow_update" when choosing method on registration`, () => {
const { result } = renderHook(() => useFormStateReducer(init))
const [, dispatch] = result.current
Expand Down
4 changes: 3 additions & 1 deletion packages/elements-react/src/context/form-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ export type FormStateAction =
}

function findMethodWithMessage(nodes?: UiNode[]) {
return nodes?.find((node) => node.messages?.length > 0)
return nodes
?.filter((n) => !["default", "identifier_first"].includes(n.group))
?.find((node) => node.messages?.length > 0)
}

function parseStateFromFlow(flow: OryFlowContainer): FormState {
Expand Down

0 comments on commit 9aad2df

Please sign in to comment.