Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PWA-3273:Get Minimum Password Length from configuration #4319

Merged
merged 5 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Object {
"userName": "gooseton",
},
"isDisabled": false,
"minimum_password_length": 8,
"recaptchaWidgetProps": Object {},
}
`;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { useMutation, useApolloClient } from '@apollo/client';
import { useMutation, useQuery, useApolloClient } from '@apollo/client';
import { act } from 'react-test-renderer';

import { useCartContext } from '../../../context/cart';
Expand All @@ -16,9 +16,17 @@ jest.mock('@apollo/client', () => {
return {
...apolloClient,
useMutation: jest.fn().mockReturnValue([jest.fn()]),
useApolloClient: jest.fn()
useApolloClient: jest.fn(),
useQuery: jest.fn()
};
});
// jest.mock('@apollo/client', () => {
// const apolloClient = jest.requireActual('@apollo/client');
// return {
// ...apolloClient,
// useQuery: jest.fn()
// };
// });
jest.mock('../../../../lib/hooks/useAwaitQuery', () => ({
useAwaitQuery: jest.fn().mockReturnValue(jest.fn())
}));
Expand Down Expand Up @@ -91,7 +99,16 @@ const createAccountMutation = 'createAccountMutation';
const createCartMutation = 'createCartMutation';
const signInMutation = 'signInMutation';
const mergeCartsMutation = 'mergeCartsMutation';
const getStoreConfigQuery = 'getStoreConfigQuery';

const getStoreConfigQueryFn = jest.fn().mockReturnValue({
data: {
storeConfig: {
store_code: 'default',
minimum_password_length: 8
}
}
});
const customerQueryFn = jest.fn();
const getCartDetailsQueryFn = jest.fn();
const createAccountMutationFn = jest
Expand Down Expand Up @@ -120,6 +137,7 @@ const defaultProps = {
getCartDetailsQuery,
getCustomerQuery,
mergeCartsMutation,
getStoreConfigQuery,
signInMutation
},
initialValues: {
Expand All @@ -143,6 +161,13 @@ const defaultFormValues = {
};

beforeAll(() => {
useQuery.mockImplementation(query => {
if (query === getStoreConfigQuery) {
return getStoreConfigQueryFn();
} else {
return [jest.fn(), {}];
}
});
useAwaitQuery.mockImplementation(query => {
if (query === getCustomerQuery) {
return customerQueryFn();
Expand Down
12 changes: 11 additions & 1 deletion packages/peregrine/lib/talons/CreateAccount/createAccount.gql.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,22 @@ export const MERGE_CARTS = gql`
}
${CheckoutPageFragment}
`;
export const GET_STORE_CONFIG_DATA = gql`
query GetStoreConfigData {
# eslint-disable-next-line @graphql-eslint/require-id-when-available
storeConfig {
store_code
minimum_password_length
}
}
`;

export default {
createAccountMutation: CREATE_ACCOUNT,
createCartMutation: CREATE_CART,
getCartDetailsQuery: GET_CART_DETAILS,
getCustomerQuery: GET_CUSTOMER,
mergeCartsMutation: MERGE_CARTS,
signInMutation: SIGN_IN
signInMutation: SIGN_IN,
getStoreConfigQuery: GET_STORE_CONFIG_DATA
};
18 changes: 15 additions & 3 deletions packages/peregrine/lib/talons/CreateAccount/useCreateAccount.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useCallback, useMemo, useState } from 'react';
import { useApolloClient, useMutation } from '@apollo/client';
import { useApolloClient, useMutation, useQuery } from '@apollo/client';

import mergeOperations from '../../util/shallowMerge';
import { useUserContext } from '../../context/user';
Expand Down Expand Up @@ -37,7 +37,8 @@ export const useCreateAccount = props => {
getCartDetailsQuery,
getCustomerQuery,
mergeCartsMutation,
signInMutation
signInMutation,
getStoreConfigQuery
} = operations;
const apolloClient = useApolloClient();
const [isSubmitting, setIsSubmitting] = useState(false);
Expand Down Expand Up @@ -69,6 +70,15 @@ export const useCreateAccount = props => {
fetchPolicy: 'no-cache'
});

const { data: storeConfigData } = useQuery(getStoreConfigQuery, {
fetchPolicy: 'cache-and-network',
nextFetchPolicy: 'cache-first'
});

const { minimum_password_length } = storeConfigData
? storeConfigData.storeConfig
: null;

const fetchUserDetails = useAwaitQuery(getCustomerQuery);
const fetchCartDetails = useAwaitQuery(getCartDetailsQuery);

Expand Down Expand Up @@ -225,7 +235,8 @@ export const useCreateAccount = props => {
handleCancelKeyPress,
initialValues: sanitizedInitialValues,
isDisabled: isSubmitting || isGettingDetails || recaptchaLoading,
recaptchaWidgetProps
recaptchaWidgetProps,
minimum_password_length
};
};

Expand All @@ -239,6 +250,7 @@ export const useCreateAccount = props => {
*
* @property {GraphQLAST} customerQuery query to fetch customer details
* @property {GraphQLAST} getCartDetailsQuery query to get cart details
* @property {GraphQLAST} getStoreConfigQuery query to get store config
*/

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ jest.mock('@apollo/client', () => ({
}
]),
useQuery: jest.fn().mockImplementation(() => ({
data: {},
data: {
storeConfig: {
minimum_password_length: 8 // or whatever value is expected
}
},
loading: false,
error: null
}))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ const CreateAccount = props => {
handleCancelKeyPress,
isDisabled,
initialValues,
recaptchaWidgetProps
recaptchaWidgetProps,
minimum_password_length
} = talonProps;

const { formatMessage } = useIntl();
const classes = useStyle(defaultClasses, props.classes);

Expand Down Expand Up @@ -164,7 +166,7 @@ const CreateAccount = props => {
})}
validate={combine([
isRequired,
[hasLengthAtLeast, 8],
[hasLengthAtLeast, minimum_password_length],
validatePassword
])}
validateOnBlur
Expand Down