From 40e34ec65bb223edeb9653847196edf928faa96b Mon Sep 17 00:00:00 2001 From: Botis Date: Fri, 15 Dec 2023 19:06:32 +0100 Subject: [PATCH] draft --- .../ProductsCategoryPreview.tsx | 3 - .../ProductPreview/ProductsPreviewBottom.tsx | 2 +- .../ProductPreview/ProductsSearchPreview.tsx | 1 - .../pages/admin/analyze/CatalogStructure.tsx | 34 +++++--- packages/shared/src/services/explain.test.ts | 84 +++++-------------- packages/shared/src/services/explain.ts | 16 ++-- 6 files changed, 48 insertions(+), 92 deletions(-) diff --git a/packages/components/src/components/stateful/ProductPreview/ProductsCategoryPreview.tsx b/packages/components/src/components/stateful/ProductPreview/ProductsCategoryPreview.tsx index 4c871ce5..ba38637f 100644 --- a/packages/components/src/components/stateful/ProductPreview/ProductsCategoryPreview.tsx +++ b/packages/components/src/components/stateful/ProductPreview/ProductsCategoryPreview.tsx @@ -32,9 +32,6 @@ function ProductsCategoryPreview(props: IProps): JSX.Element { onTopProductsLoaded, } = props - onProductsLoaded(0) - onTopProductsLoaded(0) - const positionVariables = useMemo( () => ({ localizedCatalogId: Number( diff --git a/packages/components/src/components/stateful/ProductPreview/ProductsPreviewBottom.tsx b/packages/components/src/components/stateful/ProductPreview/ProductsPreviewBottom.tsx index 179f317c..554da3e7 100644 --- a/packages/components/src/components/stateful/ProductPreview/ProductsPreviewBottom.tsx +++ b/packages/components/src/components/stateful/ProductPreview/ProductsPreviewBottom.tsx @@ -69,7 +69,7 @@ function ProductsPreviewBottom(props: IProps): JSX.Element { const tableRows = (products?.data?.products?.collection as unknown as ITableRow[]) ?? [] - if (onProductsLoaded && products.status === LoadStatus.SUCCEEDED) { + if (typeof onProductsLoaded === 'function' && products.status === LoadStatus.SUCCEEDED) { onProductsLoaded(products.data.products.paginationInfo.totalCount) } diff --git a/packages/components/src/components/stateful/ProductPreview/ProductsSearchPreview.tsx b/packages/components/src/components/stateful/ProductPreview/ProductsSearchPreview.tsx index 86071e3f..1378eced 100644 --- a/packages/components/src/components/stateful/ProductPreview/ProductsSearchPreview.tsx +++ b/packages/components/src/components/stateful/ProductPreview/ProductsSearchPreview.tsx @@ -12,7 +12,6 @@ interface IProps { function ProductsSearchPreview(props: IProps): JSX.Element { const { variables, configuration, limitationType, onProductsLoaded } = props - onProductsLoaded(0) return ( ): void { event.preventDefault() if (isValidVariables(variables, limitationType)) { + setNbResults(0) + setNbTopProducts(0) setVariableValid(true) } else { setVariableValid(false) @@ -151,6 +154,10 @@ function AdminAnalyzeCatalogStructure(): JSX.Element { return null } + if (!variables.requestType && requestTypeOptions[0]) { + setVariables({...variables, requestType: requestTypeOptions[0].value ? String(requestTypeOptions[0].value) : undefined }) + } + return ( <>
- - {limitationType === 'category' && ( )} {limitationType === 'search' && ( - )} {limitationType ? ( diff --git a/packages/shared/src/services/explain.test.ts b/packages/shared/src/services/explain.test.ts index 1b62b2c5..416ded50 100644 --- a/packages/shared/src/services/explain.test.ts +++ b/packages/shared/src/services/explain.test.ts @@ -1,70 +1,26 @@ -import {isValidVariables} from "./explain"; +import {cleanExplainGraphQLVariables, isGraphQLValidVariables} from "./explain"; describe('Explain service', () => { describe('isValidVariables', () => { - it('Test if ', () => { - expect(isValidVariables({requestType:'product_catalog', search:'', localizedCatalog:'1'}, 'search')).toEqual('Hello there') - }) - }) - - describe('firstLetterLowercase', () => { - it('Should set first letter to lowercase', () => { - expect(firstLetterLowercase('Hello there')).toEqual('hello there') - }) - }) - - describe('getNameFromDefault', () => { - it('Should get the name without the default part', () => { - expect(getNameFromDefault('defaultMaxSize')).toEqual('maxSize') - }) - }) - - describe('humanize', () => { - it('Should humanize the label', () => { - expect(humanize('defaultLabel')).toEqual('Default label') - }) - }) - - describe('getHeadTitle', () => { - it('Should add Gally at the end of string getHeadTitle', () => { - expect(getHeadTitle('Catégories')).toEqual('Catégories - Gally') - }) - }) - - describe('getFieldLabelTranslationArgs', () => { - it('Should return field label (args for translation)', () => { - expect(getFieldLabelTranslationArgs('defaultLabel')).toEqual([ - 'fields.defaultLabel', - 'Default label', - ]) - expect(getFieldLabelTranslationArgs('defaultLabel', 'metadata')).toEqual([ - 'resources.metadata.fields.defaultLabel', - 'Default label', - ]) - }) - }) - - describe('addPrefixKeyObject', () => { - it('Should add the prefix "category", on all keys of the object', () => { - expect( - addPrefixKeyObject( - { localizedCatalog: 'com_fr', search: 'bag' }, - 'category' - ) - ).toEqual({ categoryLocalizedCatalog: 'com_fr', categorySearch: 'bag' }) - }) - }) - - describe('formatPrice', () => { - it('Should format price', () => { - expect(formatPrice(100, 'USD', 'fr-FR')).toContain('100,00') - expect(formatPrice(100, 'USD', 'fr-FR')).toContain('$US') - }) - }) - describe('getIdFromIri', () => { - it('Should format price', () => { - expect(getIdFromIri('/localized_catalog/1')).toEqual('1') - expect(getIdFromIri('/localized_catalog/random/10')).toEqual('10') + it('Should test if explain GraphQL variables are valid ', () => { + expect(isGraphQLValidVariables({ + requestType:'product_catalog', search:'bag', localizedCatalog:'1' + }, 'search')).toEqual(true) + expect(isGraphQLValidVariables({ + requestType:'product_catalog', search:'', localizedCatalog:'1' + }, 'search')).toEqual(false) + expect(isGraphQLValidVariables({ + requestType:'product_catalog', search:'', localizedCatalog:'' + }, 'search')).toEqual(false) + }) + }) + + describe('cleanExplainGraphQLVariables', () => { + it('Should clean explain GraphQL variables', () => { + expect(cleanExplainGraphQLVariables({ + requestType:'product_catalog', search:'bag', localizedCatalog:'1', category: { id: 'cat_7', name: 'Bag', level: 2, isVirtual: false, path: 'cat_2/cat_8' } + }, 'search')).toEqual({ + requestType:'product_catalog', search:'bag', localizedCatalog:'1', category: { id: 'cat_7', name: 'Bag', level: 2, isVirtual: false, path: 'cat_2/cat_8' }}) }) }) }) diff --git a/packages/shared/src/services/explain.ts b/packages/shared/src/services/explain.ts index b577dbf8..7ad63cc2 100644 --- a/packages/shared/src/services/explain.ts +++ b/packages/shared/src/services/explain.ts @@ -1,26 +1,20 @@ import { IExplainVariables } from '../types/explain' -export function isValidVariables( +export function isGraphQLValidVariables( variables: IExplainVariables, limitationType: string ): boolean { - // todo tests non valide car valeur peut être null par défaut. - let isValid = - variables.localizedCatalog !== undefined && - variables.requestType !== undefined + let isValid = Boolean(variables?.localizedCatalog) && Boolean(variables?.requestType) if (limitationType === 'category') { - isValid = isValid && variables.category !== undefined + isValid = isValid && Boolean(variables?.category) } else if (limitationType === 'search') { - isValid = - isValid && - variables.search !== undefined && - variables.search.trim() !== '' + isValid = isValid && Boolean(variables?.search?.trim()) } return isValid } -export function cleanExplainVariables( +export function cleanExplainGraphQLVariables( variables: IExplainVariables, limitationType: string ): IExplainVariables {